您当前的位置: 首页 >  hive

宝哥大数据

暂无认证

  • 4浏览

    0关注

    1029博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

hive的JDBC模式

宝哥大数据 发布时间:2017-12-25 12:24:13 ,浏览量:4

hive的JDBC模式 : HIVE版本: apache-hive-0.14.0-bin JAVA API交互执行方式 hive 远程服务 (端口号10000) 启动方式

hive --service hiveserver2 

驱动:

org.apache.hive.jdbc.HiveDriver

在java代码中调用hive的JDBC建立连接

1、配置hive-site.xml, 设置mysql数据库连接

        
                javax.jdo.option.ConnectionURL
                jdbc:mysql://192.168.1.225:3307/hive?createDatabaseIfNotExist=true
        
        
                javax.jdo.option.ConnectionDriverName
                com.mysql.jdbc.Driver
        
        
                javax.jdo.option.ConnectionUserName
                root
        
        
                javax.jdo.option.ConnectionPassword
                root
        
        
                hive.querylog.location
                /opt/apache-hive-0.14.0-bin/tmp
        
        
                hive.exec.local.scratchdir
                /opt/apache-hive-0.14.0-bin/tmp
        
        
                hive.downloaded.resources.dir
                /opt/apache-hive-0.14.0-bin/tmp
        
2、启动hive远程服务
hive --service hiveserver2 >/dev/null  2>/dev/null &
3、java连接hive
package com.chb.hiveJdbcUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HiveJdbcUtils {
    public static void main(String[] args) {
        Connection conn = getConnection();
        try {
            PreparedStatement ps = conn.prepareStatement("show tables");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection()  {
        Connection conn = null;
        String ip = "192.168.1.225";
        System.out.println("开始连接。。。");
        try {
            //Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");早期是使用驱动
            Class.forName("org.apache.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //conn = DriverManager.getConnection("jdbc:hive://"+ip+":10000/default","","");//早期使用jdbc:hive
            conn = DriverManager.getConnection(
                    "jdbc:hive2://"+ip+":10000/default",//查询hive中的数据库
                    "",
                    "");
            System.out.println("连接成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
关注
打赏
1587549273
查看更多评论
立即登录/注册

微信扫码登录

0.1379s