HBase博客案例

一、 需求分析
1、微博内容的浏览,数据库表设计
2、用户社交体现:关注用户,取关用户
3、拉取关注的人的微博内容
二、 所需要的表
在这里插入图片描述
三、代码
pom.xml

  <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>

dao

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;

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


public class Dao {
    //命名空间
    private static final String NAME_SPACE="weibo";
    //用户关系表名
    private static final String USER_TABLE=NAME_SPACE+":user";
    //收件箱表
    private static final String MSG_TABLE=NAME_SPACE+":msg";
    //微博内容表
    private static final String CONENT_TABLE=NAME_SPACE+":conent";


    //初始代码块,获取配置conf
    private static Configuration conf=null;
    private static Connection conn=null;
    static {
        conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop103");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //业务1:发布微博
    public void putWeibo(String uid,String neirong) throws Exception{
        long l = System.currentTimeMillis();
        String rowKey=uid+"_"+l;
        //第一步  放微博内容进微博表
        Table connTable = conn.getTable(TableName.valueOf(CONENT_TABLE));
        Put put=new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes("conent"),Bytes.toBytes(rowKey),Bytes.toBytes(neirong));
        connTable.put(put);
        //读取粉丝表
        Table userTable =conn.getTable(TableName.valueOf(USER_TABLE));
        Get get = new Get(Bytes.toBytes(uid));
       // get.addColumn(Bytes.toBytes("fens"),Bytes.toBytes())
        get.addFamily(Bytes.toBytes("fens"));
        Result result = userTable.get(get);
        Cell[] cells = result.rawCells();
        ArrayList<byte[]> fens_list = new ArrayList<byte[]>();
        for (Cell cell : cells) {
            byte[] bytes = CellUtil.cloneQualifier(cell);
            fens_list.add(bytes);
        }
        System.out.println(fens_list.size());
        //在邮箱表里为粉丝放入发布的微博
        Table msgTable = conn.getTable(TableName.valueOf(MSG_TABLE));
        for (byte[] bytes : fens_list) {
            Put msg_put=new Put(bytes);
            msg_put.addColumn(Bytes.toBytes("info"),Bytes.toBytes(rowKey),Bytes.toBytes(""));
            msgTable.put(msg_put);
        }
        System.out.println("succed add wei bo");
        msgTable.close();
        userTable.close();
        connTable.close();
    }

    //业务2  添加关注
    public void AddAtter(String uid,String ... atter) throws Exception{
        //1 、user表  uid添加atter   atter添加 粉丝
        Table userTable = conn.getTable(TableName.valueOf(USER_TABLE));
        Put put =new Put(Bytes.toBytes(uid));

        for (String attr : atter) {
           put.addColumn(Bytes.toBytes("atter"),Bytes.toBytes(attr),Bytes.toBytes(""));
           Put put1 = new Put(Bytes.toBytes(attr));
           put1.addColumn(Bytes.toBytes("fens"),Bytes.toBytes(uid),Bytes.toBytes(""));
           userTable.put(put1);
        }
        userTable.put(put);
        //2、查找关注人的微博内容
        Table conTable = conn.getTable(TableName.valueOf(CONENT_TABLE));
        Scan scan = new Scan();
        ArrayList<byte[]> cLink = new ArrayList<byte[]>();
        for (String atterId : atter) {
            RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(atterId));
            scan.setFilter(rowFilter);
            ResultScanner results = conTable.getScanner(scan);
            for (Result result : results) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    byte[] bytes = CellUtil.cloneQualifier(cell);
                    cLink.add(bytes);
                }
            }
        }
        if(cLink.size()>=1){
            //把微博连接放入msg表中
            Table msgTable = conn.getTable(TableName.valueOf(MSG_TABLE));
            Put put1 = new Put(Bytes.toBytes(uid));
            for (byte[] bytes : cLink) {
                put1.addColumn(Bytes.toBytes("info"),bytes,Bytes.toBytes(""));
            }
            msgTable.put(put1);

            msgTable.close();
        }

        conTable.close();
        userTable.close();

    }
    //业务3  取消关注
    public  void delUser(String uid,String delId) throws Exception{
        //1 自己关注表取消 ,对方 粉丝表取消
        Table userTable = conn.getTable(TableName.valueOf(USER_TABLE));
        Delete delete1 = new Delete(Bytes.toBytes(uid));
        delete1.addColumn(Bytes.toBytes("atter"),Bytes.toBytes(delId));
        Delete delete2 = new Delete(Bytes.toBytes(delId));
        delete2.addColumn(Bytes.toBytes("fens"),Bytes.toBytes(uid));
        userTable.delete(delete1);
        userTable.delete(delete2);

        //2 取关的msg表删除 delid的微博动态
        Table msgTable = conn.getTable(TableName.valueOf(MSG_TABLE));
        Get get = new Get(Bytes.toBytes(uid));
        get.addFamily(Bytes.toBytes("info"));
        Result result = msgTable.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            byte[] bytes = CellUtil.cloneQualifier(cell);

           // RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(delId));

            String lie = Bytes.toString(CellUtil.cloneQualifier(cell));
            System.out.println(lie);
            if(lie.startsWith(delId)){
                System.out.println("shanchu"+lie+'\t'+delId);
                Delete delete = new Delete(Bytes.toBytes(uid));
                delete.addColumn(Bytes.toBytes("info"),bytes);
                msgTable.delete(delete);
            }

        }
        msgTable.close();
        userTable.close();

    }
    //业务4 读取某人的msg
    public  void  catMsg(String uid) throws Exception{
        Table msgTable = conn.getTable(TableName.valueOf(MSG_TABLE));
        Get get = new Get(Bytes.toBytes(uid));
        get.addFamily(Bytes.toBytes("info"));
        Result result = msgTable.get(get);
        Cell[] cells = result.rawCells();
        ArrayList<byte[]> conRowkey = new ArrayList<byte[]>();
        for (Cell cell : cells) {
            byte[] bytes = CellUtil.cloneQualifier(cell);
            conRowkey.add(bytes);
        }

        if(conRowkey.size()>=1){
            Table conTable = conn.getTable(TableName.valueOf(CONENT_TABLE));
            for (byte[] rowKey : conRowkey) {
                Get getCon = new Get(rowKey);
                getCon.addColumn(Bytes.toBytes("conent"),rowKey);
                Result con = conTable.get(getCon);
                Cell[] cells1 = con.rawCells();
                for (Cell cell : cells1) {
                    String neirong = Bytes.toString(CellUtil.cloneValue(cell));
                    String att =Bytes.toString(rowKey);
                    System.out.println(att.substring(0,4)+"\t"+neirong);
                }

            }
            conTable.close();

        }else {
            System.out.println("no msg!");
        }
        msgTable.close();
    }
    //业务5  读取所有表
    public void catAll() throws Exception{
        Table userTable = conn.getTable(TableName.valueOf(USER_TABLE));
        Scan scan = new Scan();
        ResultScanner scanResults = userTable.getScanner(scan);
        for (Result scanResult : scanResults) {
            Cell[] cells = scanResult.rawCells();
            for (Cell cell : cells) {
               String row= Bytes.toString(CellUtil.cloneRow(cell));
                String famliy=Bytes.toString( CellUtil.cloneFamily(cell));
                String qualifier= Bytes.toString(CellUtil.cloneQualifier(cell));
                String value=Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(row+'\t'+famliy+":"+qualifier+'\t'+value);
            }
        }

        Table conTable = conn.getTable(TableName.valueOf(CONENT_TABLE));
        Scan scan1 = new Scan();
        ResultScanner scanResults1 = conTable.getScanner(scan1);
        for (Result scanResult : scanResults1) {
            Cell[] cells = scanResult.rawCells();
            for (Cell cell : cells) {
                String row= Bytes.toString(CellUtil.cloneRow(cell));
                String famliy=Bytes.toString( CellUtil.cloneFamily(cell));
                String qualifier= Bytes.toString(CellUtil.cloneQualifier(cell));
                String value=Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(row+'\t'+famliy+":"+qualifier+'\t'+value);
            }
        }

        Table msgTable = conn.getTable(TableName.valueOf(MSG_TABLE));
        Scan scan2 = new Scan();
        ResultScanner scanResults2 = msgTable.getScanner(scan2);
        for (Result scanResult : scanResults2) {
            Cell[] cells = scanResult.rawCells();
            for (Cell cell : cells) {
                String row= Bytes.toString(CellUtil.cloneRow(cell));
                String famliy=Bytes.toString( CellUtil.cloneFamily(cell));
                String qualifier= Bytes.toString(CellUtil.cloneQualifier(cell));
                String value=Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(row+'\t'+famliy+":"+qualifier+'\t'+value);
            }
        }
        msgTable.close();
        conTable.close();
        userTable.close();
    }




    //创建命名空间
    public void CreateNameSpace() throws Exception{
        Admin admin = conn.getAdmin();
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(NAME_SPACE).build();
        admin.createNamespace(namespaceDescriptor);
        System.out.println("命名空间建立完成");
        admin.close();
      //  conn.close();
    }
    //建user表:两个列族 atter 关注    fens粉丝
    public void CreateUserTable() throws Exception{
        Admin admin = conn.getAdmin();
       HTableDescriptor table=  new HTableDescriptor(TableName.valueOf(USER_TABLE));
        HColumnDescriptor atter = new HColumnDescriptor("atter");
        HColumnDescriptor fens = new HColumnDescriptor("fens");
        table.addFamily(atter);
        table.addFamily(fens);
        admin.createTable(table);
        System.out.println(USER_TABLE+"建表成功");
        admin.close();
        //conn.close();
    }
    //建CONENT_TABLE表:一个列族 conent 微博内容
    public void CreateConentTable() throws Exception{
        Admin admin = conn.getAdmin();
        HTableDescriptor table=  new HTableDescriptor(TableName.valueOf(CONENT_TABLE));
        HColumnDescriptor conent = new HColumnDescriptor("conent");
        table.addFamily(conent);
        admin.createTable(table);
        System.out.println(CONENT_TABLE+"建表成功");
        admin.close();
        //conn.close();
    }
    //建msg表:一个列族 info
    public void CreateMsgTable() throws Exception{
        Admin admin = conn.getAdmin();
        HTableDescriptor table=  new HTableDescriptor(TableName.valueOf(MSG_TABLE));
        HColumnDescriptor info = new HColumnDescriptor("info");
        table.addFamily(info);
        admin.createTable(table);
        System.out.println(MSG_TABLE+"建表成功");
        admin.close();
       // conn.close();
    }


}

sever(main)

public class server {
    public static void main(String[] args) throws Exception {
        Dao dao= new Dao();
      /* 1、 建表
        dao.CreateNameSpace();
        dao.CreateUserTable();
        dao.CreateMsgTable();
        dao.CreateConentTable();*/
       //dao.AddAtter("1003","1004" );
       //dao.putWeibo("1004","today is 20201230");
        //dao.delUser("1003","1004");
        //dao.catMsg("1001");
        dao.catAll();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值