码迷,mamicode.com
首页 > 数据库 > 详细

Hive JDBC 连接hiveserver2

时间:2017-12-18 18:45:32      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:plugin   exists   pack   xsd   tar   sts   oal   cep   private   

1.启动hiveserver2

nohup /home/hadoop/hive-1.1.0-cdh5.5.2/bin/hiveserver2 >> /home/hadoop/gtq_dir/logs/hiveserver.log 2>&1 &

2.代码如下:

package cn.hive;

import java.sql.*;

/**
 * Created by jieyue on 2017/12/18.
 */
public class HiveJdbcTest {
    private static String driveName = "org.apache.hive.jdbc.HiveDriver";
    private static Connection con = null;
    private static Statement stmt = null;
    private static ResultSet res = null;

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driveName);
            con = DriverManager.getConnection("jdbc:hive2://172.17.101.12:10000/demo", "root", "");
            stmt = con.createStatement();
            String tableName = "t_h_jdbc";
            stmt.execute("DROP TABLE IF EXISTS " + tableName);
            stmt.execute("create table " + tableName + "(key int ,value string)");
            System.out.println("create table success!!");

            String sql = "show create table " + tableName;
            System.out.println("Running :" + sql);
            res = stmt.executeQuery(sql);
            if (res.next()) System.out.println(res.getString(1));

            sql = "desc " + tableName;
            System.out.println("Running :" + sql);
            res = stmt.executeQuery(sql);
            while (res.next()) System.out.println(res.getString(1) + "\t" + res.getString(2));

            sql = "select * from " + tableName;
            System.out.println("Running :" + sql);
            res = stmt.executeQuery(sql);
            while (res.next()) System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));

            sql = "select count(*) from " + tableName;
            res = stmt.executeQuery(sql);
            System.out.println("Running :" + sql);
            while (res.next()) System.out.println(res.getString(1));

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (res != null) res.close();
            if (stmt != null) stmt.close();
            if (con != null) con.close();
        }

    }
}

3.pom文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>mm</groupId>
    <artifactId>MyDemoApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hadoop.version>2.6.0</hadoop.version>
        <hive.version>1.1.0</hive.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-auth</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>jdk.tools</artifactId>
                    <groupId>jdk.tools</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>jdk.tools</artifactId>
                    <groupId>jdk.tools</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>jdk.tools</artifactId>
                    <groupId>jdk.tools</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>jdk.tools</artifactId>
                    <groupId>jdk.tools</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>

                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

Hive JDBC 连接hiveserver2

标签:plugin   exists   pack   xsd   tar   sts   oal   cep   private   

原文地址:http://www.cnblogs.com/guotianqi/p/8057740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!