HBase Java常用的API
package com.itheima;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
public Table table;
public Admin admin;
public Connection hBaseConn;
@Before
public void before() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "node1:2181,node2:2181,node3:2181");
hBaseConn = ConnectionFactory.createConnection(conf);
admin = hBaseConn.getAdmin();
String tableName = "WATER_BILL";
table = hBaseConn.getTable(TableName.valueOf(tableName));
}
@After
public void after() throws Exception {
table.close();
admin.close();
hBaseConn.close();
}
@Test
public void test02() throws Exception{
Put put = new Put("4944192".getBytes());
put.addColumn("C1".getBytes(), "name".getBytes(), "登卫红".getBytes());
put.addColumn("C1".getBytes(), "address".getBytes(), "贵州省铜仁市德江县7单元267室".getBytes());
put.addColumn("C2".getBytes(), "sex".getBytes(), "男".getBytes());
table.put(put);
table.close();
hBaseConn.close();
}
@Test
public void test01() throws Exception{
TableName tableName = TableName.valueOf("WATER_BILL");
boolean flag = admin.tableExists(tableName);
if (!flag) {
TableDescriptorBuilder descriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
List<ColumnFamilyDescriptor> families = new ArrayList<>();
families.add(ColumnFamilyDescriptorBuilder.newBuilder("C1".getBytes()).build());
families.add(ColumnFamilyDescriptorBuilder.newBuilder("C2".getBytes()).build());
descriptorBuilder.setColumnFamilies(families);
TableDescriptor desc = descriptorBuilder.build();
admin.createTable(desc);
}
}
@Test
public void test06() throws Exception{
Get get = new Get("4944192".getBytes());
Result result = table.get(get);
List<Cell> cellList = result.listCells();
for (Cell cell : cellList) {
byte[] rowkeyBytes = CellUtil.cloneRow(cell);
byte[] familyBytes = CellUtil.cloneFamily(cell);
byte[] qualifierBytes = CellUtil.cloneQualifier(cell);
byte[] valueBytes = CellUtil.cloneValue(cell);
String rowkey = Bytes.toString(rowkeyBytes);
String family = Bytes.toString(familyBytes);
String qualifier = Bytes.toString(qualifierBytes);
String value1 = Bytes.toString(valueBytes);
System.out.println("rowkey为:"+rowkey +"; family:"+family +"; qualifier:"+qualifier+"; value:"+value1);
}
}
@Test
public void test03() throws Exception{
Delete delete = new Delete("4944192".getBytes());
delete.addFamily("C2".getBytes());
delete.addColumn("C1".getBytes(), "name".getBytes());
table.delete(delete);
}
@Test
public void test04() throws Exception{
String tableName = "WATER_BILL";
if (admin.isTableEnabled(TableName.valueOf(tableName))){
admin.disableTable(TableName.valueOf(tableName));
}
admin.deleteTable(TableName.valueOf(tableName));
}
}