hbase-api
Posted 丶落幕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase-api相关的知识,希望对你有一定的参考价值。
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操作
封装一个util
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 Connection getConnection()
return threadLocal.get();
//创建表
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 createTable(String tableName,int version,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)
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);
hColumnDescriptor.setMaxVersions(version);
hTableDescriptor.addFamily(hColumnDescriptor);
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 实战之谷粒微博
微博内容表
键 | 值 |
---|---|
rowkey | 当前用户id + “_” + ts(时间戳) |
family | info |
column | content |
value | “微博内容” |
关系表
键 | 值 |
---|---|
rowkey | 当前用户id |
family | attends(关注的人) , fans(粉丝) |
column | 关注人的id,fens的id |
value | 关注人的id,fens的id |
收件箱表
键 | 值 |
---|---|
rowkey | 当前用户id |
family | info |
column | 关注人的uid(消息推送) |
value | 微博内容表的rowkey |
1 需求分析
- 微博内容的浏览,数据库表设计
- 用户社交体现:关注用户,取关用户
- 拉取关注的人的微博内容
2 代码设计总览:
- 创建命名空间以及表名的定义
- 创建微博内容表
- 创建用户关系表
- 创建用户微博内容接收邮件表
- 发布微博内容
- 添加关注用户
- 移除(取关)用户
- 获取关注的人的微博内容
- 测试
3 代码实现
定义常量
public class Constants
public static final Configuration CONFIGURATION= HBaseConfiguration.create();
//命名空间
public static final String NAMESPACE="blog";
//微博内容表
public static final String CONTENT_TABLE="blog:content";
public static final String CONTENT_TABLE_CF="info";
public static final int CONTENT_TABLE_VERSION=1;
//用户关系表
public static final String RELATION_TABLE="blog:relation";
public static final String RELATION_TABLE_CF1以上是关于hbase-api的主要内容,如果未能解决你的问题,请参考以下文章