Hive数据仓库--Java代码JDBC方式访问Hive中表的数据

来源:互联网 发布:mac版同花顺使用技巧 编辑:程序博客网 时间:2024/05/25 23:58

通过Java中jdbc的方式去操作Hive表中的数据,这个方式和之前web中使用的jdbc方式访问数据库是一致的。

转载注明出处:Hive数据仓库--Java代码JDBC方式访问Hive中表的数据

基本流程:

1. 加载驱动程序。

2. 建立连接。

3. 预编译sql语句。

4. 提交执行获取结果。

基本的程序如下:

try {Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");String selectSql = "select * from db.data where address = '11111111'";Connection connect = DriverManager.getConnection("jdbc:hive://192.168.xx.xx:10000/db", "xxx", "xxx");PreparedStatement state = null;state = connect.prepareStatement(selectSql);ResultSet resultSet = state.executeQuery();while (resultSet != null && resultSet.next()) {System.out.println(resultSet.getString(1) + "  " + resultSet.getString(2));}} catch (Exception e) {e.printStackTrace();}
需要的jar包:


注意一点:在运行之前请确保Hive thrift服务启动了,命令hive --service hiveserver


我当前的Hive的版本是0.13版本,我看官网上说0.14版本开始可以单独使用insert、update、和delete这些语句了,当然需要你进行适当的配置就可以了

基本的配置信息如下:

hive-site.xml中加入如下的配置

<property>  <name>hive.support.concurrency</name>  <value>true</value></property><property>  <name>hive.enforce.bucketing</name>  <value>true</value></property><property>  <name>hive.exec.dynamic.partition.mode</name>  <value>nonstrict</value></property><property>  <name>hive.txn.manager</name>  <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value></property><property>  <name>hive.compactor.initiator.on</name>  <value>true</value></property><property>  <name>hive.compactor.worker.threads</name>  <value>1</value></property><property>  <name>hive.in.test</name>  <value>true</value></property>


创建的表也相应的要改变一下,参考其他的博客中写的部分,tblproperties中只需要有transactional = true即可。


CREATE TABLE table_name (  id                int,  name              string)CLUSTERED BY (id) INTO 2 BUCKETS STORED AS ORCTBLPROPERTIES ("transactional"="true",  "compactor.mapreduce.map.memory.mb"="2048",     -- specify compaction map job properties  "compactorthreshold.hive.compactor.delta.num.threshold"="4",  -- trigger minor compaction if there are more than 4 delta directories  "compactorthreshold.hive.compactor.delta.pct.threshold"="0.5" -- trigger major compaction if the ratio of size of delta files to                                                                   -- size of base files is greater than 50%);

转载注明出处:Hive数据仓库--Java代码JDBC方式访问Hive中表的数据

0 0
原创粉丝点击