1、 引入pom文件
org.apache.hbase
hbase-client
2.1.3
2、 工具类
package com.rf.athena.rfathenadatasource.utils;
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.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HbaseUtil {
// 行键前缀固定长度,不足的用0补齐(后补)
private static final int ROW_KEY_PRE_LENGTH = 40;
// 行键参数固定长度,不足用0补齐(前补)
private static final int ROW_KEY_OPTIONS_LENGTH = 100;
// 行键前缀固定长度,不足的用0补齐(前补)
private static final int ROW_KEY_SUF_LENGTH = 40;
@Value("${hbase.zookeeper.quorum:h1,h2,h3}")
static String hbase_zookeeper_quorum;
@Value("${hbase_zookeeper_property_clientPort:2181}")
static String hbase_zookeeper_property_clientPort;
static Configuration conf;
static Connection conn = null;
static Admin admin = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "h1,h2,h3");
conf.set("hbase.zookeeper.property.clientPort", "2181");
try {
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建数据表
* @param tableName
* @param columnFamilies
* @throws IOException
*/
public static void createTable(String tableName,String ...columnFamilies) throws IOException {
TableName name = TableName.valueOf(tableName);
if (!admin.isTableAvailable(name)) {
//表描述器构造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(name);
List columnFamilyDescriptorList = new ArrayList<>();
for (String columnFamily : columnFamilies) {
//列族描述起构造器
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
//获得列描述
ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
columnFamilyDescriptorList.add(columnFamilyDescriptor);
}
// 设置列簇
tableDescriptorBuilder.setColumnFamilies(columnFamilyDescriptorList);
//获得表描述器
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
//创建表
admin.createTable(tableDescriptor);
}
}
/**
* 禁用表
* @param tableName
* @throws IOException
*/
public static void disableTable(String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
if (!admin.isTableDisabled(name)) {
admin.disableTable(name);
}
}
/**
* 清空表
* @param tableName
*/
public static void truncate(String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
disableTable(tableName);
admin.truncateTable(name,true);
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
if (admin.isTableDisabled(name)) {
admin.deleteTable(name);
}
}
/**
* 获取数据表列表
* @return
* @throws IOException
*/
public static List listTables() throws IOException {
List tableDescriptors = admin.listTableDescriptors();
return tableDescriptors;
}
/**
* 获取行键
* @param prefixString
* @param suffixString
* @param options
* @return
*/
public static String getRowKey(String prefixString,String suffixString,Object ...options) {
if (prefixString.length() >ROW_KEY_PRE_LENGTH || suffixString.length() > ROW_KEY_SUF_LENGTH) {
return null;
}
StringBuilder preStringBuilder = new StringBuilder();
preStringBuilder.append(prefixString);
for (int i = 0; i < (ROW_KEY_PRE_LENGTH - preStringBuilder.length()); i++) {
preStringBuilder.append("0");
}
StringBuilder sufStringBuilder = new StringBuilder();
sufStringBuilder.append(suffixString);
for (int i = 0; i < (ROW_KEY_SUF_LENGTH - sufStringBuilder.length()); i++) {
sufStringBuilder.append("0");
}
StringBuilder optBuilder = new StringBuilder();
for (Object option : options) {
optBuilder.append(option);
}
if (optBuilder.length() > ROW_KEY_OPTIONS_LENGTH) {
return null;
}
StringBuilder optStringBuilder = new StringBuilder();
for (int i = 0; i < (ROW_KEY_OPTIONS_LENGTH - optBuilder.length()); i++) {
optStringBuilder.append("0");
}
optStringBuilder.append(optBuilder);
return preStringBuilder.append("|").append(optStringBuilder).append("|").append(sufStringBuilder).toString();
}
/**
* 插入一条记录
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void insertOne(String tableName,String rowKey,String columnFamily,String column,String value) throws IOException {
Put put = new Put(Bytes.toBytes(rowKey));
//下面三个分别为,列族,列名,列值
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
//得到 table
Table table = conn.getTable(TableName.valueOf(tableName));
//执行插入
table.put(put);
}
/**
* 多行多列多值-单列簇
* @param tableName
* @param columnFamily
* @param mapList
* @throws IOException
*/
public static void insertAll(String tableName, String columnFamily, List
以上的工具类仅供参考