Java API 编程实例
本实例使用 Eclipse 编写 java 程序,来对 HBase 数据库进行增删改查等操作,Eclipse 可以在 Ubuntu 软件中心搜索下载并安装。
第一步:启动 hadoop,启动 hbase
$ cd /usr/local/hadoop
$ ./sbin/start-dfs.sh
$ cd /usr/local/hbase
$ ./bin/start-hbase.sh
第二步,新建 Java Project——>新建 Class
第三步:在工程中导入外部 jar 包:
这里只需要导入 hbase 安装目录中的 lib 文件中的所有 jar 包。
新版的 Hbase 1.1.2 的 java api 已经发生变化,旧版的部分 api 已经停止使用,教材上第四章编程实例部分,请以本教程为准。
这里给出一个编程实例,,以下是源代码:
1. import org.apache.hadoop.conf.Configuration;
2. import org.apache.hadoop.hbase.*;
3. import org.apache.hadoop.hbase.client.*;
4. import java.io.IOException;
5.
6. public class ExampleForHbase{
7. public static Configuration configuration;
8. public static Connection connection;
9. public static Admin admin;
10.
11. //主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时请将其他语句注释
12. public static void main(String[] args)throws IOException{
13. //创建一个表,表名为Score,列族为sname,course
14. createTable("Score",new String[]{"sname","course"});
15.
16. //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
17. //等价命令:put 'Score','95001','sname','Mary'
18. //insertRow("Score", "95001", "sname", "", "Mary");
19. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
20. //等价命令:put 'Score','95001','score:Math','88'
21. //insertRow("Score", "95001", "course", "Math", "88");
22. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
23. //等价命令:put 'Score','95001','score:English','85'
24. //insertRow("Score", "95001", "course", "English", "85");
25.
26. //1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
27. //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
28. //等价命令:delete 'Score','95001','score:Math'
29. //deleteRow("Score", "95001", "course", "Math");
30.
31. //2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
32. //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
33. //等价命令:delete 'Score','95001','score'
34. //deleteRow("Score", "95001", "course", "");
35.
36. //3、删除Score表中指定行数据,其行键为95001
37. //执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
38. //等价命令:deleteall 'Score','95001'
39. //deleteRow("Score", "95001", "", "");
40.
41. //查询Score表中,行键为95001,列族为course,列为Math的值
42. //getData("Score", "95001", "course", "Math");
43. //查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
44. //getData("Score", "95001", "sname", "");
45.
46. //删除Score表
47. //deleteTable("Score");
48. }
49.
50. //建立连接
51. public static void init(){
52. configuration = HBaseConfiguration.create();
53. configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
54. try{
55. connection = ConnectionFactory.createConnection(configuration);
56. admin = connection.getAdmin();
57. }catch (IOException e){
58. e.printStackTrace();
59. }
60. }
61. //关闭连接
62. public static void close(){
63. try{
64. if(admin != null){
65. admin.close();
66. }
67. if(null != connection){
68. connection.close();
69. }
70. }catch (IOException e){
71. e.printStackTrace();
72. }
73. }
74.
75. /**
76. * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
77. * @param myTableName 表名
78. * @param colFamily 列族名
79. * @throws IOException
80. */
81. public static void createTable(String myTableName,String[] colFamily) throws IOException {
82.
83. init();
84. TableName tableName = TableName.valueOf(myTableName);
85.
86. if(admin.tableExists(tableName)){
87. System.out.println("talbe is exists!");
88. }else {
89. HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
90. for(String str:colFamily){
91. HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
92. hTableDescriptor.addFamily(hColumnDescriptor);
93. }
94. admin.createTable(hTableDescriptor);
95. System.out.println("create table success");
96. }
97. close();
98. }
99. /**
100. * 删除指定表
101. * @param tableName 表名
102. * @throws IOException
103. */
104. public static void deleteTable(String tableName) throws IOException {
105. init();
106. TableName tn = TableName.valueOf(tableName);
107. if (admin.tableExists(tn)) {
108. admin.disableTable(tn);
109. admin.deleteTable(tn);
110. }
111. close();
112. }
113.
114. /**
115. * 查看已有表
116. * @throws IOException
117. */
118. public static void listTables() throws IOException {
119. init();
120. HTableDescriptor hTableDescriptors[] = admin.listTables();
121. for(HTableDescriptor hTableDescriptor :hTableDescriptors){
122. System.out.println(hTableDescriptor.getNameAsString());
123. }
124. close();
125. }
126. /**
127. * 向某一行的某一列插入数据
128. * @param tableName 表名
129. * @param rowKey 行键
130. * @param colFamily 列族名
131. * @param col 列名(如果其列族下没有子列,此参数可为空)
132. * @param val 值
133. * @throws IOException
134. */
135. public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
136. init();
137. Table table = connection.getTable(TableName.valueOf(tableName));
138. Put put = new Put(rowKey.getBytes());
139. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
140. table.put(put);
141. table.close();
142. close();
143. }
144.
145. /**
146. * 删除数据
147. * @param tableName 表名
148. * @param rowKey 行键
149. * @param colFamily 列族名
150. * @param col 列名
151. * @throws IOException
152. */
153. public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
154. init();
155. Table table = connection.getTable(TableName.valueOf(tableName));
156. Delete delete = new Delete(rowKey.getBytes());
157. //删除指定列族的所有数据
158. //delete.addFamily(colFamily.getBytes());
159. //删除指定列的数据
160. //delete.addColumn(colFamily.getBytes(), col.getBytes());
161.
162. table.delete(delete);
163. table.close();
164. close();
165. }
166. /**
167. * 根据行键rowkey查找数据
168. * @param tableName 表名
169. * @param rowKey 行键
170. * @param colFamily 列族名
171. * @param col 列名
172. * @throws IOException
173. */
174. public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
175. init();
176. Table table = connection.getTable(TableName.valueOf(tableName));
177. Get get = new Get(rowKey.getBytes());
178. get.addColumn(colFamily.getBytes(),col.getBytes());
179. Result result = table.get(get);
180. showCell(result);
181. table.close();
182. close();
183. }
184. /**
185. * 格式化输出
186. * @param result
187. */
188. public static void showCell(Result result){
189. Cell[] cells = result.rawCells();
190. for(Cell cell:cells){
191. System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
192. System.out.println("Timetamp:"+cell.getTimestamp()+" ");
193. System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
194. System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
195. System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
196. }
197. }
198. }
每次执行完,都可以回到 shell 界面查看是否执行成功,如:执行完插入数据后,在 shell 界面中执行scan 'Score'
。截图如下: