当前位置:Gxlcms > 数据库问题 > HIVE JDBC连接详解

HIVE JDBC连接详解

时间:2021-07-01 10:21:17 帮助过:16人阅读


package org.conan.myhadoop.mr;

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

public class HiveJDBCConnection {

    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
    // Hive 0.11.0版本以后org.apache.hadoop.hive.jdbc.HiveDriver
    private static String url = "jdbc:hive://localhost:10000/default";
    // Hive 0.11.0版本以后jdbc:hive2://localhost:10000/default
    private static String userName = "";
    private static String passWord = "";

    public static void main(String[] args) {
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection("url", "userName",
                    "passWord");
            Statement stmt = con.createStatement();
            // 如果存在了就删除
            String tableName = "jdbc_table";
            String sql = "drop table if exists " + tableName;
            stmt.execute(sql);
            // 创建表
            sql = "create table"
                    + tableName
                    + " (key string,value string) row format delimited fields terminated by ‘,‘  stored as textfile ";
            stmt.execute(sql);
            //加载数据
            String Path="/home/hive_1.txt";
            sql ="load data local inpath ‘"+Path+"‘ into table "+tableName;
            stmt.execute(sql);
            // 查询数据
            sql ="select * from "+tableName;
            ResultSet res = stmt.executeQuery(sql);
            while(res.next()){
                System.out.println(res.getString(1)+"\t"+res.getString(1));
                
            }
            
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到驱动类");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("连接Hive的信息有问题");
            e.printStackTrace();
        }

    }
}

上面是用Java连接HiveServer,而HiveServer本身存在很多问题(比如:安全性、并发性等);针对这些问题,Hive0.11.0版本提供了一个全新的服务:HiveServer2,这个很好的解决HiveServer存在的安全性、并发性等问题。

上面的userName和passWord是hive元数据mysql的用户名和密码


参考文章:

http://www.iteblog.com/archives/846


本文出自 “一无所有 QQ:934033381” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1698639

HIVE JDBC连接详解

标签:hive jdbc连接详解

人气教程排行