开发者学堂课程【分布式数据库 HBase 快速入门:关注用户(二)】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:
https://developer.aliyun.com/learning/course/101/detail/1774关注用户(二)
内容介绍
一、关注用户(二)
关注用户:
package com. atguigu.weibo;
public class WeiBoUtil {
private static Configuration configuration = HBaseConfiguration.create() ;
static {
configuration.set("hbase.zookeeper. quorum",, "192.168.9.102");
}
//创建命名空间
public static void createNamespace(String ns){
//创建连接
ConnectionFactory.createConnection(configuration) ;
Admin admin = connection.getAdmin() ;
//创建NS描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns)
build();
//创建操作
admin.createNamespace (namespaceDescriptor);
//关闭资源admin.close() ;
connection.close() ;
}
//创建表
public static void createTable(String tableName, String... cfs){
//创建连接
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin() ;
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName. valueOf(tableName));
//循环添加列族
for (String cf : cfs){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf) ;
hColumnDescriptor.setMaxVersions(versions);
hTableDescriptor.addFamily (hColumnDescriptor);
admin.createTable(hTableDescriptor);
//关闭资源
admin.close() ;
connection.close() :
}
/**
*1.更新微博内容表数据
*2.更新收件箱表数据
* --获取当前操作人的fans
* --去往收件箱表依次更新数据
*@param uid
* @paramcontent
*@throws IOException
*/
//发布微博
public static void createData(String uid,String content){
Connection connection = ConnectionFactory.createConnection(configuration);
Table contTable = connection.getTable(TableName. value0f(Constant.CONTENT));
Table relaTable = connection.getTable(TableName. valueOf(Constant.RELATIONS)) ;
Table inboxTable = connection.getTable(TableName. valueOf(Constant.INBOX));
//拼接RK
long ts = System. currentTimeMillis() ;
String rowKey = uid + "_" + ts;
//生产put对象
Put put = new Put(Bytes.toBytes(rowKey)) ;
put. addColumn(Bytes.toBytes( s:"info"),Bytes.toBytes( s:"content"),Bytes.toBytes(content)) ;
//往内容表添加数据
contTable.put(put);
//获取关系表中的fans
Get get = new Get(Bytes.toBytes(uid)) ;
get.addFamily(Bytes.toBytes( s: "fans") ) ;
Result result = relaTable.get(get);
Ce11[] cells = result.rawCells() ;
if (cells.length <= 0){
return;
}
//更新fans收件箱表
List puts = new ArrayList<>();
for (Cell cell : cells) {
byte[] cloneQualifier = Cellutil.cloneQualifier(cell);
Put inboxPut = new Put(cloneQualifier);
inboxPut.addColumn(Bytes.toBytes(s:"info"),Bytes.toBytes(uid) ,Bytes.toBytes())
puts.add(inboxPut) ;
//关闭资源admin.close() ;
connection.close() ;
}
/**
*1.在用户关系表
*—-添加操作人的attends
*--添加被操作人的fans
* 2.在收件箱中
*--在微博内容中获取被关注者的3条数据(rowkey)
*--在收件箱表中添加操作人的关注者信息
*@param uid
*@param uids
*/
//关注用户
public static void addAttend(String uid,String.. . uids) throws IOException {
//获取连接
Connection connection = ConnectionFactory.createConnection(configuration);
//获取三张操作的表对象
Table contTable = connection.getTable(TableName.valueOf(Constant.CONTENT));
Table relaTable = connection.getTable(TableName. value0f(Constant.RELATIONS) ;
Table inboxTable = connection.getTable(TableName.value0f(Constant.INBOX) );
Put relaPut = new Put (Bytes.toBytes(uid)) ;
ArrayList puts = new ArrayList<>() ;
for (String s : uids) {
relaPut.addColumn(Bytes.toBytes(s:"attends"),Bytes.toBytes(s),Bytes.toBytes(s))
//创建被关注者的put对象
put fansPut = new Put(Bytes.toBytes(s)) ;
fansPut. addColumn(Bytes.toBytes( s:"fans"),Bytes.toBytes(uid)Bytes.toBytes(uid);
puts.add(fansPut) ;
}
puts.add(relaPut) ;
relaTable.put(puts):
Put inboxPut = new Put(Bytes.toBytes(uid)) ;
//获取内容表中被关注者的rowkey
for (String s : uids){
Scan scan = new Scan(Bytes.toBytes(s),Bytes.toBytes( s:s +"|"));
ResultScanner results = contTable.getScanner(scan);
for (Result result : results){
byte[] row = result.getRow () ;
inboxPut.addColumn(Bytes.toBytes( s: "info"),Bytes.toBytes(s),row) ;
}
}
inboxTable.put(inboxPut) ;
inboxTable.close() ;
relaTable.close() ;
contTable.close() ;
connection.close() ;
}
//取关用户
//获取微博内容(初始化页面)
//获取微博内容(查看某个人所有微博内容)
}