在集群中Java 通过调用API操作HBase_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 在集群中Java 通过调用API操作HBase

在集群中Java 通过调用API操作HBase

 2016/5/22 5:33:12  aoyouzi  程序员俱乐部  我要评论(0)
  • 摘要:本文的内容是在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据,查询一条数据,查询所有数据等操作。具体流程如下:1.创建项目2.获取jar包到项目的lib目录下(这边试用的事hbase0.98lib目录下的所有jar包)3.编写java程序4.编写ant脚本packagecom.wan.hbase;importjava.io.IOException;importorg.apache.Hadoop.conf.Configuration
  • 标签:API Java 操作 ASE
本文的内容是在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据,查询一条数据,查询所有数据等操作。

具体流程如下:

1.创建项目
2.获取jar包到项目的lib目录下(这边试用的事hbase 0.98 lib目录下的所有jar包)
3.编写java程序
4.编写ant脚本 ? ?
  1. package com.wan.hbase;
  2. import java.io.IOException;
  3. import org.apache.Hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.Cell;
  5. import org.apache.hadoop.hbase.CellUtil;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.HColumnDescriptor;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.MasterNotRunningException;
  10. import org.apache.hadoop.hbase.TableName;
  11. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  12. import org.apache.hadoop.hbase.client.Delete;
  13. import org.apache.hadoop.hbase.client.Get;
  14. import org.apache.hadoop.hbase.client.HBaseAdmin;
  15. import org.apache.hadoop.hbase.client.HTable;
  16. import org.apache.hadoop.hbase.client.Put;
  17. import org.apache.hadoop.hbase.client.Result;
  18. import org.apache.hadoop.hbase.client.ResultScanner;
  19. import org.apache.hadoop.hbase.client.Scan;
  20. import org.apache.hadoop.hbase.util.Bytes;
  21. public class SimpleHBase {
  22. public static void main(String[] args) {
  23. ??Configuration configuration=HBaseConfiguration.create();
  24. ??String tableName="student";
  25. ??createTable(configuration, tableName);
  26. //??addData(configuration, tableName);
  27. //??getData(configuration, tableName);
  28. //??getAllData(configuration, tableName);
  29. //??deleteDate(configuration, tableName);
  30. //??dropTable(configuration, tableName);
  31. ??
  32. }
  33. public static void createTable(Configuration configuration,String tableName){
  34. ??HBaseAdmin admin;
  35. ??try {
  36. ???admin = new HBaseAdmin(configuration);
  37. ???if(admin.tableExists(tableName)){
  38. ????admin.disableTable(tableName);
  39. ????admin.deleteTable(tableName);
  40. ????System.out.println(tableName+"is exist ,delete ......");
  41. ???}
  42. ???
  43. ???
  44. ???HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
  45. ???tableDescriptor.addFamily(new HColumnDescriptor("info"));
  46. ???tableDescriptor.addFamily(new HColumnDescriptor("address"));
  47. ???admin.createTable(tableDescriptor);
  48. ???System.out.println("end create table");
  49. ??} catch (MasterNotRunningException e) {
  50. ???// TODO Auto-generated catch block
  51. ???e.printStackTrace();
  52. ??} catch (ZooKeeperConnectionException e) {
  53. ???// TODO Auto-generated catch block
  54. ???e.printStackTrace();
  55. ??} catch (IOException e) {
  56. ???// TODO Auto-generated catch block
  57. ???e.printStackTrace();
  58. ??}
  59. ??
  60. }
  61. public static void dropTable(Configuration configuration,String tableName){
  62. ??HBaseAdmin admin;
  63. ??try {
  64. ???admin = new HBaseAdmin(configuration);
  65. ???if(admin.tableExists(tableName)){
  66. ????admin.disableTable(tableName);
  67. ????admin.deleteTable(tableName);
  68. ????System.out.println(tableName+"delete success!");
  69. ???}else{
  70. ????System.out.println(tableName+"Table does not exist!");
  71. ???}
  72. ??} catch (MasterNotRunningException e) {
  73. ???// TODO Auto-generated catch block
  74. ???e.printStackTrace();
  75. ??} catch (ZooKeeperConnectionException e) {
  76. ???// TODO Auto-generated catch block
  77. ???e.printStackTrace();
  78. ??} catch (IOException e) {
  79. ???// TODO Auto-generated catch block
  80. ???e.printStackTrace();
  81. ??}
  82. }
  83. public static void addData(Configuration configuration,String tableName){
  84. ??HBaseAdmin admin;
  85. ??try {
  86. ???admin = new HBaseAdmin(configuration);
  87. ???if(admin.tableExists(tableName)){
  88. ????HTable table=new HTable(configuration, tableName);
  89. ????Put put=new Put(Bytes.toBytes("zhangsan"));
  90. ????put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
  91. ????table.put(put);
  92. ????System.out.println("add success!");
  93. ???}else{
  94. ????System.out.println(tableName+"Table does not exist!");
  95. ???}
  96. ??} catch (MasterNotRunningException e) {
  97. ???// TODO Auto-generated catch block
  98. ???e.printStackTrace();
  99. ??} catch (ZooKeeperConnectionException e) {
  100. ???// TODO Auto-generated catch block
  101. ???e.printStackTrace();
  102. ??} catch (IOException e) {
  103. ???// TODO Auto-generated catch block
  104. ???e.printStackTrace();
  105. ??}
  106. }
  107. public static void deleteDate(Configuration configuration,String tableName){
  108. ??HBaseAdmin admin;
  109. ??try {
  110. ???admin=new HBaseAdmin(configuration);
  111. ???if(admin.tableExists(tableName)){
  112. ????HTable table=new HTable(configuration, tableName);
  113. ????Delete delete=new Delete(Bytes.toBytes("zhangsan"));
  114. ????table.delete(delete);
  115. ????System.out.println("delete success!");
  116. ???}else{
  117. ????System.out.println("Table does not exist!");
  118. ???}
  119. ??} catch (MasterNotRunningException e) {
  120. ???// TODO Auto-generated catch block
  121. ???e.printStackTrace();
  122. ??} catch (ZooKeeperConnectionException e) {
  123. ???// TODO Auto-generated catch block
  124. ???e.printStackTrace();
  125. ??} catch (IOException e) {
  126. ???// TODO Auto-generated catch block
  127. ???e.printStackTrace();
  128. ??}
  129. }
  130. public static void getData(Configuration configuration,String tableName){
  131. ??HTable table;
  132. ??try {
  133. ???table = new HTable(configuration, tableName);
  134. ???Get get=new Get(Bytes.toBytes("zhangsan"));
  135. ???Result result=table.get(get);
  136. ???for(Cell cell:result.rawCells()){???
  137. ????System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
  138. ????System.out.println("Timetamp:"+cell.getTimestamp()+" ");
  139. ????System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
  140. ????System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
  141. ????System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
  142. ???}
  143. ??} catch (IOException e) {
  144. ???// TODO Auto-generated catch block
  145. ???e.printStackTrace();
  146. ??}
  147. }
  148. public static void getAllData(Configuration configuration,String tableName){
  149. ??HTable table;
  150. ??try {
  151. ???table=new HTable(configuration, tableName);
  152. ???Scan scan=new Scan();
  153. ???ResultScanner results=table.getScanner(scan);
  154. ???for(Result result:results){
  155. ????for(Cell cell:result.rawCells()){???
  156. ?????System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
  157. ?????System.out.println("Timetamp:"+cell.getTimestamp()+" ");
  158. ?????System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
  159. ?????System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
  160. ?????System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
  161. ????}
  162. ???}
  163. ??} catch (IOException e) {
  164. ???// TODO Auto-generated catch block
  165. ???e.printStackTrace();
  166. ??}
  167. ????
  168. }
  169. }

?

?

?

?

?

http://blog.sina.com.cn/s/blog_68674da70102vaaw.html

发表评论
用户名: 匿名