java生成数据插入hbase_使用Java API连接和操作HBase数据库

本文介绍了如何使用Java API连接HBase数据库,创建表`dept`并设置列族`info`和`subdept`。然后展示了如何插入数据,包括网络部、开发部和测试部及其子部门的数据。最后,提供了查询和操作数据的示例,如插入新部门、更新和删除部门信息。
摘要由CSDN通过智能技术生成

创建的数据库存储如下数据

表结构

java代码

1

2 public class HbaseTest {

3

4 /**

5 * 配置ss

6 */

7 static Configuration config = null;

8 private Connection connection = null;

9 private Table table = null;

10

11 @Before

12 public void init() throws Exception {

13 config = HBaseConfiguration.create();// 配置

14 config.set("hbase.zookeeper.quorum", "192.168.33.61");// zookeeper地址

15 config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口

16 connection = ConnectionFactory.createConnection(config);

17 table = connection.getTable(TableName.valueOf("dept"));

18 }

19

20 /**

21 * 创建数据库表dept,并增加列族info和subdept

22 *

23 * @throws Exception

24 */

25 @Test

26 public void createTable() throws Exception {

27 // 创建表管理类

28 HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理

29 // 创建表描述类

30 TableName tableName = TableName.valueOf("dept"); // 表名称

31 HTableDescriptor desc = new HTableDescriptor(tableName);

32 // 创建列族的描述类

33 HColumnDescriptor family = new HColumnDescriptor("info"); // 列族

34 // 将列族添加到表中

35 desc.addFamily(family);

36 HColumnDescriptor family2 = new HColumnDescriptor("subdept"); // 列族

37 // 将列族添加到表中

38 desc.addFamily(family2);

39 // 创建表

40 admin.createTable(desc); // 创建表

41 System.out.println("创建表成功!");

42 }

43

44 /**

45 * 向hbase中插入前三行网络部、开发部、测试部的相关数据,

46 * 即加入表中的前三条数据

47 *

48 * @throws Exception

49 */

50 @SuppressWarnings({ "deprecation", "resource" })

51 @Test

52 public void insertData() throws Exception {

53 table.setAutoFlushTo(false);

54 table.setWriteBufferSize(534534534);

55 ArrayList arrayList = new ArrayList();

56

57 Put put = new Put(Bytes.toBytes("0_1"));

58 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("网络部"));

59 put.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept1"), Bytes.toBytes("1_1"));

60 put.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept2"), Bytes.toBytes("1_2"));

61 arrayList.add(put);

62

63 Put put1 = new Put(Bytes.toBytes("1_1"));

64 put1.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发部"));

65 put1.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));

66

67 Put put2 = new Put(Bytes.toBytes("1_2"));

68 put2.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("测试部"));

69 put2.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));

70

71 for (int i = 1; i <= 100; i++) {

72

73 put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i), Bytes.toBytes("2_"+i));

74 put2.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i), Bytes.toBytes("3_"+i));

75 }

76 arrayList.add(put1);

77 arrayList.add(put2);

78 //插入数据

79 table.put(arrayList);

80 //提交

81 table.flushCommits();

82 System.out.println("数据插入成功!");

83 }

84

85 /**

86 * 向hbase中插入开发部、测试部下的所有子部门数据

87 * @throws Exception

88 */

89 @Test

90 public void insertOtherData() throws Exception {

91 table.setAutoFlushTo(false);

92 table.setWriteBufferSize(534534534);

93 ArrayList arrayList = new ArrayList();

94 for (int i = 1; i <= 100; i++) {

95 Put put_development = new Put(Bytes.toBytes("2_"+i));

96 put_development.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发"+i+"组"));

97 put_development.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("1_1"));

98 arrayList.add(put_development);

99

100 Put put_test = new Put(Bytes.toBytes("3_"+i));

101 put_test.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("测试"+i+"组"));

102 put_test.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("1_2"));

103 arrayList.add(put_test);

104 }

105

106 //插入数据

107 table.put(arrayList);

108 //提交

109 table.flushCommits();

110 System.out.println("插入其他数据成功!");

111 }

112

113 /**

114 * 查询所有一级部门(没有上级部门的部门)

115 * @throws Exception

116 */

117 @Test

118 public void scanDataStep1() throws Exception {

119

120 // 创建全表扫描的scan

121 Scan scan = new Scan();

122 System.out.println("查询到的所有一级部门如下:");

123 // 打印结果集

124 ResultScanner scanner = table.getScanner(scan);

125 for (Result result : scanner) {

126 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("f_pid")) == null) {

127 for (KeyValue kv : result.raw()) {

128 System.out.print(new String(kv.getRow()) + " ");

129 System.out.print(new String(kv.getFamily()) + ":");

130 System.out.print(new String(kv.getQualifier()) + " = ");

131 System.out.print(new String(kv.getValue()));

132 System.out.print(" timestamp = " + kv.getTimestamp() + "\n");

133 }

134 }

135 }

136 }

137

138 /**

139 * 已知rowkey,查询该部门的所有(直接)子部门信息 rowkey=1_1

140 * @throws Exception

141 */

142 @Test

143 public void scanDataStep2() throws Exception {

144 Get g = new Get("1_1".getBytes());

145 g.addFamily("subdept".getBytes());

146 // 打印结果集

147 Result result = table.get(g);

148 for (KeyValue kv : result.raw()) {

149 Get g1 = new Get(kv.getValue());

150 Result result1 = table.get(g1);

151 for (KeyValue kv1 : result1.raw()) {

152 System.out.print(new String(kv1.getRow()) + " ");

153 System.out.print(new String(kv1.getFamily()) + ":");

154 System.out.print(new String(kv1.getQualifier()) + " = ");

155 System.out.print(new String(kv1.getValue()));

156 System.out.print(" timestamp = " + kv1.getTimestamp() + "\n");

157 }

158 }

159 }

160

161 /**

162 * 已知rowkey,向该部门增加一个子部门

163 * rowkey:0_1

164 * 增加的部门名:我增加的部门

165 * @throws Exception

166 */

167 @Test

168 public void scanDataStep3() throws Exception {

169 //新增一个部门

170 Put put = new Put(Bytes.toBytes("4_1"));

171 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("我增加的部门"));

172 put.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));

173 //插入数据

174 table.put(put);

175 //提交

176 table.flushCommits();

177

178 //更新网络部

179 Put put1 = new Put(Bytes.toBytes("0_1"));

180 put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept3"), Bytes.toBytes("4_1"));

181 //插入数据

182 table.put(put1);

183 //提交

184 table.flushCommits();

185 }

186

187 /**

188 * 已知rowkey(且该部门存在子部门),删除该部门信息,该部门所有(直接)子部门被调整到其他部门中

189 * @throws Exception

190 */

191 @Test

192 public void scanDataStep4() throws Exception {

193 /**

194 * 向部门"我增加的部门"添加两个子部门"

195 */

196 table.setAutoFlushTo(false);

197 table.setWriteBufferSize(534534534);

198 ArrayList arrayList = new ArrayList();

199 Put put1 = new Put(Bytes.toBytes("5_1"));

200 put1.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("新增子部门1"));

201 put1.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("4_1"));

202 Put put2 = new Put(Bytes.toBytes("5_2"));

203 put2.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("新增子部门2"));

204 put2.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("4_1"));

205

206 arrayList.add(put1);

207 arrayList.add(put2);

208 //插入数据

209 table.put(arrayList);

210 //提交

211 table.flushCommits();

212

213 /**

214 * 目的:删除"我增加的部门"的部门信息,该部门所有(直接)子部门被调整到其他部门中

215 * 使用策略:更新部门名就可以了,也就是说一个部门可能有多个rowkey

216 */

217 Put put = new Put(Bytes.toBytes("4_1"));

218 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发部"));

219 //插入数据

220 table.put(put);

221 //提交

222 table.flushCommits();

223 }

224

225 @After

226 public void close() throws Exception {

227 table.close();

228 connection.close();

229 }

230

231 }

Java&&num;160&semi;java&&num;160&semi;jdbc&&num;160&semi;thin远程连接并操作Oracle数据库

JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...

Java连接并操作SQLServer数据库

本人只需在项目中引入sqljdbc4.jar 包即可 ----------------------------------------- 在JAVA中如何连接SQL Server数据库 - hangh ...

loadrunner&&num;160&semi;脚本开发-调用java&&num;160&semi;jar文件远程操作Oracle数据库测试

调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...

Python 使用Python远程连接并操作InfluxDB数据库

使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...

JAVA API连接HDFS HA集群

使用JAVA API连接HDFS时我们需要使用NameNode的地址,开启HA后,两个NameNode可能会主备切换,如果连接的那台主机NameNode挂掉了,连接就会失败. HDFS提供了names ...

HBase的Java Api连接失败的问题及解决方法

分布式方式部署的HBase,启动正常,Shell操作正常,使用HBase的Java Api操作时总是连接失败,信息如下: This server is in the failed servers li ...

java API连接虚拟机上的hbase

今天用本地的eclipse连接虚拟机上的hbase数据库,代码如下: public static void main(String[] args) throws Exception{ Configur ...

Python之操作HBASE数据库

目前有两个库可以操作HBASE:hbase-thrift 和  happybase happybase使用起来比较简单方便,因此重点学习该库,hbase-thrift只做简要介绍. (一)hbase- ...

怎样利用JDBC连接并操作Oracle数据库

之前学习.NET的时候.以前利用ODBC进行连接数据库,而在Java中通常採用JDBC连接数据库,这里以oracle数据库为例简单的总结一下利用JDBC怎样连接并操作数据库. 1.连接 public ...

随机推荐

svn的安装与使用

Eclipse安装SVN插件 1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版 如果没有安装的请到这里下载安装 ...

使用IDEA进行远程调试

虽然很早以前就只有Eclipse和IDEA都支持远程调试功能的,但是基本没怎么使用过,今天因为紧急处理一个问题,而本地环境搭建起来比较麻烦,所以就使用了IDEA的远程调试功能.因此写一篇文章记录一下. ...

linux项目-之监控-nagios

nagios core plugins 对象 主机(交换机,路由器,防火墙,服务器,虚拟机等),主机组 服务(主机上提供的服务如80,3306,1521,21等)/资源(cpu,内存使用情况,磁盘,网 ...

html简介

什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...

添加swap分区

1.创建swap文件,可以单独划分一个分区出来,也可以直接生成一个swap文件 dd if=/dev/zero of=swap bs=1M count=1024 2.格式化为swap文件 mkswap ...

mcd&comma; lm&comma; VS lx

LED常识之 mcd&lm&w的关系 转载自:http://1198.vip.blog.163.com/blog/static/202177117201211624535412/ LE ...

转&colon; linux下错误的捕获:errno和strerror的使用

经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...

JsonArray和JsonObject遍历方法

一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...

2018~第三届南宁市网络安全技术大赛~nnctf~write-up

Web 1.超简单 分值:100 类型:WEB 已解决 题目:超简单的web题  http://gxnnctf.gxsosec.cn:12311/ 代码审计 <?php $white_list ...

【Spark】Spark性能调优

官网:http://spark.apache.org/docs/latest/tuning.html 1.引言 提到Spark与Hadoop的区别,基本最常说的就是Spark采用基于内存的计算方式,尽 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值