1 package com.bawei.hbase;
  2 
  3 import org.apache.hadoop.conf.Configuration;
  4 import org.apache.hadoop.hbase.*;
  5 import org.apache.hadoop.hbase.client.*;
  6 import org.apache.hadoop.hbase.filter.CompareFilter;
  7 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
  8 import org.apache.hadoop.hbase.util.Bytes;
  9 
 10 import java.io.IOException;
 11 
 12 public class HbaseApiTest {
 13 
 14     //创建连接 静态的单例
 15     private static Connection connection = null;
 16 
 17     //初始化connection
 18     static {
 19 
 20         //创建配置对象
 21         Configuration conf = HBaseConfiguration.create();
 22         //配zookeeper的主机名
 23         conf.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
 24         //设置端口的参数
 25         conf.set("hbase.zookeeper.property.clientPort", "2181");
 26         //实例化connection
 27         try {
 28             connection = ConnectionFactory.createConnection(conf);
 29         } catch (IOException e) {
 30             e.printStackTrace();
 31         }
 32 
 33     }
 34     //创建表
 35     private static void createTable(String tableName, String... families) throws IOException {
 36         //创建表先要获得admin对象
 37         Admin admin = connection.getAdmin();
 38         try {
 39             //判断表是否存在
 40             if (admin.tableExists(TableName.valueOf(tableName))) {
 41                 System.out.println("table:" + tableName + "exists!");
 42                 return;
 43             }
 44 
 45             //创建表的描述器
 46             HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
 47 
 48             //添加列族的信息 因为列族可能是多个,所以需要遍历families
 49             for (String family : families) {
 50                 //创建列族的描述器
 51                 HColumnDescriptor familyDesc = new HColumnDescriptor(family);
 52                 //需要传入列族的描述器
 53                 desc.addFamily(familyDesc);
 54             }
 55             //创建表的方法 需要表的描述器
 56             admin.createTable(desc);
 57         } finally {
 58 
 59             admin.close();
 60         }
 61 
 62     }
 63 
 64 
 65     //删除表
 66 
 67     public static void dropTable(String tableName) throws IOException {
 68 
 69         Admin admin = connection.getAdmin();
 70         try {
 71             //判断表是否存在
 72             if (!admin.tableExists(TableName.valueOf(tableName))) {
 73                 System.out.println("table:" + tableName + " not exists!");
 74                 return;
 75             }
 76             admin.disableTable(TableName.valueOf(tableName));
 77             admin.deleteTable(TableName.valueOf(tableName));
 78 
 79         } finally {
 80             admin.close();
 81         }
 82 
 83     }
 84 
 85 
 86     //判断表是否存在
 87 
 88     public static boolean tableExist(String tableName) throws IOException {
 89         Admin admin = connection.getAdmin();
 90         try {
 91             return admin.tableExists(TableName.valueOf(tableName));
 92         } finally {
 93             admin.close();
 94         }
 95     }
 96 
 97 
 98     //插入数据:一行中的一个列
 99 
100     public static void putCell(String tableName, String rowKey, String family, String column, String value) throws IOException {
101 
102         if (!tableExist(tableName)) {
103             System.out.println("table:" + tableName + " not exists!");
104             return;
105         }
106 
107         Table table = connection.getTable(TableName.valueOf(tableName));
108 
109         try {
110             Put put = new Put(Bytes.toBytes(rowKey));
111             put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
112 
113             table.put(put);
114         } finally {
115             table.close();
116         }
117 
118     }
119 
120 
121     //删除一行数据
122 
123     public static void deleteRow(String tableName, String rowKey) throws IOException {
124 
125         if (!tableExist(tableName)) {
126             System.out.println("table:" + tableName + " not exists!");
127             return;
128         }
129 
130         Table table = connection.getTable(TableName.valueOf(tableName));
131         try {
132             Delete delete = new Delete(Bytes.toBytes(rowKey));
133             table.delete(delete);
134         } finally {
135             table.close();
136         }
137 
138 
139     }
140 
141 
142     //删除一行中的一个列族
143 
144     public static void deleteFamily(String tableName, String rowKey, String family) throws IOException {
145         if (!tableExist(tableName)) {
146             System.out.println("table:" + tableName + " not exists!");
147             return;
148         }
149 
150         Table table = connection.getTable(TableName.valueOf(tableName));
151 
152         Delete delete = new Delete(Bytes.toBytes(rowKey));
153         delete.addFamily(Bytes.toBytes(family));
154         table.delete(delete);
155 
156         table.close();
157 
158     }
159 
160 
161     //删除一行中的一个列的最新版本
162 
163     public static void deleteCell(String tableName, String rowKey, String family, String column) throws IOException {
164 
165         if (!tableExist(tableName)) {
166             System.out.println("table:" + tableName + " not exists!");
167             return;
168         }
169 
170         Table table = connection.getTable(TableName.valueOf(tableName));
171 
172         Delete delete = new Delete(Bytes.toBytes(rowKey));
173         delete.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
174         table.delete(delete);
175 
176         table.close();
177 
178     }
179 
180 
181     //删除一行中的一个列的所有版本
182 
183     public static void deleteCells(String tableName, String rowKey, String family, String column) throws IOException {
184 
185         if (!tableExist(tableName)) {
186             System.out.println("table:" + tableName + " not exists!");
187             return;
188         }
189 
190         Table table = connection.getTable(TableName.valueOf(tableName));
191 
192         Delete delete = new Delete(Bytes.toBytes(rowKey));
193         delete.addColumns(Bytes.toBytes(family), Bytes.toBytes(column));
194         table.delete(delete);
195 
196         table.close();
197 
198     }
199 
200 
201     //获取一行数据
202 
203     public static void getRow(String tableName, String rowKey) throws IOException {
204 
205         Table table = connection.getTable(TableName.valueOf(tableName));
206 
207         Get get = new Get(Bytes.toBytes(rowKey));
208 
209         //get之后需要有返回值
210         //一行数据有多个列,也就是多个cell,所以reoult返回的应该是一个集合
211         Result result = table.get(get);
212         //获取多个列
213         Cell[] cells = result.rawCells();
214         //遍历cells cells.iter
215         for (Cell cell : cells) {
216             //cell在底层存的是k-v,有哪些信息 rowKey 列族 列  + value
217 
218             //获取列名
219             byte[] columnBytes = CellUtil.cloneQualifier(cell);
220 
221             String columnStr = Bytes.toString(columnBytes);
222 
223             //使用cell的工具类获取value
224             byte[] valueBytes = CellUtil.cloneValue(cell);
225             //把字节数组变成string
226             String valueStr = Bytes.toString(valueBytes);
227             //打印输出
228             System.out.println(columnStr + ":" + valueStr);
229         }
230 
231         table.close();
232 
233     }
234 
235 
236     //根据rowKey的范围获取多行数据
237 
238     public static void getRowsByRange(String tableName, String startRow, String stopRow) throws IOException {
239 
240         Table table = connection.getTable(TableName.valueOf(tableName));
241         Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
242 
243         //使用scan会返回大量的数据,数据需要一批一批的返回,
244         // 所以scanner不是一个集合而是一个连接器,用完之后需要关掉
245         // 返回的scaner是多行,每一行里有多个列
246         ResultScanner scanner = table.getScanner(scan);
247         //scanner为什么会有iter? 实现了Iterable接口
248         for (Result result : scanner) {
249 
250             //获取result数组 然后result.rawCells().iter
251             for (Cell cell : result.rawCells()) {
252 
253                 //获取rowkey
254                 byte[] rowBytes = CellUtil.cloneRow(cell);
255                 String rowKey = Bytes.toString(rowBytes);
256                 //获取列
257                 byte[] columnBytes = CellUtil.cloneQualifier(cell);
258                 String column = Bytes.toString(columnBytes);
259                 //获取value
260                 String value = Bytes.toString(CellUtil.cloneValue(cell));
261 
262                 System.out.println(rowKey + "-" + column + ":" + value);
263             }
264 
265         }
266         scanner.close();
267         table.close();
268 
269     }
270 
271     //使用过滤器
272     public static void getRowsByColumn(String tableName, String family, String column, String value) throws IOException {
273 
274         Table table = connection.getTable(TableName.valueOf(tableName));
275 
276         Scan scan = new Scan();
277 
278         SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
279         filter.setFilterIfMissing(true);
280 
281         scan.setFilter(filter);
282         ResultScanner scanner = table.getScanner(scan);
283 
284         for (Result result : scanner) {
285 
286             //获取result数组 然后result.rawCells().iter
287             for (Cell cell : result.rawCells()) {
288 
289                 //获取rowkey
290                 byte[] rowBytes = CellUtil.cloneRow(cell);
291                 String rowKey = Bytes.toString(rowBytes);
292                 //获取列
293                 byte[] columnBytes = CellUtil.cloneQualifier(cell);
294                 String columnstr = Bytes.toString(columnBytes);
295                 //获取value
296                 String valuestr = Bytes.toString(CellUtil.cloneValue(cell));
297 
298                 System.out.println(rowKey + "-" + columnstr + ":" + valuestr);
299             }
300 
301         }
302 
303         scanner.close();
304         table.close();
305     }
306 
307     public static void main(String[] args) throws IOException {
308 
309 //            使用API创建company表,包含info和data两个列族
310        // createTable("score", "sname", "course");
311 //            使用API向company表中添加数据
312       //  putCell("score", "95001", "sname", "name", "Mary");
313       //  putCell("score", "95001", "course", "math", "88");
314        // putCell("score", "95001", "course", "english", "81");
315 //            使用API删除Score表中指定列数据,其行键为95001,列族为course,列为Math;
316      //  deleteCell("score", "95001", "course", "math");
317 //            使用API查询Score表中,行键为95001,列族为course,列为Math的值;
318     //    getRow("score", "95001");
319 //            使用API删除Score表,并展示删除后效果
320         dropTable("score");
321     }
322 }