Hbase Java API
package com.hbase.api;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class HbaseClient {
private static Connection connection = null;
private static Admin admin = null;
private static Logger logger = LoggerFactory.getLogger(HbaseClient.class);
static {
try {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node1:2181,node2:2181,node3:2181");
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
logger.info("获取hbase连接失败!", e.getMessage());
}
}
@SuppressWarnings("all")
public static boolean isTableExist(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
}
@SuppressWarnings("all")
public static void createTable(String tableName, String... columFamilys) throws IOException {
if (columFamilys.length <= 0) {
logger.info("创建表时至少需要一个列族");
return;
}
if (isTableExist(tableName)) {
logger.info("表已存在!");
return;
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String columFamily : columFamilys) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columFamily);
tableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(tableDescriptor);
}
public static void dropTable(String tableName) throws IOException {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}
public static void createNameSpace(String spaceName) throws IOException {
NamespaceDescriptor descriptor = NamespaceDescriptor.create(spaceName).build();
admin.createNamespace(descriptor);
}
@SuppressWarnings("all")
public static void putData(String tableName) throws Exception {
Table tb = null;
try {
tb = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes("0001"));
put.addColumn("f1".getBytes(), "id".getBytes(), "1".getBytes(StandardCharsets.UTF_8));
put.addColumn("f1".getBytes(), "name".getBytes(), "Tony".getBytes(StandardCharsets.UTF_8));
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("20"));
put.addColumn("f2".getBytes(), "address".getBytes(), "Japan".getBytes(StandardCharsets.UTF_8));
put.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15888888888"));
tb.put(put);
} catch (IOException e) {
e.printStackTrace();
logger.info("插入数据失败 {}" + tableName);
} finally {
if (tb != null) {
tb.close();
}
}
}
@SuppressWarnings("all")
public static void batchPut(String tableName) throws Exception {
Table myuserTb = null;
try {
myuserTb = connection.getTable(TableName.valueOf(tableName));
Put put = new Put("0002".getBytes());
put.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("2"));
put.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("30"));
put.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("沛国谯县"));
put.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("3"));
put2.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("刘备"));
put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("32"));
put2.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("4"));
put3.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("孙权"));
put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("35"));
put3.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("5"));
put4.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("诸葛亮"));
put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("28"));
put4.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0006".getBytes());
put5.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("6"));
put5.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("司马懿"));
put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("27"));
put5.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0007".getBytes());
put6.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes("7"));
put6.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes("28"));
put6.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("内蒙人"));
put6.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("貂蝉去哪了"));
ArrayList<Put> listPut = new ArrayList<>();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuserTb.put(listPut);
} catch (IOException e) {
e.printStackTrace();
logger.info("批量插入数据失败 {}" + tableName);
} finally {
if (myuserTb != null) {
myuserTb.close();
}
}
}
@SuppressWarnings("all")
public static void getDataByRowKey(String tableName, String rowKey) {
Table MyuserTable = null;
try {
MyuserTable = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
Result result = MyuserTable.get(get);
Cell[] cells = result.rawCells();
if (cells.length < 1) {
System.out.println("未查询到数据");
}
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
} catch (IOException e) {
e.printStackTrace();
logger.info("查询数据异常!", e.getMessage());
} finally {
if (MyuserTable != null) {
try {
MyuserTable.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("释放表异常");
}
}
}
}
@SuppressWarnings("all")
public static void getColumnFamily(String tableName, String rowKey, String columFamily, String qualifilter) {
Table myuserTable = null;
try {
myuserTable = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(columFamily.getBytes(), qualifilter.getBytes());
Result result = myuserTable.get(get);
Cell[] cells = result.rawCells();
if (cells.length < 1) {
System.out.println("没有查询导数据");
logger.info("表 {},行键 {},列族 {},列 {}" + tableName, rowKey, columFamily, qualifilter);
}
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
} catch (IOException e) {
e.printStackTrace();
logger.info("获取数据失败");
} finally {
if (myuserTable != null) {
try {
myuserTable.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("关闭表失败");
}
}
}
}
@SuppressWarnings("all")
public static void getRowKeyRange(String tableName, String startRowKey, String endRowKey) {
Table myuserTable = null;
ResultScanner scannerResult = null;
try {
myuserTable = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scannerResult = myuserTable.getScanner(scan);
Iterator<Result> iterator = scannerResult.iterator();
while (iterator.hasNext()) {
Result result = iterator.next();
if (result == null) {
System.out.println("扫描失败");
}
Cell[] cells = result.rawCells();
if (cells.length < 1) {
System.out.println("未查询到数据");
}
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
logger.info("扫描范围失败!");
} finally {
if (myuserTable != null) {
try {
myuserTable.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("关闭表失败!");
}
}
if (scannerResult != null) {
scannerResult.close();
}
}
}
public static void getScan(String tableName){
Table myuserTable = null;
Scan scan = null;
try {
myuserTable = connection.getTable(TableName.valueOf(tableName));
scan = new Scan();
ResultScanner resultScanner = myuserTable.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
logger.info("扫描失败!");
} finally {
if (myuserTable != null){
try {
myuserTable.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("关表失败!");
}
}
}
}
public static void deleteRowKey(String tableName,String rowKey){
Table myuserTable = null;
try {
myuserTable = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
myuserTable.delete(delete);
} catch (IOException e) {
e.printStackTrace();
logger.info("删除失败!");
}finally {
if (myuserTable != null){
try {
myuserTable.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("关表失败!");
}
}
}
}
public static void rowKeyFilter(String tableName,String rowKey){
Table myUserName = null;
ResultScanner scanner = null;
try {
myUserName = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes(rowKey)));
scan.setFilter(rowFilter);
scanner = myUserName.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
logger.info("扫描失败");
} finally {
if (myUserName != null){
try {
myUserName.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null){
scanner.close();
}
}
}
public static void familyFilter(String tableName,String family){
Table table = null;
ResultScanner scanner = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator(family));
scan.setFilter(familyFilter);
scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null ){
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null) {
scanner.close();
}
}
}
public static void qualifierFilter(String tableName,String qualifier){
Table table = null;
ResultScanner scanner = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(qualifier));
scan.setFilter(qualifierFilter);
scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
logger.info("过滤失败!");
} finally {
if (table != null){
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null){
scanner.close();
}
}
}
public static void singleColumnFilter(String tableName){
Table table = null;
ResultScanner scanner = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
scan.setFilter(singleColumnValueFilter);
scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
logger.info("过滤失败!");
} finally {
if (table != null){
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null){
scanner.close();
}
}
}
public static void rowKeyPrefixFilter(String tableName,String rowKeyPrefix){
Table table =null;
ResultScanner scanner = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
PrefixFilter prefixFilter = new PrefixFilter(rowKeyPrefix.getBytes());
scan.setFilter(prefixFilter);
scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowKey: " + 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)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null){
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null){
scanner.close();
}
}
}
public static void main(String[] args) throws Exception {
boolean myuser = isTableExist("myuser");
System.out.println(myuser);
HbaseClient.rowKeyPrefixFilter("myuser", "00");
}
}