使用hbase Java api完成以下操作: 1.建表userfollower 2.用户Mary有一个粉丝Alice,Alice关注列表中也存在Mary 3.用户Jack有两个粉丝Bob和Ben 4.Jack还关注了Mary 5.查看Jack被哪些人关注,Jack关注了谁 6.Bob取关了Jack 7.扫描全表数据 导入以下包完成import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.;import org.apache.hadoop.hbase.client.;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;人名不仅可以作为value也可以作为列名
时间: 2023-08-05 20:06:32 浏览: 78
以下是使用HBase Java API完成上述操作的代码:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseExample {
private static final String TABLE_NAME = "userfollower";
private static final String COLUMN_FAMILY = "cf";
private static final String USER_QUALIFIER = "user";
private static final String FOLLOWER_QUALIFIER = "follower";
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
// 建表userfollower
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
tableDescriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY));
admin.createTable(tableDescriptor);
// 用户Mary有一个粉丝Alice,Alice关注列表中也存在Mary
addUserFollower("Mary", "Alice", connection);
// 用户Jack有两个粉丝Bob和Ben
addUserFollower("Jack", "Bob", connection);
addUserFollower("Jack", "Ben", connection);
// Jack还关注了Mary
addFollowing("Jack", "Mary", connection);
// 查看Jack被哪些人关注,Jack关注了谁
System.out.println("Jack被哪些人关注:" + getFollowers("Jack", connection));
System.out.println("Jack关注了谁:" + getFollowing("Jack", connection));
// Bob取关了Jack
removeFollowing("Bob", "Jack", connection);
// 扫描全表数据
scanTable(connection);
admin.close();
connection.close();
}
private static void addUserFollower(String user, String follower, Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Put put = new Put(Bytes.toBytes(user));
put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER), Bytes.toBytes(follower));
table.put(put);
table.close();
}
private static void addFollowing(String user, String following, Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Put put = new Put(Bytes.toBytes(following));
put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER), Bytes.toBytes(user));
table.put(put);
table.close();
}
private static String getFollowers(String user, Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Get get = new Get(Bytes.toBytes(user));
Result result = table.get(get);
String followers = Bytes.toString(result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER)));
table.close();
return followers;
}
private static String getFollowing(String user, Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER));
ResultScanner resultScanner = table.getScanner(scan);
String following = "";
for (Result result : resultScanner) {
String followingUser = Bytes.toString(result.getRow());
String[] followings = Bytes.toString(result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER))).split(",");
for (String followingName : followings) {
if (followingName.equals(user)) {
following += followingUser + ",";
}
}
}
table.close();
return following;
}
private static void removeFollowing(String follower, String following, Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Delete delete = new Delete(Bytes.toBytes(following));
delete.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER), Bytes.toBytes(follower));
table.delete(delete);
table.close();
}
private static void scanTable(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
String user = Bytes.toString(result.getRow());
String followers = Bytes.toString(result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER)));
String[] followings = Bytes.toString(result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER))).split(",");
System.out.println("User: " + user + ", Followers: " + followers + ", Followings: " + String.join(",", followings));
}
table.close();
}
}
```
需要注意的是,这里使用了一个列作为value,另一个列作为列名的方式。在列族cf下,有两个列,一个是user,存储用户的关注列表;另一个是follower,存储用户的粉丝列表。这种方式可以方便地查询某个用户的关注列表和粉丝列表。
阅读全文