HBase API操作
阿新 • • 發佈:2020-12-22
HBase API操作
1、環境準備
新建專案後在pom.xml中新增依賴:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>
2、HBase API
1)獲取Configuration物件
public static Configuration conf;
static{
//使用HBaseConfiguration的單例方法例項化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.9.102");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
2)判斷表是否存在
public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ //在HBase中管理、訪問表需要先建立HBaseAdmin物件 //Connection connection = ConnectionFactory.createConnection(conf); //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); HBaseAdmin admin = new HBaseAdmin(conf); return admin.tableExists(tableName); }
3)建立表
public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(conf); //判斷表是否存在 if(isTableExist(tableName)){ System.out.println("表" + tableName + "已存在"); //System.exit(0); }else{ //建立表屬性物件,表名需要轉位元組 HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); //建立多個列族 for(String cf : columnFamily){ descriptor.addFamily(new HColumnDescriptor(cf)); } //根據對錶的配置,建立表 admin.createTable(descriptor); System.out.println("表" + tableName + "建立成功!"); } }
4)刪除表
public static void dropTable(String tableName) throws MasterNotRunningException,
ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(isTableExist(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表" + tableName + "刪除成功!");
}else{
System.out.println("表" + tableName + "不存在!");
}
}
5)向表中插入資料向表中插入資料
public static void addRowData(String tableName, String rowKey, String columnFamily, String
column, String value) throws IOException{
//建立HTable物件
HTable hTable = new HTable(conf, tableName);
//向表中插入資料
Put put = new Put(Bytes.toBytes(rowKey));
//向Put物件中組裝資料
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
hTable.put(put);
hTable.close();
System.out.println("插入資料成功");
}
6)刪除多行資料
public static void deleteMultiRow(String tableName, String... rows) throws IOException{
HTable hTable = new HTable(conf, tableName);
List<Delete> deleteList = new ArrayList<Delete>();
for(String row : rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
hTable.delete(deleteList);
hTable.close();
}
7)獲取所有資料
public static void getAllRows(String tableName) throws IOException{
HTable hTable = new HTable(conf, tableName);
//得到用於掃描region的物件
Scan scan = new Scan();
//使用HTable得到resultcanner實現類的物件
ResultScanner resultScanner = hTable.getScanner(scan);
for(Result result : resultScanner){
Cell[] cells = result.rawCells();
for(Cell cell : cells){
//得到rowkey
System.out.println("行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
8)獲取某一行資料
public static void getRow(String tableName, String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions();顯示所有版本
//get.setTimeStamp();顯示指定時間戳的版本
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println("行鍵:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("時間戳:" + cell.getTimestamp());
}
}
9)獲取某一行指定“列族:列”的資料
public static void getRowQualifier(String tableName, String rowKey, String family, String
qualifier) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println("行鍵:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
10) 過濾器-行鍵過濾器掃描
Scan scan = new Scan();
Filter filter = new RowFilter(
CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("^\\w*99$"));
scan.setFilter(filter);
11) 過濾器-列族過濾器
Filter filter = new FamilyFilter(
CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("info2"))
);
scan.setFilter(filter);
12) 過濾器-列標識名過濾器
Filter filter = new QualifierFilter(
CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("^\\w*m\\w*$")
);
scan.setFilter(filter);
13) 過濾器-值過濾器
Filter filter = new ValueFilter(
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("100")
);
scan.setFilter(filter);
14) 過濾器-單列值過濾器,按指定列的值進行過濾
Filter filter = new SingleColumnValueFilter(
Bytes.toBytes("info1"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("88"));
scan.setFilter(filter);
15) 過濾器-過濾組合條件
//過濾出info1.name中含有8並且info.sno含有6,或者info1.name中含有6並且info2.name含有8
Filter filter1 = new SingleColumnValueFilter(
Bytes.toBytes("info1"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("8"));
Filter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("info1"),
Bytes.toBytes("sno"),
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("6"));
FilterList list1 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
list1.addFilter(filter1);
list1.addFilter(filter2);
Filter filter3 = new SingleColumnValueFilter(
Bytes.toBytes("info1"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("6"));
Filter filter4 = new SingleColumnValueFilter(
Bytes.toBytes("info2"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("8"));
// 並且
FilterList list2 = new FilterList(FilterList.Operator.MUST_PASS_ALL);
list2.addFilter(filter3);
list2.addFilter(filter4);
// 或者
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
list.addFilter(list1);
list.addFilter(list2);
scan.setFilter(list);
```java