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

表结构

java代码

  1. 1
  2. 2 public class HbaseTest {
  3. 3
  4. 4 /**
  5. 5 * 配置ss
  6. 6 */
  7. 7 static Configuration config = null;
  8. 8 private Connection connection = null;
  9. 9 private Table table = null;
  10. 10
  11. 11 @Before
  12. 12 public void init() throws Exception {
  13. 13 config = HBaseConfiguration.create();// 配置
  14. 14 config.set("hbase.zookeeper.quorum", "192.168.33.61");// zookeeper地址
  15. 15 config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
  16. 16 connection = ConnectionFactory.createConnection(config);
  17. 17 table = connection.getTable(TableName.valueOf("dept"));
  18. 18 }
  19. 19
  20. 20 /**
  21. 21 * 创建数据库表dept,并增加列族info和subdept
  22. 22 *
  23. 23 * @throws Exception
  24. 24 */
  25. 25 @Test
  26. 26 public void createTable() throws Exception {
  27. 27 // 创建表管理类
  28. 28 HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理
  29. 29 // 创建表描述类
  30. 30 TableName tableName = TableName.valueOf("dept"); // 表名称
  31. 31 HTableDescriptor desc = new HTableDescriptor(tableName);
  32. 32 // 创建列族的描述类
  33. 33 HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
  34. 34 // 将列族添加到表中
  35. 35 desc.addFamily(family);
  36. 36 HColumnDescriptor family2 = new HColumnDescriptor("subdept"); // 列族
  37. 37 // 将列族添加到表中
  38. 38 desc.addFamily(family2);
  39. 39 // 创建表
  40. 40 admin.createTable(desc); // 创建表
  41. 41 System.out.println("创建表成功!");
  42. 42 }
  43. 43
  44. 44 /**
  45. 45 * 向hbase中插入前三行网络部、开发部、测试部的相关数据,
  46. 46 * 即加入表中的前三条数据
  47. 47 *
  48. 48 * @throws Exception
  49. 49 */
  50. 50 @SuppressWarnings({ "deprecation", "resource" })
  51. 51 @Test
  52. 52 public void insertData() throws Exception {
  53. 53 table.setAutoFlushTo(false);
  54. 54 table.setWriteBufferSize(534534534);
  55. 55 ArrayList<Put> arrayList = new ArrayList<Put>();
  56. 56
  57. 57 Put put = new Put(Bytes.toBytes("0_1"));
  58. 58 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("网络部"));
  59. 59 put.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept1"), Bytes.toBytes("1_1"));
  60. 60 put.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept2"), Bytes.toBytes("1_2"));
  61. 61 arrayList.add(put);
  62. 62
  63. 63 Put put1 = new Put(Bytes.toBytes("1_1"));
  64. 64 put1.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发部"));
  65. 65 put1.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));
  66. 66
  67. 67 Put put2 = new Put(Bytes.toBytes("1_2"));
  68. 68 put2.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("测试部"));
  69. 69 put2.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));
  70. 70
  71. 71 for (int i = 1; i <= 100; i++) {
  72. 72
  73. 73 put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i), Bytes.toBytes("2_"+i));
  74. 74 put2.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept"+i), Bytes.toBytes("3_"+i));
  75. 75 }
  76. 76 arrayList.add(put1);
  77. 77 arrayList.add(put2);
  78. 78 //插入数据
  79. 79 table.put(arrayList);
  80. 80 //提交
  81. 81 table.flushCommits();
  82. 82 System.out.println("数据插入成功!");
  83. 83 }
  84. 84
  85. 85 /**
  86. 86 * 向hbase中插入开发部、测试部下的所有子部门数据
  87. 87 * @throws Exception
  88. 88 */
  89. 89 @Test
  90. 90 public void insertOtherData() throws Exception {
  91. 91 table.setAutoFlushTo(false);
  92. 92 table.setWriteBufferSize(534534534);
  93. 93 ArrayList<Put> arrayList = new ArrayList<Put>();
  94. 94 for (int i = 1; i <= 100; i++) {
  95. 95 Put put_development = new Put(Bytes.toBytes("2_"+i));
  96. 96 put_development.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发"+i+"组"));
  97. 97 put_development.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("1_1"));
  98. 98 arrayList.add(put_development);
  99. 99
  100. 100 Put put_test = new Put(Bytes.toBytes("3_"+i));
  101. 101 put_test.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("测试"+i+"组"));
  102. 102 put_test.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("1_2"));
  103. 103 arrayList.add(put_test);
  104. 104 }
  105. 105
  106. 106 //插入数据
  107. 107 table.put(arrayList);
  108. 108 //提交
  109. 109 table.flushCommits();
  110. 110 System.out.println("插入其他数据成功!");
  111. 111 }
  112. 112
  113. 113 /**
  114. 114 * 查询所有一级部门(没有上级部门的部门)
  115. 115 * @throws Exception
  116. 116 */
  117. 117 @Test
  118. 118 public void scanDataStep1() throws Exception {
  119. 119
  120. 120 // 创建全表扫描的scan
  121. 121 Scan scan = new Scan();
  122. 122 System.out.println("查询到的所有一级部门如下:");
  123. 123 // 打印结果集
  124. 124 ResultScanner scanner = table.getScanner(scan);
  125. 125 for (Result result : scanner) {
  126. 126 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("f_pid")) == null) {
  127. 127 for (KeyValue kv : result.raw()) {
  128. 128 System.out.print(new String(kv.getRow()) + " ");
  129. 129 System.out.print(new String(kv.getFamily()) + ":");
  130. 130 System.out.print(new String(kv.getQualifier()) + " = ");
  131. 131 System.out.print(new String(kv.getValue()));
  132. 132 System.out.print(" timestamp = " + kv.getTimestamp() + "\n");
  133. 133 }
  134. 134 }
  135. 135 }
  136. 136 }
  137. 137
  138. 138 /**
  139. 139 * 已知rowkey,查询该部门的所有(直接)子部门信息 rowkey=1_1
  140. 140 * @throws Exception
  141. 141 */
  142. 142 @Test
  143. 143 public void scanDataStep2() throws Exception {
  144. 144 Get g = new Get("1_1".getBytes());
  145. 145 g.addFamily("subdept".getBytes());
  146. 146 // 打印结果集
  147. 147 Result result = table.get(g);
  148. 148 for (KeyValue kv : result.raw()) {
  149. 149 Get g1 = new Get(kv.getValue());
  150. 150 Result result1 = table.get(g1);
  151. 151 for (KeyValue kv1 : result1.raw()) {
  152. 152 System.out.print(new String(kv1.getRow()) + " ");
  153. 153 System.out.print(new String(kv1.getFamily()) + ":");
  154. 154 System.out.print(new String(kv1.getQualifier()) + " = ");
  155. 155 System.out.print(new String(kv1.getValue()));
  156. 156 System.out.print(" timestamp = " + kv1.getTimestamp() + "\n");
  157. 157 }
  158. 158 }
  159. 159 }
  160. 160
  161. 161 /**
  162. 162 * 已知rowkey,向该部门增加一个子部门
  163. 163 * rowkey:0_1
  164. 164 * 增加的部门名:我增加的部门
  165. 165 * @throws Exception
  166. 166 */
  167. 167 @Test
  168. 168 public void scanDataStep3() throws Exception {
  169. 169 //新增一个部门
  170. 170 Put put = new Put(Bytes.toBytes("4_1"));
  171. 171 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("我增加的部门"));
  172. 172 put.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_1"));
  173. 173 //插入数据
  174. 174 table.put(put);
  175. 175 //提交
  176. 176 table.flushCommits();
  177. 177
  178. 178 //更新网络部
  179. 179 Put put1 = new Put(Bytes.toBytes("0_1"));
  180. 180 put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("subdept3"), Bytes.toBytes("4_1"));
  181. 181 //插入数据
  182. 182 table.put(put1);
  183. 183 //提交
  184. 184 table.flushCommits();
  185. 185 }
  186. 186
  187. 187 /**
  188. 188 * 已知rowkey(且该部门存在子部门),删除该部门信息,该部门所有(直接)子部门被调整到其他部门中
  189. 189 * @throws Exception
  190. 190 */
  191. 191 @Test
  192. 192 public void scanDataStep4() throws Exception {
  193. 193 /**
  194. 194 * 向部门"我增加的部门"添加两个子部门"
  195. 195 */
  196. 196 table.setAutoFlushTo(false);
  197. 197 table.setWriteBufferSize(534534534);
  198. 198 ArrayList<Put> arrayList = new ArrayList<Put>();
  199. 199 Put put1 = new Put(Bytes.toBytes("5_1"));
  200. 200 put1.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("新增子部门1"));
  201. 201 put1.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("4_1"));
  202. 202 Put put2 = new Put(Bytes.toBytes("5_2"));
  203. 203 put2.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("新增子部门2"));
  204. 204 put2.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("4_1"));
  205. 205
  206. 206 arrayList.add(put1);
  207. 207 arrayList.add(put2);
  208. 208 //插入数据
  209. 209 table.put(arrayList);
  210. 210 //提交
  211. 211 table.flushCommits();
  212. 212
  213. 213 /**
  214. 214 * 目的:删除"我增加的部门"的部门信息,该部门所有(直接)子部门被调整到其他部门中
  215. 215 * 使用策略:更新部门名就可以了,也就是说一个部门可能有多个rowkey
  216. 216 */
  217. 217 Put put = new Put(Bytes.toBytes("4_1"));
  218. 218 put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("开发部"));
  219. 219 //插入数据
  220. 220 table.put(put);
  221. 221 //提交
  222. 222 table.flushCommits();
  223. 223 }
  224. 224
  225. 225 @After
  226. 226 public void close() throws Exception {
  227. 227 table.close();
  228. 228 connection.close();
  229. 229 }
  230. 230
  231. 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. 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程

     场景:实现安装一个apk应用程序的过程.界面如下: 编写如下应用,应用结构如下: <RelativeLayout   编写activity_main.xml布局: <Relative ...

  2. Win8 HTML5与JS编程学习笔记(二)

    近期一直受到win8应用的Grid布局困扰,经过了半下午加半个晚上的奋斗,终于是弄明白了Grid布局方法的规则.之前我是阅读的微软官方的开发教程,书中没有详细说明CSS3的布局规则,自己鼓捣了半天也是 ...

  3. TCP的核心系列 — SACK和DSACK的实现(六)

    上篇文章中我们主要说明如何skip到一个SACK块对应的开始段,如何walk这个SACK块包含的段,而没有涉及到 如何标志一个段的记分牌.37版本把给一个段打标志的内容独立出来,这就是tcp_sack ...

  4. 数据挖掘进阶之序列模式分析算法GSP的实现

    序列模式分析算法GSP的实现 一.算法简介 序列模式定义:给定一个由不同序列组成的集合,其中,每个序列由不同的元素按顺序有序排列,每个元素由不同项目组成,同时给定一个用户指定的最小支持度阈值,序列模式 ...

  5. 机房收费系统之导出Excel

            刚开始接触机房收费的时候,连上数据库,配置ODBC,登陆进去,那窗体叫一个多,不由地有种害怕的感觉,但是有人说,每天努力一点点,就会进步一点点,不会的就会少一点点,会的就会多一点点.. ...

  6. PHP基本的语法结构

    学过C语言的话,上手PHP语言就非常快了,如果你有bash shell的基础,那恭喜你,上手PHP会更快,我们先来了解一下一些比较简单的东西,界定符和注释在PHP中的写法: 一 php文档的语法结构 ...

  7. MATLAB坐标系中绘制图片

    MATLAB坐标系中绘制图片 方法一 使用图片坐标循环的方式,代码如下. clear,clc,close all tic; map=imbinarize(imread('map.bmp'));%map ...

  8. 锋利的Jquery摘要

    一本好书值得去反复回味 第一章: jquery中的$(document).ready(function(){})与js中的windows.onload()的比较   $(document).ready ...

  9. 【深入理解Java内存模型】

    深入理解Java内存模型(一)--基础 深入理解Java内存模型(二)--重排序 深入理解Java内存模型(三)--顺序一致性 深入理解Java内存模型(四)--volatile 深入理解Java内存 ...

  10. CSS的应用下

    样式继承: 就是父类的颜色如果变了,子类下的div(或者其他属性)会继承父类的. 参考代码: <!DOCTYPE html> <html lang="en"> ...