HBase的微博案例
HBase的微博案例
- 1. 實驗環(huán)境說明
- 2. 實驗?zāi)康?/li>
- 3. 實驗步驟
- 3.1 正常啟動HADOOP、ZOOKEEPER
- 3.2 啟動HBASE
- 3.3 實驗步驟
- 3.3.1 先把虛擬機(jī)的地址映射加入到windows下的Hosts
- 3.3.2 搭建Maven項目
- 3.3.3 添加依賴并在resource目錄下添加hbase-site.xml
- 3.3.4 HBase微博案例
- 3.3.4.1 表結(jié)構(gòu)設(shè)計
- 3.3.4.2 定義常量接口Constants
- 3.3.4.3 創(chuàng)建命名空間以及表名的定義
- 3.3.4.4 微博操作的Dao類
- 3.3.4.5 信息對象JavaBean類
- 3.3.4.6 測試類
- 3.3.4.7 測試結(jié)果
- 4.總結(jié)
1. 實驗環(huán)境說明
安裝HBase前已經(jīng)安裝好了Hadoop完全分布式集群,和Zookeeper集群.其中Hadoop版本為2.7.2 , Zookeeper的版本為3.4.12, 虛擬機(jī)系統(tǒng)為centos7.5
另外,本人所有的軟件都會裝在/software/spath/目錄下
實驗用到的三臺虛擬機(jī):
Hadoop集群部署:
2. 實驗?zāi)康?/h1>
通HBase的微博模擬案例,加強(qiáng)對HBase的理解和應(yīng)用,能夠把知識用于實踐,并且能熟系掌握HBase的Java API操作.
3. 實驗步驟
3.1 正常啟動HADOOP、ZOOKEEPER
啟動Zookeeper
3.2 啟動HBASE
3.3 實驗步驟
3.3.1 先把虛擬機(jī)的地址映射加入到windows下的Hosts
方法一:
直接在C:\Windows\System32\drivers\etc下編輯hosts文件,然后加入虛擬機(jī)的地址映射.
方法二: (推薦)
實驗SwitchHosts軟件,啟用不同的映射策略
3.3.2 搭建Maven項目
3.3.3 添加依賴并在resource目錄下添加hbase-site.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>red.ygs</groupId><artifactId>hbase</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.3.1</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.3.1</version></dependency></dependencies> </project>
hbase-site.xml
3.3.4 HBase微博案例
3.3.4.1 表結(jié)構(gòu)設(shè)計
A.創(chuàng)建微博內(nèi)容表
表結(jié)構(gòu):
B.創(chuàng)建用戶關(guān)系表
表結(jié)構(gòu)
C. 創(chuàng)建微博收件箱表
表結(jié)構(gòu)
3.3.4.2 定義常量接口Constants
package red.ygs.bigdata.weibo.constants;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration;/*** @author YGS---www.ygs.red* @create 2022-02-10 21:36* @description: 常量類*/ public class Constants {//HBase的配置信息public static Configuration CONFIGURATION = HBaseConfiguration.create();//命名空間public static String NAMESPACE = "weibo";//微博內(nèi)容表public static String CONTENT_TABLE = "weibo:content";public static String CONTENT_TABLE_CF ="info";public static int CONTENT_TABLE_VERSION = 1;//用戶關(guān)系表public static String RELATION_TABLE = "weibo:relation";public static String RELATION_TABLE_CF1 = "attends";public static String RELATION_TABLE_CF2 = "fans";public static int RELATION_TABLE_VERSION = 1;//收件箱表public static String INBOX_TABLE = "weibo:inbox";public static String INBOX_TABLE_CF ="info";public static int INBOX_TABLE_VERSION = 2;}3.3.4.3 創(chuàng)建命名空間以及表名的定義
package red.ygs.bigdata.weibo.utils;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.util.Bytes; import red.ygs.bigdata.weibo.constants.Constants;import java.io.IOException;/*** @author YGS---www.ygs.red* @create 2022-02-10 21:40* @description: HBase工具類*/ /* * 1.創(chuàng)建命名空間 * 2.判斷表是否存在 * 3.創(chuàng)建表 * * */ public class HBaseUtils {// TODO 創(chuàng)建命名空間public static void createNameSpace(String nameSpace) throws IOException {//1.獲取connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);//2.獲取admin對象Admin admin = connection.getAdmin();//3.構(gòu)建命名空間描述器NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();//4.創(chuàng)建命名空間admin.createNamespace(namespaceDescriptor);//5.關(guān)閉資源admin.close();connection.close();}// TODO 判斷表是否存在private static Boolean isTableExist(String tableName) throws IOException {//1.獲取connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);//2.獲取admin對象Admin admin = connection.getAdmin();//3.判斷是否存在Boolean result=admin.tableExists(TableName.valueOf(tableName));//4..關(guān)閉資源admin.close();connection.close();return result;}// TODO 創(chuàng)建表public static void createTable(String tableName, int version , String... cfs) throws IOException {//1.判斷是否存在列族信息if (cfs.length <= 0 ){System.out.println("請設(shè)置列族信息!!!");return;}//2.判斷表是否存在if(isTableExist(tableName)){System.out.println(tableName+"表已經(jīng)存在!!!");return;}//3.獲取connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);//4.獲取admin對象Admin admin = connection.getAdmin();//5.創(chuàng)建表描述器HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//6.循環(huán)添加列族信息for (String cf: cfs ) {//6.1.創(chuàng)建列族描述器HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);//6.2.設(shè)置版本hColumnDescriptor.setMaxVersions(version);//6.3.添加具體的列族信息hTableDescriptor.addFamily(hColumnDescriptor);}//7. 創(chuàng)建表admin.createTable(hTableDescriptor);//8.關(guān)閉資源connection.close();admin.close();}}3.3.4.4 微博操作的Dao類
package red.ygs.bigdata.weibo.dao;import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; 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 red.ygs.bigdata.weibo.bean.Message; import red.ygs.bigdata.weibo.constants.Constants;import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List;/*** @author YGS---www.ygs.red* @create 2022-02-11 10:54* @description:*/ /** 1.發(fā)布微博* 2.刪除微博* 3.關(guān)注用戶* 4.取關(guān)用戶* 5.獲取用戶的微博詳情* 6.獲取用戶的初始化頁面* */public class HBaseDao {/*** 發(fā)布微博* a、微博內(nèi)容表中數(shù)據(jù)+1* b、向微博收件箱表中加入微博的 Rowkey*/// TODO 1.發(fā)布微博public static void publishWeiBo(String uid, String content) throws IOException {// 1.獲取Connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);// 第一部分:操作微博內(nèi)容表// 1.獲取微博內(nèi)容表對象Table conTable = connection.getTable(TableName.valueOf(Constants.CONTENT_TABLE));// 2.獲取當(dāng)前時間戳long ts = System.currentTimeMillis();// 3.獲取rowkeyString rowKey = uid + "_" + ts;// 4.創(chuàng)建Put對象Put conPut = new Put(Bytes.toBytes(rowKey));// 5.給put對象賦值conPut.addColumn(Bytes.toBytes(Constants.CONTENT_TABLE_CF), Bytes.toBytes("content"), Bytes.toBytes(content));// 6.執(zhí)行插入數(shù)據(jù)的操作conTable.put(conPut);// 第二部分:操作微博收件箱表// 1.獲取用戶關(guān)系表對象Table relaTable = connection.getTable(TableName.valueOf(Constants.RELATION_TABLE));// 2.獲取當(dāng)前發(fā)布微博人的fans列簇數(shù)據(jù)Get get = new Get(Bytes.toBytes(uid));Result result = relaTable.get(get);// 3.創(chuàng)建一個集合,用于存放微博內(nèi)容表的Put對象ArrayList<Put> inboxPuts = new ArrayList<Put>();// 4.遍歷粉絲for (Cell cell : result.rawCells()) {// 5.構(gòu)建微博收件箱表的Put對象Put inboxPut = new Put(CellUtil.cloneQualifier(cell));// 6.給收件箱表的Put對象賦值inboxPut.addColumn(Bytes.toBytes(Constants.INBOX_TABLE_CF), Bytes.toBytes(uid), Bytes.toBytes(rowKey));// 7.將收件箱表的Put對象存入集合inboxPuts.add(inboxPut);}// 8.判斷是否有粉絲if (inboxPuts.size() > 0) {// 獲取收件箱對象Table inboxTable = connection.getTable(TableName.valueOf(Constants.INBOX_TABLE));// 執(zhí)行收件箱表數(shù)據(jù)插入操作inboxTable.put(inboxPuts);// 關(guān)閉收件箱表inboxTable.close();}// 關(guān)閉資源connection.close();relaTable.close();conTable.close();}/*** 關(guān)注用戶邏輯* a、在微博用戶關(guān)系表中,對當(dāng)前主動操作的用戶添加新的關(guān)注的好友* b、在微博用戶關(guān)系表中,對被關(guān)注的用戶添加粉絲(當(dāng)前操作的用戶)* c、當(dāng)前操作用戶的微博收件箱添加所關(guān)注的用戶發(fā)布的微博 rowkey*/// TODO 3.關(guān)注用戶public static void addAttends(String uid, String... attends) throws IOException {//校驗是否添加了待關(guān)注的人if (attends.length <= 0) {System.out.println("請選擇待關(guān)注的人!");return;}// 獲取Connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);// 第一部分:操作用戶關(guān)系表// 1.獲取用戶關(guān)系表對象Table relaTable = connection.getTable(TableName.valueOf(Constants.RELATION_TABLE));// 2.創(chuàng)建一個集合,用于存放用戶關(guān)系表的Put對象ArrayList<Put> relaPuts = new ArrayList<Put>();// 3.創(chuàng)建操作著的Put對象Put uidPut = new Put(Bytes.toBytes(uid));// 4.循環(huán)創(chuàng)建被關(guān)注的著Put對象for (String attend : attends) {// 5.給操作者的Put對象賦值uidPut.addColumn(Bytes.toBytes(Constants.RELATION_TABLE_CF1), Bytes.toBytes(attend), Bytes.toBytes(attend));// 6.創(chuàng)建被關(guān)注著的Put對象Put attendPut = new Put(Bytes.toBytes(attend));// 7.給被關(guān)注著的Put對象賦值attendPut.addColumn(Bytes.toBytes(Constants.RELATION_TABLE_CF2), Bytes.toBytes(uid), Bytes.toBytes(uid));// 8.將被關(guān)注著的Put對象放入集合relaPuts.add(attendPut);}// 9.將操作者的Put對象添加至集合relaPuts.add(uidPut);// 10.執(zhí)行用戶關(guān)系表的插入數(shù)據(jù)操作relaTable.put(relaPuts);// 第二部分:操作收件箱表// 1.獲取微博內(nèi)容表的對象Table conTable = connection.getTable(TableName.valueOf(Constants.CONTENT_TABLE));// 2.創(chuàng)建收件箱表的Put對象Put inboxPut = new Put(Bytes.toBytes(uid));// 3.循環(huán)attends,獲取每個被關(guān)注著的近期發(fā)布的微博for (String attend : attends) {// 4.獲取當(dāng)前被關(guān)注著的近期發(fā)布的微博scanScan scan = new Scan(Bytes.toBytes(attend + "_"), Bytes.toBytes(attend + "|"));ResultScanner resultScanner = conTable.getScanner(scan);// 5.對獲取的值進(jìn)行遍歷// 定義一個時間戳long ts = System.currentTimeMillis();for (Result result : resultScanner) {// 6.給收件箱的Put對象賦值inboxPut.addColumn(Bytes.toBytes(Constants.INBOX_TABLE_CF), Bytes.toBytes(attend), ts++, result.getRow());}}// 7.判斷當(dāng)前的Put對象是否為空if (!inboxPut.isEmpty()) {// 獲取收件箱表對象Table inboxTable = connection.getTable(TableName.valueOf(Constants.INBOX_TABLE));// 插入數(shù)據(jù)inboxTable.put(inboxPut);// 關(guān)閉收件箱表連接inboxTable.close();}// 關(guān)閉資源relaTable.close();conTable.close();connection.close();}/*** 取消關(guān)注(remove)* a、在微博用戶關(guān)系表中,對當(dāng)前主動操作的用戶刪除對應(yīng)取關(guān)的好友* b、在微博用戶關(guān)系表中,對被取消關(guān)注的人刪除粉絲(當(dāng)前操作人)* c、從收件箱中,刪除取關(guān)的人的微博的 rowkey*/// TODO 4.取關(guān)用戶public static void deleteAttends(String uid, String... dels) throws IOException {if (dels.length <= 0) {System.out.println("請?zhí)砑哟£P(guān)的用戶!");return;}// 1.獲取Connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);// 第一部分:操作用戶關(guān)系表// 1.獲取用戶關(guān)系表對象Table relaTable = connection.getTable(TableName.valueOf(Constants.RELATION_TABLE));// 2.創(chuàng)建一個集合,用于存放用戶關(guān)系表的Delete對象ArrayList<Delete> relaDeletes = new ArrayList<Delete>();// 3.創(chuàng)建操作者的Delete對象Delete uidDelete = new Delete(Bytes.toBytes(uid));// 4.循環(huán)創(chuàng)建被取關(guān)的Delete對象for (String del : dels) {// 5.給操作者的Delete對象賦值uidDelete.addColumns(Bytes.toBytes(Constants.RELATION_TABLE_CF1), Bytes.toBytes(del));// 6.創(chuàng)建被取關(guān)的Delete對象Delete delDelete = new Delete(Bytes.toBytes(del));// 7.給被取關(guān)者的Delete對象賦值delDelete.addColumns(Bytes.toBytes(Constants.RELATION_TABLE_CF2), Bytes.toBytes(uid));// 8.將被取關(guān)者的Delete對象添加至集合relaDeletes.add(delDelete);}// 9.將操作者的Delete對象添加至集合relaDeletes.add(uidDelete);// 10.執(zhí)行用戶關(guān)系表的刪除操作relaTable.delete(relaDeletes);// 第二部分:操作收件箱表// 1.獲取收件箱表對象Table inboxTable = connection.getTable(TableName.valueOf(Constants.INBOX_TABLE));// 2.創(chuàng)建操作者的Delete對象Delete inboxDelete = new Delete(Bytes.toBytes(uid));// 3.給操作者的Delete賦值for (String del : dels) {inboxDelete.addColumns(Bytes.toBytes(Constants.INBOX_TABLE_CF), Bytes.toBytes(del));}// 4.執(zhí)行收件箱表的刪除操作inboxTable.delete(inboxDelete);// 關(guān)閉資源relaTable.close();inboxTable.close();connection.close();}/*** 獲取微博實際內(nèi)容* a、從微博收件箱中獲取所有關(guān)注的人的發(fā)布的微博的 rowkey* b、根據(jù)得到的 rowkey 去微博內(nèi)容表中得到數(shù)據(jù)* c、將得到的數(shù)據(jù)封裝到 Message 對象中*/// TODO 5.獲取用戶的微博詳情public static void getWeiBO(String uid) throws IOException {// 1.獲取Connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);// 2.獲取微博內(nèi)容表對象Table conTable = connection.getTable(TableName.valueOf(Constants.CONTENT_TABLE));// 3.構(gòu)建Scan對象Scan scan = new Scan();// 構(gòu)建過濾器RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(uid + "_"));scan.setFilter(rowFilter);// 4.獲取數(shù)據(jù)ResultScanner resultScanner = conTable.getScanner(scan);// 5.解析數(shù)據(jù)并打印for (Result result : resultScanner) {for (Cell conCell : result.rawCells()) {System.out.println("RK="+Bytes.toString(CellUtil.cloneRow(conCell)));System.out.println("CF="+Bytes.toString(CellUtil.cloneFamily(conCell)));System.out.println("CN="+Bytes.toString(CellUtil.cloneQualifier(conCell)));System.out.println("Value="+Bytes.toString(CellUtil.cloneValue(conCell)));}}// 6.關(guān)閉資源conTable.close();connection.close();}// TODO 6.獲取用戶的的初始化頁面public static void getInit(String uid) throws IOException {// 1.獲取Connection對象Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);// 2.獲取收件箱表對象Table inboxTable = connection.getTable(TableName.valueOf(Constants.INBOX_TABLE));// 3.獲取微博內(nèi)容表對象Table conTable = connection.getTable(TableName.valueOf(Constants.CONTENT_TABLE));// 4.創(chuàng)建收件箱表Get對象,并獲取數(shù)據(jù)(設(shè)置最大版本)Get inboxGet = new Get(Bytes.toBytes(uid));inboxGet.setMaxVersions();Result result = inboxTable.get(inboxGet);// 5.遍歷獲取的數(shù)據(jù)for (Cell cell : result.rawCells()) {// 6.構(gòu)建微博內(nèi)容表Get對象Get contGet = new Get(CellUtil.cloneValue(cell));// 7.獲取該Get對象的數(shù)據(jù)內(nèi)容Result contResult = conTable.get(contGet);// 8.解析內(nèi)容并打印for (Cell conCell : contResult.rawCells()) {System.out.println("RK:"+Bytes.toString(CellUtil.cloneRow(conCell)));System.out.println("CF:"+Bytes.toString(CellUtil.cloneFamily(conCell)));System.out.println("CN:"+Bytes.toString(CellUtil.cloneQualifier(conCell)));System.out.println("Value:"+Bytes.toString(CellUtil.cloneValue(conCell)));}}// 9.關(guān)閉資源inboxTable.close();conTable.close();connection.close();} }3.3.4.5 信息對象JavaBean類
package red.ygs.bigdata.weibo.bean;/*** @author YGS---www.ygs.red* @create 2022-02-11 18:47* @description:*/ 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;}@Overridepublic String toString() {return "Message [uid=" + uid + ", timestamp=" + timestamp +", content=" + content + "]";} }3.3.4.6 測試類
package red.ygs.bigdata.weibo.test;import red.ygs.bigdata.weibo.constants.Constants; import red.ygs.bigdata.weibo.dao.HBaseDao; import red.ygs.bigdata.weibo.utils.HBaseUtils;import java.io.IOException;/*** @author YGS---www.ygs.red* @create 2022-04-17 20:26* @description:*/ public class WeiBoTest {public static void init(){try {// 創(chuàng)建命名空間HBaseUtils.createNameSpace(Constants.NAMESPACE);} catch (IOException e) {e.printStackTrace();}try {// 創(chuàng)建微博內(nèi)容表HBaseUtils.createTable(Constants.CONTENT_TABLE,Constants.CONTENT_TABLE_VERSION,Constants.CONTENT_TABLE_CF);} catch (IOException e) {e.printStackTrace();}// 創(chuàng)建用戶關(guān)系表try {HBaseUtils.createTable(Constants.RELATION_TABLE,Constants.RELATION_TABLE_VERSION,Constants.RELATION_TABLE_CF1,Constants.RELATION_TABLE_CF2);} catch (IOException e) {e.printStackTrace();}// 創(chuàng)建收件箱表try {HBaseUtils.createTable(Constants.INBOX_TABLE,Constants.INBOX_TABLE_VERSION,Constants.INBOX_TABLE_CF);} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException, InterruptedException {// 初始化init();// 1001發(fā)微博HBaseDao.publishWeiBo("1001","趕緊下課ba!");// 1002關(guān)注1001和1003HBaseDao.addAttends("1001","1002","1003");// 獲取1002初始化頁面HBaseDao.getInit("1002");System.out.println("----------------------------------------------");// 1003發(fā)布3條微博,同時1001發(fā)布2條微博HBaseDao.publishWeiBo("1003","誰說的趕緊下課!");Thread.sleep(10);HBaseDao.publishWeiBo("1001","我沒說話!");Thread.sleep(10);HBaseDao.publishWeiBo("1003","那誰說的!");Thread.sleep(10);HBaseDao.publishWeiBo("1002","就是不下課!");HBaseDao.publishWeiBo("1002","愛咋咋地!");System.out.println("發(fā)送消息完畢!!!");// 獲取1002初始化頁面HBaseDao.getInit("1002");// 1002取關(guān)1003HBaseDao.deleteAttends("1002","1003");// 獲取1002初始化頁面HBaseDao.getInit("1002");// 1002再次關(guān)注1003HBaseDao.addAttends("1002","1003");// 獲取1002初始化頁面HBaseDao.getInit("1002");} }3.3.4.7 測試結(jié)果
4.總結(jié)
通本次HBase的微博模擬案例,加強(qiáng)對HBase的理解和應(yīng)用,能夠把知識用于實踐,并且能熟系掌握HBase的Java API操作。
原文鏈接地址:https://www.ygs.red/article/543
訪問原文即可獲取源碼項目!!!
訪問原文即可獲取源碼項目!!!
訪問原文即可獲取源碼項目!!!
總結(jié)
以上是生活随笔為你收集整理的HBase的微博案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序源码使用反编译工具解码
- 下一篇: MATLAB之时频域乐器信号的分析与处理