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

表结构

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<Put> arrayList = new ArrayList<Put>();
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<Put> arrayList = new ArrayList<Put>();
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<Put> arrayList = new ArrayList<Put>();
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 API连接和操作HBase数据库的更多相关文章

  1. 本地eclipse java api连接远程虚拟机HBase

    1.本地与远程连通 无论是域名或者ip都可以,另外需保证HBase在虚拟机集群上正常运行. 2.本地要有一个跟远程相同的hadoop环境 当然不相同,只要兼容也可以,现采用hadoop-2.5.0-c ...

  2. Java java jdbc thin远程连接并操作Oracle数据库

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

  3. Java连接并操作SQLServer数据库

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

  4. loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试

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

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

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

  6. JAVA API连接HDFS HA集群

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

  7. robot_framewok自动化测试--(9)连接并操作 MySql 数据库

    连接并操作 MySql 数据库 1.mysql数据库 1.1安装mysql数据库 请参考我的另一篇文章:MYSQL5.7下载安装图文教程 1.2.准备测试数据 请参考我的另一篇文章:Mysql基础教程 ...

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

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

  9. java API连接虚拟机上的hbase

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

随机推荐

  1. python的read() 、readline()、readlines()、xreadlines()

    先来一个小例子: import sys dir= os.path.dirname(os.path.abspath(__file__)) file_path='%s/test.txt'  % dir f ...

  2. 【Android 应用开发】Android 平台 HTTP网速测试 案例 API 分析

    作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速测试标准 : 除普通网页测速 ...

  3. cas 单点登录(SSO)实验之二: cas-client

    cas 单点登录(SSO)实验之二: cas-client 参考文章: http://my.oschina.net/indestiny/blog/200768#comments http://wenk ...

  4. (九)UIScrollView和PageControl的分页

    涉及到内容的滚动与拖拽,使用UIScrllView. 对于滚动的多张图片,由于超出屏幕,应该使用代码添加代码. 添加的细节是:图片的宽高即为滚动视图的宽高,图片的y=0,x=图片的序号乘以图片的宽度. ...

  5. 【Unity技巧】统一管理回调函数——观察者模式

    这次的内容有点类似设计模式里的观察者模式.但是和常规意义上的观察者模式也不是完全一致,所以各位就不要咬文嚼字啦!咦?设计模式?!不懂!没关系,说不定你以前就用过. 开场白 我们来想象一个场景.在加载一 ...

  6. 《java入门第一季》之面向对象(private关键字与封装概念的初探)

    /* 定义一个学生类: 成员变量:name,age 成员方法:show()方法 在使用这个案例的过程中,发现了一个问题: 通过对象去给成员变量赋值,可以赋值一些非法的数据.例如:name你赋值了一个3 ...

  7. 《java入门第一季》之面向对象(匿名对象)

    /* 匿名对象:就是没有名字的对象. 匿名对象的应用场景: A:调用方法,仅仅只调用一次的时候. 注意:调用多次的时候,不适合. 匿名对象调用完毕就是垃圾.可以被垃圾回收器回收,释放了系统资源. B: ...

  8. 仿百度壁纸客户端(六)——完结篇之Gallery画廊实现壁纸预览已经项目细节优化

    仿百度壁纸客户端(六)--完结篇之Gallery画廊实现壁纸预览已经项目细节优化 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Fragment 仿百度 ...

  9. Erlang Rebar 使用指南之二:制作发布版本

    Erlang Rebar 使用指南之二:制作发布版本 全文目录: https://github.com/rebar/rebar/wiki 本章位置: https://github.com/rebar/ ...

  10. 苹果新的编程语言 Swift 语言进阶(十六)--泛型

    泛型允许你定义一个宽松.可重用的函数或者类型,使用泛型能够避免代码的重复,也能以更清楚和抽象的方式来表达程序的意图. 泛型是Swift语言提供的强大功能之一,Swift提供的许多标准库都使用了泛型来创 ...