package com.hessianhealth.common.core.HBase;
import java.io.IOException;
import java.util.ArrayList;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* Created by liangguannan on 2020/10/27.
*/
public class HbseApiDemo11 {
public static Configuration conf;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.8.144:2181");
}
/**
* 判断是表是否存在
*
* @param tableName
* @return
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static boolean isTableExist(String tableName)
throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
// 在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
/**
* create table
*
* @param tableName
* @param columnFamily
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static void createTable(String tableName, String... columnFamily) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 判断表是否存在
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在");
// System.exit(0);
} else {
// 创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 创建多个列族
for (String cf : columnFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hColumnDescriptor.setMinVersions(1);
hColumnDescriptor.setMaxVersions(1);
descriptor.addFamily(hColumnDescriptor);
}
// 根据对表的配置,创建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "创建成功!");
}
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void dropTable(String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 判断表是否存在
if (isTableExist(tableName)) {
// delete this table
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表" + tableName + "删除成功!");
} else {
System.out.println("表" + tableName + "不存在!");
}
}
/**
* 删除多行数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteMutilRows(String tableName ,String... rowkeys )throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
ArrayList<Delete> deletes = new ArrayList<Delete>();
for (String rowkey : rowkeys)
{
deletes.add(new Delete(Bytes.toBytes(rowkey)));
}
table.delete(deletes);
}
/**
* 删除指定列族的数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteColumnFamily(String tableName ,String rowkey ,String columnFamilys,String column)throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(column));
table.delete(delete);
}
/**
* 向表中插入数据
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void addRowData(String tableName,String rowkey,String columnFamily ,String colName,String value) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put=new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(colName), Bytes.toBytes(value));
table.put(put);
}
/**
* 得到所有的数据
* @param tableName
* @throws IOException
*/
public static void getAllRows(String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan=new Scan();
ResultScanner scanResult = table.getScanner(scan);
for (Result result : scanResult) {
// byte[] row = result.getRow();
Cell[] rawCell = result.rawCells();
for (Cell cell : rawCell)
{
System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
}
}
}
/**
* 得到所有的数据
* @param tableName
* @throws IOException
*/
public static void getByRowkeys(String tableName,String rowkey) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Get get=new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
Cell[] rawCell = result.rawCells();
for (Cell cell : rawCell)
{
System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
}
}
/**
* test filter
* @param args
* @throws IOException
* 比较器 ByteArrayComparable
通过比较器可以实现多样化目标匹配效果,比较器有以下子类可以使用:
BinaryComparator 匹配完整字节数组
BinaryPrefixComparator 匹配字节数组前缀
BitComparator
NullComparator
RegexStringComparator 正则表达式匹配
SubstringComparator 子串匹配
*/
public static ResultScanner getDataFamilyFilter(String tableName,String family) throws IOException{
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes(family))); //表中不存在account列族,过滤结果为空
// new BinaryPrefixComparator(value) //匹配字节数组前缀
// new RegexStringComparator(expr) // 正则表达式匹配
// new SubstringComparator(substr)// 子字符串匹配
Scan scan = new Scan();
// 通过scan.addFamily(family) 也可以实现此操作
scan.setFilter(ff);
ResultScanner resultScanner = table.getScanner(scan);
return resultScanner;
}
public static void main(String[] args) throws IOException {
boolean tableExist = isTableExist("student");
System.err.println(tableExist);
System.err.println("############################");
System.err.println("#########create staff1 ###################");
createTable("staff1", "info1","info2");
addRowData("staff1","0101","info1","sea","test");
addRowData("staff1","0102","info2","sea1","test1");
addRowData("staff1","0102","info2","name","test1");
addRowData("staff1","0102","info2","addr","test1");
boolean isExitStaff = isTableExist("staff1");
System.err.println("exit table isExitStaff"+isExitStaff);
System.err.println(isExitStaff);
// dropTable("staff1");
System.err.println("############################");
// deleteMutilRows("student","1002","10023");
// deleteColumnFamily("student","10025","info","sex");
//
// addRowData("student","0101","info","sea","test");
getAllRows("staff1");
}
}