工作中遇到,用 Java 代码操作 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.Bytes;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import javax.xml.soap.SOAPConnectionFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class HBASEFilterTest {
public static Configuration configuration;
public static HConnection hConnection;
/**
* 配置 hbase 信息,连接 hbase (最好配置成单例模式,在程序启动的时候,进行 hbase 连接)
*/
public void confirmHbase() {
System.out.println("配置 hbase 的参数信息");
try {
//这边的信息只是在 Windows 调试hbase 程序的时候使用,如果在 Linux 下调试,就不用这条语句了
System.setProperty("hadoop.home.dir", "D:\\hadooputil\\hadoop-common-2.2.0-bin");
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
// configuration.set("hbase.zookeeper.quorum", "10.100.3.168");
configuration.set("hbase.zookeeper.quorum", "10.100.2.159");
hConnection = HConnectionManager.createConnection(configuration);
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭 hbase 连接
*/
public void closeHbaseConnect() {
try {
hConnection.close();
System.out.println("close");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建 hbase 表
* @param tableName 表名
* @param familyList 列族列表
* @throws Exception
*/
public void createTable(String tableName,String []familyList) throws Exception {
// 配置 Hbase 连接
confirmHbase();
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {
//如果需要删除表了,就要放开注释
// hBaseAdmin.disableTable(tableName);
// hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " 表已经存在");
}
HTableDescriptor hDescriptor = new HTableDescriptor(tableName);
for(String family : familyList){
if(!Strings.isBlank(family)){
hDescriptor.addFamily(new HColumnDescriptor(family));
}
}
hBaseAdmin.createTable(hDescriptor);
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
* 向表中插入数据
* @param tableName 表名
* @param rowKeyName 行名
* @param valueList 格式: 数据信息:列名:value(匹配的值) 例如:cf1:name:zhang
* @throws Exception
*/
public void insertIntoTable(String tableName,String rowKeyName,String []valueList) throws Exception {
// 配置数据库连接
confirmHbase();
HTable table = (HTable) hConnection.getTable(tableName);
table.setAutoFlush(false);// 关闭自动刷新,提交IO吞吐率
List
//计数器
int count = 0;
for(String value : valueList){
Put put = new Put(Bytes.toBytes(rowKeyName));
String []strArr = value.split(":");
if(Strings.isBlank(strArr[1])){
put.add(Bytes.toBytes(strArr[0]), null, Bytes.toBytes(strArr[2]));
}else{
put.add(Bytes.toBytes(strArr[0]), Bytes.toBytes(strArr[1]), Bytes.toBytes(strArr[2]));
}
count++;
if (count % 1000 == 0) {
count = 0;
table.put(list);
list = new ArrayList
}
}
table.close();
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
* 通过hbase 表名 和 rowKey 获取表中的数据
* @param tableName 表名
* @param rowKey
* @throws Exception
* @author leilei.zhang
*/
public void getValueByRowKey(String tableName,String rowKey) throws Exception {
// 配置 Hbase 连接
confirmHbase();
HTable table = (HTable) hConnection.getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
if (result != null && result.size() > 0) {
System.out.print("key=" + new String(result.getRow()) + " ");
for (KeyValue key : result.raw()) {
System.out.println(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
} else {
System.out.println(" ---- 没有数据 ----");
}
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
* 单个过滤器的使用方法
* @param tableName 表名
* @param family 列簇名
* @param colum 列名
* @param value 过滤的值
* @throws Exception
*/
public void getValueOnlyOneFilter(String tableName,String family,String colum,String value) throws Exception {
// 配置数据库连接
confirmHbase();
System.out.println("通过单过滤器,获取当前的数据");
HTable table = (HTable) hConnection.getTable(tableName);
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(colum), CompareOp.EQUAL, Bytes.toBytes(value));
scan.setFilter(filter);
System.out.println("获取到的数据");
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获取到的数据是:");
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
System.out.println("关闭数据库连接");
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
*
* @Description: 对于列,使用多个过滤器
*/
// public void testScanFilterList() throws Exception {
//
// // 配置数据库连接
// confirmHbase();
//
// HTable table = (HTable) hConnection.getTable("userinfozll");
// Scan scan = new Scan();
// scan.setCaching(1000);// 设置扫描器缓存区的大小
//
// List
// Filter filter = new SingleColumnValueFilter(Bytes.toBytes("cf1"), Bytes.toBytes("lname"), CompareOp.EQUAL, Bytes.toBytes("zhang"));
// Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf2"), Bytes.toBytes("age"), CompareOp.EQUAL, Bytes.toBytes("18"));
//
// filterList.add(filter);
// filterList.add(filter2);
//
// FilterList fList = new FilterList(filterList);
// scan.setFilter(fList);
//
// ResultScanner rs = table.getScanner(scan);
// for (Result r : rs) {
// System.out.print("key ********* =" + new String(r.getRow()) + " ");
// for (KeyValue key : r.raw()) {
// System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
// }
//
// System.out.println();
// }
// // 关闭 hbase 连接
// closeHbaseConnect();
// }
/**
* 多值过滤器的使用
* @param tableName
* @param colnumInfo 格式: 列族名称:列名:value(匹配的值) 例如:cf1:name:zhang
* @throws Exception
*/
public void getValueByMulFilter(String tableName,String []colnumInfo) throws Exception {
// 配置数据库连接
confirmHbase();
HTable table = (HTable) hConnection.getTable(tableName);
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
List
//列族名称:列名:value(匹配的值)
for(String str : colnumInfo){
String [] strArr = str.split(":");
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(strArr[0]), Bytes.toBytes(strArr[1]), CompareOp.EQUAL, Bytes.toBytes(strArr[2]));
filterList.add(filter);
}
FilterList fList = new FilterList(filterList);
scan.setFilter(fList);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key ********* =" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
* 通过前置过滤器获取表中数据
* @param tableName
* @param preValue
* @throws Exception
*/
public void getValueByPrefixFilter(String tableName,String preValue) throws Exception{
// 配置数据库连接
confirmHbase();
HTable table = (HTable) hConnection.getTable(tableName);
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
Filter filter = new PrefixFilter(Bytes.toBytes(preValue));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
// 关闭 hbase 连接
closeHbaseConnect();
}
/**
* 时间戳过滤器(TimestampsFilter)
* @param tableName 表名
* @param timestapList 时间戳数组
*/
public void getValueTimestampsFilter(String tableName,Long []timestapList) throws Exception{
HTable table = (HTable) hConnection.getTable(tableName);
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
List
for(Long time : timestapList){
ts.add(time);
}
Filter filter = new TimestampsFilter(ts);
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
}
}
}