封装一个Hbase工具类
public class HbaseUtils {
public static void showData(Result result){
while (result.advance()){
Cell cell = result.current();
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row+"->"+family+":"+qualifier+"->"+value);
}
}
public static Admin getAdmin() throws IOException {
Connection conn = getHbaseConnection();
Admin admin = conn.getAdmin();
return admin;
}
public static Table getTable(String name) throws IOException {
Connection conn = getHbaseConnection();
Table table = conn.getTable(TableName.valueOf(name));
return table;
}
public static Connection getHbaseConnection() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
Connection conn = ConnectionFactory.createConnection(conf);
return conn;
}
}
插入一个属性值
private static void putOneRowOneCell() throws IOException {
Table tbUser = HbaseUtils.getTable("tb_user");
Put put = new Put("0002".getBytes());
put.addColumn("cf2".getBytes(),
"age".getBytes(),
"19".getBytes());
tbUser.put(put);
tbUser.close();
}
插入一行多个属性值
private static void putOneRowSomeCell() throws IOException {
Table tbUser = HbaseUtils.getTable("user");
Put put = new Put("0001".getBytes());
put.addColumn("info".getBytes(), "name".getBytes(), "zss".getBytes());
put.addColumn("info".getBytes(), "age".getBytes(), "18".getBytes());
put.addColumn("extra".getBytes(), "job".getBytes(), "coder".getBytes());
tbUser.put(put);
tbUser.close();
}
插入多行
private static void putSomeRows() throws IOException {
Table user = HbaseUtils.getTable("user");
Put put1 = new Put(Bytes.toBytes("0002"));
Put put2 = new Put(Bytes.toBytes("0003"));
Put put3 = new Put(Bytes.toBytes("0004"));
List<Put> list = new ArrayList<>();
list.add(put1);
list.add(put2);
list.add(put3);
user.put(list);
user.close();
}
批次插入
private static void mutationInsert() throws IOException {
Connection conn = HbaseUtils.getHbaseConnection();
BufferedMutator mutator = conn.getBufferedMutator(TableName.valueOf("user"));
Put put = new Put(Bytes.toBytes("0002"));
put.addColumn(Bytes.toBytes("info"),
Bytes.toBytes("gender"),
Bytes.toBytes("M"));
mutator.mutate(put);
mutator.flush();
mutator.close();
conn.close();
}
创建名称空间
private static void nameSpace() throws IOException {
Admin admin = HbaseUtils.getAdmin();
NamespaceDescriptor.Builder nsb = NamespaceDescriptor.create("doit");
NamespaceDescriptor ns = nsb.build();
admin.createNamespace(ns);
admin.close();
}
scan全表
private static void scanAllTable(Table table) throws IOException {
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
Iterator<Result> it = results.iterator();
while (it.hasNext()){
Result result = it.next();
HbaseUtils.showData(result);
}
}
扫描行数据
private static void scanWithRow(Table table) throws IOException {
Scan scan = new Scan();
scan.withStartRow("uid002".getBytes());
scan.withStopRow("uid006".getBytes());
ResultScanner results = table.getScanner(scan);
Iterator<Result> it = results.iterator();
while (it.hasNext()){
Result result = it.next();
HbaseUtils.showData(result);
}
}
扫描一个列族的数据
private static void scanFamily(Table table) throws IOException {
ResultScanner scanner = table.getScanner("info".getBytes());
Iterator<Result> it = scanner.iterator();
while (it.hasNext()){
Result result = it.next();
HbaseUtils.showData(result);
}
}
获取某个属性值
private static void getCell(Table table) throws IOException {
Get get = new Get("0001".getBytes());
get.addColumn("info".getBytes(),"name".getBytes());
Result result = table.get(get);
HbaseUtils.showData(result);
}
删除某个属性值
private static void deleteCell(Table table) throws IOException {
Delete delete = new Delete("0001".getBytes());
delete.addColumn("extra".getBytes(),"job".getBytes());
table.delete(delete);
}
自增
private static void incr(Table table) throws IOException {
Increment increment = new Increment("0002".getBytes());
increment.addColumn("info".getBytes(),"cnt".getBytes(),1);
table.increment(increment);
long l = table.incrementColumnValue("0002".getBytes(), "info".getBytes(), "cnt".getBytes(), 1);
System.out.println(l);
}
获取一个表的所有region
private static void getTableRegions() throws IOException {
Admin admin = HbaseUtils.getAdmin();
List<RegionInfo> regions = admin.getRegions(TableName.valueOf("doit:user"));
for (RegionInfo region : regions) {
String encodedName = region.getEncodedName();
byte[] startKey = region.getStartKey();
byte[] endKey = region.getEndKey();
System.out.println(encodedName+"--"+Bytes.toString(startKey)+"--"+Bytes.toString(endKey));
admin.close();
}
}
putDetails
private static void putDetails() {
Put put = new Put("rk10001".getBytes());
List<String> cfs = new ArrayList<>() ;
NavigableMap<byte[], List<Cell>> familyCellMap = put.getFamilyCellMap();
Set<Map.Entry<byte[], List<Cell>>> entries = familyCellMap.entrySet();
for (Map.Entry<byte[], List<Cell>> entry : entries) {
byte[] key = entry.getKey();
String cf = Bytes.toString(key);
cfs.add(cf) ;
List<Cell> cells = entry.getValue();
for (Cell cell : cells) {
byte[] bytes = CellUtil.cloneQualifier(cell);
String q = new String(bytes);
if (q.equals("name")) {}
byte[] value = CellUtil.cloneValue(cell);
}
}
}
获取某行数据(get)
private static void getRowData() throws IOException {
Table tb_user = HbaseUtils.getTable("tb_user");
Get get = new Get("0001".getBytes());
Result result = tb_user.get(get);
while (result.advance()){
Cell cell = result.current();
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row+"->"+family+":"+qualifier+"->"+value);
}
tb_user.close();
}