用Hbase设计一个:假设你是用户部门员工,现需一张表用于存放用户粉丝数据,表:userfollow;存:follower粉丝有谁;followed关注谁。实现以下功能:1.建表userfollow2.用户Mary有一个粉丝Alice3.用户Jack有两个粉丝Bob和Ben4.Jack还关注了Mary5.查看Jack被哪些人关注,关注了谁6.Bob取关了Jack7.扫描了全表数据

时间: 2024-02-28 12:53:49 浏览: 22
好的,以下是用HBase设计的用户粉丝数据表userfollow的建表、插入数据和查询操作的代码实现: 1. 建表 首先需要创建一个Java项目,并导入HBase的相关依赖包。 然后可以使用HBaseAdmin类创建表: ```java Configuration conf = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("userfollow")); tableDesc.addFamily(new HColumnDescriptor("follower")); tableDesc.addFamily(new HColumnDescriptor("followed")); admin.createTable(tableDesc); ``` 这里定义了一个名为“userfollow”的表,包含两个列族“follower”和“followed”。 2. 插入数据 接下来可以使用HBase的Put类插入数据: ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); // 用户Mary有一个粉丝Alice Put put1 = new Put(Bytes.toBytes("Mary")); put1.add(Bytes.toBytes("follower"), Bytes.toBytes("Alice"), Bytes.toBytes("1")); table.put(put1); // 用户Jack有两个粉丝Bob和Ben Put put2 = new Put(Bytes.toBytes("Jack")); put2.add(Bytes.toBytes("follower"), Bytes.toBytes("Bob"), Bytes.toBytes("1")); put2.add(Bytes.toBytes("follower"), Bytes.toBytes("Ben"), Bytes.toBytes("1")); table.put(put2); // Jack还关注了Mary Put put3 = new Put(Bytes.toBytes("Jack")); put3.add(Bytes.toBytes("followed"), Bytes.toBytes("Mary"), Bytes.toBytes("1")); table.put(put3); ``` 这里使用了Put类的add方法,将数据插入到表中。 3. 查询操作 可以使用HBase的Get类和Scan类来进行查询操作。 (1) 查看Jack被哪些人关注,关注了谁 ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); // 查看Jack被哪些人关注 Get get1 = new Get(Bytes.toBytes("Jack")); Result result1 = table.get(get1); byte[] followed = result1.getValue(Bytes.toBytes("followed"), Bytes.toBytes("Mary")); if (followed != null) { System.out.println("Jack关注了Mary"); } // 查看Jack有哪些粉丝 Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("follower"), Bytes.toBytes("")); scan.setFilter(new PrefixFilter(Bytes.toBytes("Jack"))); ResultScanner scanner = table.getScanner(scan); for (Result result2 : scanner) { byte[] follower = result2.getRow(); System.out.println(Bytes.toString(follower) + "关注了Jack"); } scanner.close(); ``` 这里使用了Get类和Scan类,分别查询了Jack被哪些人关注和Jack有哪些粉丝。 (2) Bob取关了Jack ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); Delete delete = new Delete(Bytes.toBytes("Jack")); delete.deleteColumns(Bytes.toBytes("follower"), Bytes.toBytes("Bob")); table.delete(delete); ``` 这里使用了Delete类,将Bob从Jack的粉丝列表中删除。 (3) 扫描全表数据 ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { byte[] row = result.getRow(); byte[] follower = result.getValue(Bytes.toBytes("follower"), null); byte[] followed = result.getValue(Bytes.toBytes("followed"), null); System.out.println(Bytes.toString(row) + "的粉丝:" + Bytes.toString(follower) + ",关注:" + Bytes.toString(followed)); } scanner.close(); ``` 这里使用了Scan类,扫描了全表数据并输出结果。 以上就是用HBase设计用户粉丝数据表userfollow的建表、插入数据和查询操作的代码实现。

相关推荐

最新推荐

recommend-type

HBase入门:命名空间、建表、增删改查

注意HBase Shell 中的删除键没用,要用【Ctrl+Backspace】,每个命令之后不需要分号(;)结束。 HBase帮助命令: hbase> help 'create' 命名空间 列出所有命名空间 hbase> list_namespace 新建命名空间 hbase> ...
recommend-type

详解hbase与hive数据同步

主要介绍了详解hbase与hive数据同步的相关资料,需要的朋友可以参考下
recommend-type

HBase多表关联查找资料

HBase建表关联关系,通过hive和其他的组件,关联数据表查询,将关系型数据库的数据表在分布式集群下的重建。
recommend-type

scala API 操作hbase表

最近看了hbase的源码根据源码写了一些scala调动hbase表的API,话不多说直接上代码!Hadoop的版本是2.7.3,scala版本是2.1.1,hbase的版本是1.1.2 如果版本不同可以修改pom的依赖项,但要注意版本冲突。 并且在scala...
recommend-type

安装笔记:hadoop+hbase+sqoop2+phoenix+kerberos

利用两台云主机(真实环境至少三台)安装hadoop+hbase+sqoop2+phoenix+kerberos的过程笔记,从伪分布式到kerberos认证。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。