java访问hive的方式

保证hive --service hiveserver

1、jdbc方式

hive的lib下的jar全部都包含进去,另外要包含hadoop-0.20.2-core.jar

/**
 *  通过jdbc方式访问hive
 */
publicclass HiveClient {
 
    privatestatic Connection conn = null;
 
    privatestatic Connection connToMysql = null;
 
    privateHiveClient() {
 
    }
 
    publicstatic Connection GetHiveConn() throwsSQLException {
 
        if(conn == null) {
 
            try{
 
                Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
 
            }catch(ClassNotFoundException e) {
 
                e.printStackTrace();
 
                System.exit(1);
 
            }
 
            conn = DriverManager.getConnection(
 
                    "jdbc:hive://test01:5008/default","","");   //5008是启动hiveserver时设置的端口,默认是10000
        }
 
        returnconn;
 
    }
 
    publicstatic void closeHive() throwsSQLException {
 
        if(conn != null) {
 
            conn.close();
 
        }
 
    }
    publicstatic void main(String[] args) throwsSQLException {
 
        Connection conn = HiveClient.GetHiveConn();     
 
        Statement stmt = conn.createStatement();
 
        ResultSet rs = stmt.executeQuery("select * from user_device_base limit 1");
 
        while(rs.next()){
 
            System.out.println(rs.getString(1));
 
        }
    }
 
}


2、thrift方式

TSocket transport = newTSocket("test01",5008);
 
transport.setTimeout(1000);
 
TBinaryProtocol protocol = newTBinaryProtocol(transport);
 
Client client = newThriftHive.Client(protocol);
 
transport.open();
 
client.execute("select * from user_device_base limit 1"); // Omitted HQL
 
List<String> rows = null;
 
while((rows = client.fetchN(1000)) != null) {
 
    for(String row : rows) {
 
        System.out.println(row);
 
    }
}
transport.close();


pom dependency

<apache.hadoop.version>1.0.1</apache.hadoop.version>
 
<apache.hive.version>0.9.0</apache.hive.version>
 
 
 
<dependency>
 
    <groupId>org.apache.hive</groupId>
 
    <artifactId>hive-serde</artifactId>
 
    <optional>true</optional>
 
    <version>${apache.hive.version}</version>
 
</dependency>
 
 
<dependency>
 
    <groupId>org.apache.hive</groupId>
 
    <artifactId>hive-common</artifactId>
 
    <version>${apache.hive.version}</version>
 
</dependency>
 
<dependency>
 
    <groupId>org.apache.hive</groupId>
 
    <artifactId>hive-exec</artifactId>
 
    <version>${apache.hive.version}</version>
 
</dependency>
 
<dependency>
 
    <groupId>org.apache.hive</groupId>
 
    <artifactId>hive-jdbc</artifactId>
 
    <version>${apache.hive.version}</version>
 
</dependency>
 
<dependency>
 
    <groupId>org.apache.hadoop</groupId>
 
    <artifactId>hadoop-core</artifactId>
 
    <version>${apache.hadoop.version}</version>
 
</dependency>
 
<dependency>
 
    <groupId>org.apache.hadoop</groupId>
 
    <artifactId>hadoop-test</artifactId>
 
    <version>${apache.hadoop.version}</version>
 
    <scope>provided</scope>
 
</dependency>
 
<dependency>
 
    <groupId>javax.jdo</groupId>
 
    <artifactId>jdo2-api</artifactId>
 
    <version>2.3-eb</version>
 
    <scope>test</scope>
 
</dependency>
 
<!-- Needed forHive unit tests -->
 
<dependency>
 
    <groupId>org.apache.hive</groupId>
 
    <artifactId>hive-cli</artifactId>
 
    <version>${apache.hive.version}</version>
 
    <scope>test</scope>
 
</dependency>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中远程访问Hive,你可以使用Hive JDBC驱动程序。下面是一个简单的示例代码,演示如何连接到远程Hive服务器并执行查询: ```java import java.sql.*; public class HiveRemoteAccess { public static void(String[] args) { // 配置远程Hive服务器的连接信息 String driverName = "org.apache.hive.jdbc.HiveDriver"; String connectionUrl = "jdbc:hive2://<hive_server>:<port>/<database>"; // 连接到Hive服务器 try { Class.forName(driverName); Connection con = DriverManager.getConnection(connectionUrl, "<username>", "<password>"); // 创建Statement对象 Statement stmt = con.createStatement(); // 执行Hive查询 String query = "SELECT * FROM <table>"; ResultSet rs = stmt.executeQuery(query); // 处理查询结果 while (rs.next()) { // 读取每行数据 // TODO: 处理数据逻辑 } // 关闭连接 rs.close(); stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保将`<hive_server>`替换为实际的Hive服务器主机名或IP地址,`<port>`替换为Hive服务器的端口号,`<database>`替换为要连接的数据库名称,`<username>`和`<password>`替换为有效的用户名和密码。 代码中的TODO部分是处理查询结果的逻辑,你可以根据自己的需求进行处理。此示例使用的是Hive JDBC驱动程序,你需要将其添加到项目的依赖中。你可以从Apache Hive的官方网站或Maven仓库下载驱动程序。 希望这可以帮助到你实现Java远程访问Hive
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值