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

通过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 ORC
TBLPROPERTIES ("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中表的数据

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值