谷粒微博-项目进度--6-23
作者:互联网
/**
* 发布微博
* a、微博内容表中数据+1
* b、向微博收件箱表中加入微博的 Rowkey
*/
public void publishContent(String uid, String content) {
HConnection connection = null;
try {
// 设置zookeeper
conf.set("hbase.zookeeper.quorum", "master");
connection = HConnectionManager.createConnection(conf);
// a、微博内容表中添加 1 条数据,首先获取微博内容表描述
HTableInterface contentTBL = connection.getTable(TableName.valueOf(TABLE_CONTENT));
// 组装 Rowkey
long timestamp = System.currentTimeMillis();
String rowKey = uid + "_" + timestamp;
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes("info"), Bytes.toBytes("content"), timestamp, Bytes.toBytes(content));
contentTBL.put(put);
// b、向微博收件箱表中加入发布的 Rowkey
// b.1、查询用户关系表,得到当前用户有哪些粉丝
HTableInterface relationsTBL = connection.getTable(TableName.valueOf(TABLE_RELATIONS));
// b.2、取出目标数据
Get get = new Get(Bytes.toBytes(uid));
get.addFamily(Bytes.toBytes("fans"));
Result result = relationsTBL.get(get);
List<byte[]> fans = new ArrayList<byte[]>();
// 遍历取出当前发布微博的用户的所有粉丝数据
for (Cell cell : result.rawCells()) {
fans.add(CellUtil.cloneQualifier(cell));
}
// 如果该用户没有粉丝,则直接 return
if (fans.size() <= 0)
return;
// 开始操作收件箱表
HTableInterface recTBL = connection.getTable(TableName.valueOf(TABLE_RECEIVE_CONTENT_EMAIL));
List<Put> puts = new ArrayList<Put>();
for (byte[] fan : fans) {
Put fanPut = new Put(fan);
fanPut.add(Bytes.toBytes("info"), Bytes.toBytes(uid), timestamp, Bytes.toBytes(rowKey));
puts.add(fanPut);
}
recTBL.put(puts);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != connection) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
标签:23,--,Bytes,connection,new,微博,toBytes,fans 来源: https://www.cnblogs.com/gagachen/p/13371951.html