Java 连接 Hive的样例程序及解析

以后编程基于这个样例可节省查阅API的时间。

 

private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

try{

  Class.forName(driverName);//取得数据驱动,应用不同的数据驱动可以用不同的class路径。

}catch (ClassNotFoundException e){

  e.printStackTrack();//optional

  System.exit(1);//optional

}

 

Connection con  = DriverManager.getConnection("jdbc:hive://localhost:10000/default","","");

//DriverManager : The basic service for managing a set of JDBC drivers.

//所用方法的详细说明如下:

//在这里,subprotocol = "hive";

Statement stmt = con.createStatement();

//A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments .

//可用于执行不带参数的简单 SQL 语句

 

String tableName = "CustomerHiveTableJava";

// Table to be created;

ResultSet res = stmt.executeQuery("create table "+tableName+"(custid int, fname string,lname string) row format delimited fields terminated by ',' stored as textfile");

//ResultSet A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

//show tables

String st = "show tables" + tableName +"";

System.out.println("Running :" + sql);

res = stmt.executeQuery(sql);//res记录所有返回的记录

if(res.next()){

  System.out.println(res.getString(1));

}

//Loading data into the table

String filepath = "/path";

sql = "load data local inpath"+filepath+"into table " + tableName;

System.out.println("Running :"+ sql);

转载于:https://www.cnblogs.com/xiamodeqiuqian/p/4887315.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值