天天看點

釋出微網誌 | 學習筆記

開發者學堂課程【分布式資料庫 HBase快速入門:釋出微網誌】學習筆記,與課程緊密聯系,讓使用者快速學習知識。

課程位址:

https://developer.aliyun.com/learning/course/101/detail/1772

釋出微網誌

内容介紹

一、釋出微網誌

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() ;

}

//關注使用者

//取關使用者

//擷取微網誌内容(初始化頁面)

//擷取微網誌内容(檢視某個人所有微網誌内容)

}

繼續閱讀