hbase-api
Posted 丶落幕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase-api相关的知识,希望对你有一定的参考价值。
HBase-api
1 依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
2 log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3 配置文件
配置文件中有域名映射的话,Windows也需要配
4 java-api操作
public class HBaseUtil {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
private static HBaseUtil hBaseUtil = new HBaseUtil();
private HBaseUtil() {
}
public static HBaseUtil getInstance() throws IOException {
makeConnection();
return hBaseUtil;
}
//获取连接
private static void makeConnection() throws IOException {
Connection conn = threadLocal.get();
if (conn == null) {
conn = ConnectionFactory.createConnection(HBaseConfiguration.create());
threadLocal.set(conn);
}
}
//创建表
public void createTable(String tableName, String... columnFamilys) throws IOException {
if (columnFamilys.length <= 0) {
throw new RuntimeException("没有设置列族信息~~~");
}
TableName name = TableName.valueOf(tableName);
Admin admin = threadLocal.get().getAdmin();
if (admin.tableExists(name)) {
throw new RuntimeException("表已经存在~~~");
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(name);
for (String columnFamily : columnFamilys) {
hTableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(hTableDescriptor);
admin.close();
}
//删除表
public void deleteTable(String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
Admin admin = threadLocal.get().getAdmin();
if (!admin.tableExists(name)) {
throw new RuntimeException("没有这张表~~~");
}
admin.disableTable(name);
admin.deleteTable(name);
admin.close();
}
//判断表是否存在
public boolean tableExist(String tableName) throws IOException {
Admin admin = threadLocal.get().getAdmin();
boolean b = admin.tableExists(TableName.valueOf(tableName));
admin.close();
return b;
}
//创建命名空间
public void createNameSpace(String nameSpace) throws IOException {
try {
Admin admin = threadLocal.get().getAdmin();
admin.createNamespace(NamespaceDescriptor.create(nameSpace).build());
admin.close();
} catch (NamespaceExistException e) {
throw new RuntimeException("命名空间已存在~~~");
}
}
//插入数据
public void insertData(String tableName, String rowkey, String family, String column, String value) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
table.put(put);
table.close();
}
//获取数据
public void getData(String tableName,String rowkey) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
printData(result);
table.close();
}
//指定获取的列族
public void getData(String tableName,String rowkey,String family) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addFamily(Bytes.toBytes(family));
Result result = table.get(get);
printData(result);
table.close();
}
//指定获取的列
public void getData(String tableName,String rowkey,String family,String column) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
Result result = table.get(get);
printData(result);
table.close();
}
//扫描数据
public void scanTable(String tableName) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
printData(result);
}
table.close();
}
//区间扫描
public void scanTable(String tableName,String startRowKey,String stopRowKey) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Scan scan = new Scan(Bytes.toBytes(startRowKey),Bytes.toBytes(stopRowKey));
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
printData(result);
}
table.close();
}
//删除数据(rowkey级别,相当于命令行deleteall)
public void deleteData(String tableName,String rowKey) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
}
//删除数据(列族级别)
public void deleteData(String tableName,String rowKey,String family) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(family));
table.delete(delete);
table.close();
}
//删除数据(column级别)
//addColumns: 会删除所有的版本,如果传入时间戳参数,则删除小于等于这个时间戳的所有版本
//addColumn: 仅删除最后一个版本,后面的版本会显示出来(当然如果flush了,那就找不到了,除非设置多个版本持久化,生产环境慎用)
public void deleteData(String tableName,String rowKey,String family,String column) throws IOException {
if (!tableExist(tableName)) {
throw new RuntimeException("没有这张表~~~");
}
Table table = threadLocal.get().getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(family), Bytes.toBytes(column));
table.delete(delete);
table.close();
}
//关闭连接
public void close() throws IOException {
Connection conn = threadLocal.get();
if (conn != null) {
conn.close();
threadLocal.remove();
}
}
//打印数据
public void printData(Result result){
for (Cell cell : result.rawCells()) {
System.out.println("***************分隔符***************");
System.out.println("rowkey = "+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("family = "+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column = "+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value = "+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
以上是关于hbase-api的主要内容,如果未能解决你的问题,请参考以下文章