HBase 项目:微博业务需求

题记:

那一夜,就只专注着灌输

所以,那一天,就只干了这事儿

其它的,没做

累吗?

嗯啊

这大概就是,开心着跌爬滚打。。。

============================== 业 务 需 求 =============================

 1. 微博内容的浏览,数据库表设计

 2. 用户社交体现:关注用户,取关用户

 3. 拉取关注的人的微博内容

============================= 实 现 思 路 =============================

 1.  创建命名空间以及表名的定义

      a.设置配置,定义常量表名
 
      b.初始化表与命名空间

      c.创建微博命名空间

 2.  创建微博内容表

-------------------------------------------------------------------------------------------------------------
tableName(表名)  rowKey(行键)      ColumnFamily(列族):column(列名)    Value(值) 

CONTENT_TABLE    uid_timestamp     info :blog                        content
--------------------------------------------------------------------------------------------------------------

 3.  创建微博用户关系表

------------------------------------------------------------------------------------------------------------------------------
tableName       rowkey    ColumnFamily_01:column     Value_01     ColumnFamily_02:column    Value_02 

RELATION_TABLE  uid       attends :attend_uid         attend_uid      fans:fan_uid                         fan_uid
--------------------------------------------------------------------------------------------------------------------------------

 4.  创建微博用户收件箱表(消息提示表)

--------------------------------------------------------------------------------------------------------
tableName             rowKey      ColumnFamily:column   Value

RECEIVE_EMAIL_TABLE   uid     info : attend_uid       attend_rowkey(uid_timestamp)
----------------------------------------------------------------------------------------------------------

 5.  发布微博内容 ----> Put

      a.在用户的微博内容表中添加数据(content)(需连接内容表操作)
 
      b.将发布微博内容用户的 rowkey(uid:uid_timestamp)发送到该用户粉丝的收件箱表中(需连接收件箱表操作)

 6.  添加关注用户 ----> Put

      a.添加关注用户的 id 到 attends 中 (需连接关系表操作)
      
      b.在被关注用户的 列族fans中添加关注用户的 id (需连接关系表操作) 

      c.将被关注用户的微博内容表中的 "id: rowkey" 添加到关注用户的收件箱表中(需连接收件箱表操作)  

 7.  取消(移除)关注用户 ----> Delete

      a.在 attends 中删除被取关用户的 id  (需连接取关用户的关系表操作)
     
      b.在被取关用户的 fans 中删除取关当前用户id  (需连接被取关用户的关系表操作)

      c.在当前取关用户的收件箱表中删除被取关用户的 id 和 rowkey(uid_timestamp) (需连接取关用户的收件箱表操作)

 8.  获取关注的人的微博内容 ---->Get

      a.根据收件箱表获取发布微博用户的 rowkey(需连接收件箱表操作)  

      b.根据发布微博用户的 rowkey,从他的微博内容表中获取他发布的微博(uid,timestamp,content)(需连接内容表操作)

 9.  代码测试 
      a.发布微博内容

      b.添加关注

      c.取消关注

      d.展示内容

=============================== 创 建 WeiBo 类 ============================

IDEA  scala  JAVA Hbase  API:

package WeiBo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WeiBo {
 
    // private Configuration conf = HBaseConfiguration.create();
    public static Configuration conf;
    static {
        conf = HBaseConfiguration.create(); //使用 HBaseConfiguration的单例方法获取配置conf
        conf.set("hbase.zookeeper.quorum","192.168.15.14"); //设置访问hbase.zookeeper的ip地址
        conf.set("hbase.zookeeper.property.clientPort","2181"); //设置访问hbase.zookeeper的端口号
    }
    
    /**
     * 1.1 表名的定义
     */
    private static final byte[] CONTENT_TABLE = Bytes.toBytes("weibo:content");  //定义微博内容表的表名
    private static final byte[] RELATION_TABLE = Bytes.toBytes("weibo:relation"); //定义用户关系表的表名
    private static final byte[] RECEIVE_EMAIL_TABLE = Bytes.toBytes("weibo:receive_email"); //定义收件箱表的表名

    /**
     * 1.2 初始化表与命名空间
     */
    public void initTable(){
        init_namespace(); //创建微博业务命名空间
        content_table(); //创建内容表
        relation_table(); //创建用户关系表
        receive_email_table();  //创建收件箱表
    }

    /**
     * 1.3 创建微博命名空间 
     */
    public void init_namespace(){
        HBaseAdmin admin = null; //创建管理对象(在hbase中管理、访问表需要先创建HBaseAdmin对象),此时未使用
        try {
            admin = new HBaseAdmin(conf); //HBaseAdmin对象(实例化),通过连接hbase对数据库进行操作
            NamespaceDescriptor weibo = NamespaceDescriptor
                    .create("weibo")
                    .addConfiguration("creator","Meng")
                    .addConfiguration("create_time", String.valueOf(System.currentTimeMillis()))
                    .build(); //创建命名空间描述器:名字,创建者,创建时间(感觉像是一本书的封面)
            admin.createNamespace(weibo); //创建命名空间
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != admin){
                try {
                    admin.close(); //关闭连接
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 2.创建微博内容表
     * 表名:weibo:content  (CONTENT_TABLE)
     * 行键:uid_timestamp
     * 列族:info
     * 列名:blog
     * 值  :content
     */
    private void content_table() {
        HBaseAdmin admin = null; //创建HBaseAdmin对象
        try {
            admin = new HBaseAdmin(conf);   //将HBaseAdmin对象实例化
            
            HTableDescriptor content_table_desc = new HTableDescriptor(TableName.valueOf(CONTENT_TABLE));//创建表描述:表属性对象,表名转字节
            HColumnDescriptor column_desc_info = new HColumnDescriptor(Bytes.toBytes("info")); //创建列描述:列族info

            column_desc_info.setBlockCacheEnabled(true); //设置块缓存 
            column_desc_info.setBlocksize(2*1024*1024); //设置块缓存大小: 2M
            //column_desc_info.getCompressionType(Algorithm.SNAPPY); //设置压缩方式:SNAPPY
            //设置版本界限
            column_desc_info.setMaxVersions(1);
            column_desc_info.setMinVersions(1);
            content_table_desc.addFamily(column_desc_info); //将列描述添加到表描述中

            admin.createTable(content_table_desc); //根据对表的配置,创建表
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != admin){
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 3.创建用户关系表
     * 表名:weibo:relation  (RELATION_TABLE)
     * 行键:uid
     * 列族:attends(关注) fans(粉丝)
     * 列名:attend_uid     fan_uid
     * 值  :attend_uid     fan_uid
     */
    private void relation_table() {
        HBaseAdmin admin = null;  //创建HBaseAdmin对象
        try {
            admin = new HBaseAdmin(conf);  //将HBaseAdmin对象实例化
            HTableDescriptor relation_table_desc = new HTableDescriptor(TableName.valueOf(RELATION_TABLE));//创建表描述

            HColumnDescriptor column_desc_attends = new HColumnDescriptor(Bytes.toBytes("attends"));//创建列描述1:关注列族 attends
            column_desc_attends.setBlockCacheEnabled(true); //设置块缓存(有效)
            column_desc_attends.setBlocksize(2*1024*1024); //设置块缓存大小: 2M
            //设置版本界限
            column_desc_attends.setMaxVersions(1);
            column_desc_attends.setMinVersions(1);
            
            relation_table_desc.addFamily(column_desc_attends); //将关注列族添加到表描述中

            HColumnDescriptor column_desc_fans = new HColumnDescriptor(Bytes.toBytes("fans"));  //创建列描述2:粉丝列族 fans
            column_desc_fans.setBlockCacheEnabled(true); //设置块缓存 
            column_desc_fans.setBlocksize(2*1024*1024);//设置块缓存大小: 2M
            //设置版本界限
            column_desc_fans.setMaxVersions(1);
            column_desc_fans.setMinVersions(1);
            
            relation_table_desc.addFamily(column_desc_fans); //将粉丝列族添加到表描述中

            admin.createTable(relation_table_desc); //根据对表的配置,创建用户关系表
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != admin){
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 4.创建微博收件箱表
     * 表名:RECEIVE_EMAIL_TABLE
     * 行键:uid
     * 列族:info
     * 列名:attend_id
     * 值  :attend_rowkey(uid_timestamp)
     */

    private void receive_email_table() {
        HBaseAdmin admin = null;  //创建HBaseAdmin对象
        try {
            admin = new HBaseAdmin(conf);  //将HBaseAdmin对象实例化
            HTableDescriptor recceive_email_table_desc = new HTableDescriptor(TableName.valueOf(RECEIVE_EMAIL_TABLE));//创建表描述
            HColumnDescriptor column_desc_info = new HColumnDescriptor(Bytes.toBytes("info")); //创建列描述:列族 info
            column_desc_info.setBlockCacheEnabled(true); //设置块缓存(有效)
            column_desc_info.setBlocksize(2*1024*1024); //设置块缓存大小: 2M
            //设置版本界限
            column_desc_info.setMaxVersions(1000);
            column_desc_info.setMinVersions(1000);
            
            recceive_email_table_desc.addFamily(column_desc_info); //将列族添加到表描述中

            admin.createTable(recceive_email_table_desc); //根据对表的配置,创建收件箱表
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != admin){
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 5.发布微博内容
     * step 1:向内容表中添加数据
     * step 2:将发布内容的用户rowkey发送给被关注用户
     */
    public void publish_content(String uid,String content){
        HConnection connection = null; //创建hbase连接
        try{
            connection = HConnectionManager.createConnection(conf); //连接实例化
            // step 1:向内容表中添加数据
            HTableInterface content_table_con = connection.getTable(TableName.valueOf(CONTENT_TABLE)); //通过连接实现对内容表的操作
//            HTable content_table_con = new HTable(conf,TableName.valueOf(CONTENT_TABLE)); //通过连接实现对内容表的操作(上面两句可用本句代替,更简洁)
            long timestamp = System.currentTimeMillis(); //获取当前时间戳
            String rowkey = uid + "_" + timestamp; //组装行键 RowKey(uid_timestamp)
            Put put = new Put(Bytes.toBytes(rowkey)); //根据组装好的rowkey,创建一个Put对象:put
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("blog"),timestamp,Bytes.toBytes(content));//把发布的微博内容添加到put中,列族info,列名blog,内容content
            content_table_con.put(put); //通过连接把要插入的数据添加到相应的用户微博内容表中

            // step 2:将发布微博的用户rowkey发送到他所有粉丝的收件箱表中
            //** 首先,从发布微博用户的关系表中获取发布微博用户的粉丝
            HTableInterface relation_table_con = connection.getTable(TableName.valueOf(RELATION_TABLE)); //通过连接实现对关系表的操作
            // HTable relation_table_con = new HTable(conf,TableName.valueOf(RELATION_TABLE));
            Get get = new Get(Bytes.toBytes(uid)); //根据发布微博用户id,创建一个Get对象:get
            get.addFamily(Bytes.toBytes("fans")); //通过指定发布微博用户的fans列族,获取该列族的所有列(uid-->fans:fan_uid)
            Result result = relation_table_con.get(get); //通过连接获取发布微博用户的所有粉丝信息 --> result
            List<byte[]> fan_uids = new ArrayList<byte[]>(); //创建一个fan_uids列表,用来存储所有粉丝的id
           
            // 遍历获取发布微博用户的所有粉丝数据(fan_uid)
            for(Cell cell:result.rawCells()){ //rawCells()将result转换可遍历文件,获取单元格Qualifier信息
                fan_uids.add(CellUtil.cloneQualifier(cell)); //将fans列族中的列名fan_uid添加到fan_uids列表中
            }
            if(fan_uids.size()<=0)return; // 如果当前用户没有粉丝,则直接退出

            //** Then,在粉丝的收件箱表中插入发布微博用户的rowkey
            HTableInterface receive_email_table_con = connection.getTable(TableName.valueOf(RECEIVE_EMAIL_TABLE)); //通过连接实现对收件箱表操作
            //HTable receive_email_table_con = new HTable(conf,TableName.valueOf(RECEIVE_EMAIL_TABLE));
            List<Put> puts = new ArrayList<Put>(); //创建一个Put对象:puts列表,用来存储所关注用户的id和rowkey

            // 遍历所有粉丝,将发布微博用户的id和rowkey存放在puts列表中
            for(byte[] fan_uid:fan_uids){
                Put fan_put = new Put(fan_uid); //通过fan_uid,创建一个put对象:fan_put
                fan_put.addColumn(Bytes.toBytes("info"),Bytes.toBytes(uid),timestamp,Bytes.toBytes(rowkey)); //将发布微博用户的id和rowkey添加到fan_put中
                puts.add(fan_put); // 将fan_put中的数据添加到puts列表中
            }
            receive_email_table_con.put(puts); // 通过连接向收件箱表放置数据 
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=connection){
                try {
                    connection.close(); // 关闭连接
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 6.关注用户
     * step 1:在该用户的 列族attends中添加关注用户的id(关注用户的关系表)
     * step 2:在被关注用户的 列族fans中添加该用户的id (被关注用户的关系表)
     * step 3:将被关注用户的微博内容表中的 "id: rowkey" 添加到该用户的收件箱表中
     */
     public void add_attends(String uid,String... attend_uids){
         
         // 参数过滤:如果没有传递关注的人或没有关注的对象,则直接返回
         if(attend_uids == null || attend_uids.length <= 0|| uid == null || uid.length()<=0){
             return;
         }
         
         HConnection connection = null; //创建hbase连接

         try{
             //step 1:在该用户关系表中添加新关注用户
             //step 2:为被关注的用户添加粉丝(该用户)
             HTable relation_table_con = new HTable(conf,TableName.valueOf(RELATION_TABLE)); //通过连接实现对关系表操作
             List<Put> puts = new ArrayList<Put>(); //创建一个Put对象:puts列表,用来存储所关注用户的id
             Put attend_put = new Put(Bytes.toBytes(uid));//根据该用户id,创建一个Put对象:attend_put,用来存放所有attend_uid
             for(String attend_uid:attend_uids){
                 attend_put.addColumn(Bytes.toBytes("attends"),Bytes.toBytes(attend_uid),Bytes.toBytes(attend_uid));//逐一向该用户关系表中的attends列族中添加关注用户id

                 Put fan_put = new Put(Bytes.toBytes(attend_uid)); //对每一个关注的用户,创建一个Put对象:fan_put,用来存放fan_uid
                 fan_put.addColumn(Bytes.toBytes("fans"),Bytes.toBytes(uid),Bytes.toBytes(uid)); //向一个被关注用户关系表中的fans列族添加该用户的信息id
                 puts.add(fan_put); //将被关注用户要增加的fans列族信息添加到puts列表中
             }
             puts.add(attend_put); //将该用户要增加的attends列族信息添加到puts列表中
             relation_table_con.put(puts); //通过连接操作将要增加的信息添加到相应的用户关系表中

              //step 3:将关注用户的id和rowkey添加到当前用户的收件箱表中
              //3-1:获取所有新关注用户发布微博内容表的rowkey
              HTable content_table_con = new HTable(conf,TableName.valueOf(CONTENT_TABLE)); //通过连接实现对发布微博内容表操作
              Scan scan = new Scan(); //创建Scan对象:scan
              List<byte[]> rowkeys = new ArrayList<byte[]>(); //创建一个列表:rowkeys,用来存放所关注用户微博内容表的rowkey

             //遍历所有新关注用户,将他们的rowkey存放到rowkeys列表中
             for(String attend_uid:attend_uids){
                  PrefixFilter filter = new PrefixFilter(attend_uid.getBytes()); //根据每一个新关注用户的id,创建一个过滤器
                  scan.setFilter(filter); //过滤扫描获取每一个新关注用户的发布微博内容表信息
                  ResultScanner result = content_table_con.getScanner(scan); //将获取的新关注用户的内容表信息存放到result中
                  Iterator<Result> iterator = result.iterator(); //迭代器遍历扫描出来的结果集
                  while(iterator.hasNext()){
                      Result r = iterator.next(); //取出符合扫描结果的一行数据
                      //逐一将新关注用户的rowkey添加到rowkeys列表中
                      for(Cell cell:r.rawCells()){ 
                          rowkeys.add(CellUtil.cloneRow(cell));
                      }
                  }
              }

              if(rowkeys.size()<=0)return; //如果新关注用户没有发表微博内容,则直接退出

             //3-2:将获取的rowkeys列表信息添加到当前用户的收件箱表中
             HTable receive_email_table_con = new HTable(conf,TableName.valueOf(RECEIVE_EMAIL_TABLE)); //通过连接实现对邮件箱表操作
             Put rowkeys_put = new Put(Bytes.toBytes(uid)); //创建Put对象:rowkey_puts,用来存放新关注用户发布微博内容的信息(attend_uid rowkey)

             for(byte[] rk:rowkeys){
                 String rowkey = Bytes.toString(rk);//将字节rowkey转字符
                 String attend_uid = rowkey.substring(0,rowkey.indexOf("_")); //截取新关注用户的rowkey字符串,获得关注用户id
                 long timestamp = Long.parseLong(rowkey.substring(rowkey.indexOf("_")+1)); //获得新关注用户发布内容时间戳

                 rowkeys_put.addColumn(Bytes.toBytes("info"),Bytes.toBytes(attend_uid),timestamp,rk); //将新关注用户的id,timestamp,rowkey添加到rowkeys_put中
             }
             receive_email_table_con.put(rowkeys_put); //通过连接操作把新关注用户的相关信息添加到当前用户的收件箱表
         } catch (Exception e) {
             e.printStackTrace();
         }finally {
             if(null!=connection){
                 try {
                     connection.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }

    /**
     * 7.取消关注
     * step 1:在该用户关系表的列族attends中删除要取消关注的用户id(关注用户的关系表)
     * step 2:在被关注用户关系表的列族fans中删除该用户id (被关注用户的关系表)
     * step 3:从该用户的收件箱表删除取消关注用户的"id: rowkey"
     */
    public void remove_attends(String uid, String... attend_uids){
        HConnection connection = null; //创建连接
        
        //step 1:在该用户关系表的列族attends中删除要取消关注的用户id(关注用户的关系表)
        //step 2:在被关注用户关系表的列族fans中删除该用户id (被关注用户的关系表)"
        try{
            HTable relation_table_con = new HTable(conf,TableName.valueOf(RELATION_TABLE)); //通过连接实现对关系表操作
            List<Delete> deletes = new ArrayList<Delete>(); //创建一个列表deletes,用来存放被取消关注用户的id
            Delete attend_delete = new Delete(Bytes.toBytes(uid)); //根据当前用户ID,创建Delete对象,存储被取关用户的id
            for(String attend_uid:attend_uids){
                attend_delete.addColumn(Bytes.toBytes("attends"),Bytes.toBytes(attend_uid)); //将取关用户的数据添加到删除对象attend_delete中

                Delete fan_delete = new Delete(Bytes.toBytes(attend_uid)); //根据被取关用户id,创建Delete对象,存储当前用户的id
                fan_delete.addColumn(Bytes.toBytes("fans"),Bytes.toBytes(uid)); //从被取关用户的关系表中获取当前用户的数据,添加到删除fan_delete
                deletes.add(fan_delete); //将被取关用户要删除的数据添加到删除列表deletes中
            }
            deletes.add(attend_delete); //将当前用户要删除的数据添加到删除列表deletes中
            relation_table_con.delete(deletes); //通过连接,从取关用户和被取关用户的关系表中删除相应的数据
            
            //step 3:从当前用户的收件箱表删除被取关用户的"id: rowkey"(连接收件箱表)
            HTable receive_email_table_desc = new HTable(conf,TableName.valueOf(RECEIVE_EMAIL_TABLE)); //通过连接实现对收件箱表操作
            Delete receive_deletes = new Delete(Bytes.toBytes(uid)); //根据当前用户id,创建删除对象receive_delete,用来存放被取关用户的id和rowkey

            //遍历被取关用户id,将他们发布微博的相关数据添加到删除对象receive_delete
            for(String attend_uid:attend_uids){
                receive_deletes.addColumn(Bytes.toBytes("info"),Bytes.toBytes(attend_uid)); //从当前用户收件箱表中删除被取关用户的发布微博数据
            }
            receive_email_table_desc.delete(receive_deletes); //通过连接从当前用户收件箱中删除取关用户发布微博的信息
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null!=connection){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 8.获取关注的人的微博内容
     * step 1:根据收件箱表获取关注用户的rowkey{}
     * step 2:根据关注用户的id在他的微博内容表中获取他发布的微博内容--->List<Message>
     */
    public List<Message> get_attends_contents(String uid) {
        HConnection connection = null; //创建连接,未使用

        try {
            //step 1:根据收件箱表获取关注用户发布微博的rowkey ---->Get
            HTable receive_email_table_con = new HTable(conf, TableName.valueOf(RECEIVE_EMAIL_TABLE)); //通过连接实现对收件箱表的操作
            Get get = new Get(Bytes.toBytes(uid)); //创建get对象,用来获取当前用户邮件箱表的数据
            Result result = receive_email_table_con.get(get); //从用户的邮件箱表中获取所有数据
            List<byte[]> rowkeys = new ArrayList<byte[]>(); //创建一个列表,用来存放关注用户发布微博的rowkey

            //遍历邮件箱中的单元格,将发布微博用户的rowkey添加到rowkeys 列表中
            for (Cell cell : result.rawCells()) {
                rowkeys.add(CellUtil.cloneValue(cell));
            }

            //step 2:根据关注用户发布微博的rowkey从他的微博内容表中获取具体的微博内容---->Get
            HTable content_table_con = new HTable(conf, TableName.valueOf(CONTENT_TABLE)); //通过连接实现对发布微博内容表的操作
            List<Get> gets = new ArrayList<Get>(); //创建Get对象,用来存放关注用户发布的微博内容
            for (byte[] rowkey : rowkeys) {
                Get row_get = new Get(rowkey); //根据每个发布微博的rowkey创建Get对象
                gets.add(row_get); //将rowkey_get添加到get列表
            }
            
            //step 3:将获得的内容整理到Message列表中
            Result[] results = content_table_con.get(gets); //通过连接获取gets列表中的rowkeys及每个rowkey所对应的微博内容,并存放于results
            List<Message> blog_contents = new ArrayList<Message>(); //创建Message列表,存储发布微博用户的数据:id,timestamp,content
            
            for (Result rs : results) {
                //遍历结果列表中每行数据的单元格,将发布微博用户的id,timestamp,content存储到blog_contents列表
                for (Cell cell : rs.rawCells()) {
                    Message blog = new Message(); //对每一个发布微博用户的rowkey,创建一个Message对象,存放发布微博的数据
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell)); //获取行键
                    String blog_uid = rowkey.substring(0, rowkey.indexOf("_")); //获取发布微博用户的id
                    String blog_timestamp = rowkey.substring(rowkey.indexOf("_") + 1); //获取发布微博的时间戳
                    String blog_content = Bytes.toString(CellUtil.cloneValue(cell)); //获取发布的微博内容

                    blog.setUid(blog_uid); //设置blog_content的用户ID为:blog_uid
                    blog.setTimestamp(blog_timestamp); //设置blog_content的时间戳为:timestamp
                    blog.setContent(blog_content); //设置blog_content的内容为:content
                    blog_contents.add(blog); //将组装好的blog_content添加到Message列表blog_contents中
                }
            }
            return blog_contents; //返回Message列表,即可查看关注的用户发布的微博内容
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (null != connection) {
                try {
                    connection.close(); // 关闭连接
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    /**
     * 9.代码测试
     * a.发布微博内容
     * b.添加关注
     * c.取消关注
     * d.展示内容
     */
    //a.发布微博内容
    public void test_publish_content(WeiBo wb){
        wb.publish_content("0001","【声明】因工作调整,中国国际广播电台英语新闻直播节目The Beijing Hour官方微博自2018年6月16日起停止一切内容更新,包括每日节目预告,由衷感谢您多年的陪伴与支持。");
        wb.publish_content("0001","随时随地发现新鲜事!微博带你欣赏世界上每一个精彩瞬间,了解每一个幕后故事。分享你想表达的,让全世界都能听到你的心声!");
    }

    //b.添加关注
    public void test_add_attends(WeiBo wb){
        wb.publish_content("0003","人生最美是清欢");
        wb.publish_content("0007","Alawys believe that something wonderful is about to happen...");
        wb.publish_content("0009","人生有很多岔路口,不过是,你拐弯我直走。。。");
        wb.add_attends("0001","0003","0007","0009");
    }

    //c.取消关注
    public void test_remove_attends(WeiBo wb){
        wb.remove_attends("0001","0003");
    }

    //d.展示内容
    public void test_show_messages(WeiBo wb){
        List<Message> messages = wb.get_attends_contents("0001");
        for(Message message:messages){
            System.out.println(message);
        }
    }

    public static void main(String[] args){
        WeiBo weibo = new WeiBo();
        weibo.initTable();
        weibo.test_publish_content(weibo);
        weibo.test_add_attends(weibo);
        weibo.test_remove_attends(weibo);
        weibo.test_show_messages(weibo);
    }

}

执行时出现的小插曲:

由于我在写代码时漏掉了一点儿,如

List<Get> gets = new ArrayList<>();   —— 后面<>里面忘记写 Get

List<byte[]> fan_uids = new ArrayList<>();  ——    后面<>里面忘记写 byte[]

List<Put> puts = new ArrayList<>();   ——后面<>里面忘记写  Put

。。。。。。

结果就显示以这句话开始的几句错误信息:

Exception in thread "main" java.lang.NullPointerException at org.apache.hado

通过搜查很多人反应是某个地方出现空值,于是仔细看了一遍代码终于发现漏洞,补上它们:

List<Get> gets = new ArrayList<Get>();   

List<byte[]> fan_uids = new ArrayList<byte[]>();  

List<Put> puts = new ArrayList<Put>();     

。。。。。。

结果就完美地运行出来啦!

========================= 部 分 展 示 ============================

hbase(main):002:0> list

=> ["fruit", "student", "weibo:content", "weibo:receive_email", "weibo:relation"]

hbase(main):005:0> scan "weibo:relation"
ROW                          COLUMN+CELL                                                                       
 0001                        column=attends:0007, timestamp=1569227889548, value=0007                          
 0001                        column=attends:0009, timestamp=1569227889548, value=0009                          
 0007                        column=fans:0001, timestamp=1569227889548, value=0001                             
 0009                        column=fans:0001, timestamp=1569227889548, value=0001 

 

Message[uid=0007,timestamp=1569227888527,content=Alawys believe that something wonderful is about to happen...]
Message[uid=0009,timestamp=1569227888638,content=人生有很多岔路口,不过是,你拐弯我直走。。。]

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值