尚硅谷大数据技术之HBase

2018-12-12  本文已影响10人  尚硅谷教育

第8章 Hbase实战之谷粒微博
8.1 需求分析

  1. 微博内容的浏览,数据库表设计
  2. 用户社交体现:关注用户,取关用户
  3. 拉取关注的人的微博内容
    8.2 代码实现
    8.2.1 代码设计总览:
  4. 创建命名空间以及表名的定义
  5. 创建微博内容表
  6. 创建用户关系表
  7. 创建用户微博内容接收邮件表
  8. 发布微博内容
  9. 添加关注用户
  10. 移除(取关)用户
  11. 获取关注的人的微博内容
  12. 测试
    8.2.2 创建命名空间以及表名的定义
    //获取配置conf
    private Configuration conf = HBaseConfiguration.create();

//微博内容表的表名
private static final byte[] TABLE_CONTENT = Bytes.toBytes("weibo:content");
//用户关系表的表名
private static final byte[] TABLE_RELATIONS = Bytes.toBytes("weibo:relations");
//微博收件箱表的表名
private static final byte[] TABLE_RECEIVE_CONTENT_EMAIL = Bytes.toBytes("weibo:receive_content_email");
public void initNamespace(){
HBaseAdmin admin = null;
try {
admin = new HBaseAdmin(conf);
//命名空间类似于关系型数据库中的schema,可以想象成文件夹
NamespaceDescriptor weibo = NamespaceDescriptor
.create("weibo")
.addConfiguration("creator", "Jinji")
.addConfiguration("create_time", System.currentTimeMillis() + "")
.build();
admin.createNamespace(weibo);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null != admin){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8.2.3 创建微博内容表
表结构:
方法名 creatTableeContent
Table Name weibo:content
RowKey 用户ID_时间戳
ColumnFamily info
ColumnLabel 标题,内容,图片
Version 1个版本
代码:
/**

// info.setCompressionType(Algorithm.SNAPPY);
//设置版本确界
attends.setMaxVersions(1);
attends.setMinVersions(1);

    //粉丝列族
    HColumnDescriptor fans = new HColumnDescriptor(Bytes.toBytes("fans"));
    fans.setBlockCacheEnabled(true);
    fans.setBlocksize(2097152);
    fans.setMaxVersions(1);
    fans.setMinVersions(1);
    
    
    relations.addFamily(attends);
    relations.addFamily(fans);
    admin.createTable(relations);
    
} catch (MasterNotRunningException e) {
    e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}finally{
    if(null != admin){
        try {
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}
8.2.5 创建微博收件箱表
表结构:
方法名 createTableReceiveContentEmails
Table Name weibo:receive_content_email
RowKey 用户ID
ColumnFamily info
ColumnLabel 用户ID
ColumnValue 取微博内容的RowKey
Version 1000
代码:
/**

public class Message {
private String uid;
private String timestamp;
private String content;

public String getUid() {
    return uid;
}
public void setUid(String uid) {
    this.uid = uid;
}
public String getTimestamp() {
    return timestamp;
}
public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
@Override
public String toString() {
    return "Message [uid=" + uid + ", timestamp=" + timestamp + ", content=" + content + "]";
}

}
代码:public void publishContent(String uid, String content)
/**

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。

上一篇 下一篇

猜你喜欢

热点阅读