使用hbase Java api完成以下操作: 1.建表userfllower 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;
时间: 2023-08-05 13:04:16 浏览: 123
Hbase笔记 —— 利用JavaAPI的方式操作Hbase数据库(往hbase的表中批量插入数据).pdf
5星 · 资源好评率100%
以下是使用 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();
// 1.建表userfollower
createTable(admin);
// 2.用户Mary有一个粉丝Alice,Alice关注列表中也存在Mary
addUserFollower(connection, "Mary", "Alice");
addUserFollower(connection, "Alice", "Mary");
// 3.用户Jack有两个粉丝Bob和Ben
addUserFollower(connection, "Jack", "Bob");
addUserFollower(connection, "Jack", "Ben");
// 4.Jack还关注了Mary
addUserFollower(connection, "Jack", "Mary");
// 5.查看Jack被哪些人关注,Jack关注了谁
scanUserFollowers(connection, "Jack");
// 6.Bob取关了Jack
removeUserFollower(connection, "Bob", "Jack");
// 7.扫描全表数据
scanTable(connection);
admin.close();
connection.close();
}
private static void createTable(Admin admin) throws IOException {
if (!admin.tableExists(TableName.valueOf(TABLE_NAME))) {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
HColumnDescriptor columnDescriptor = new HColumnDescriptor(COLUMN_FAMILY);
tableDescriptor.addFamily(columnDescriptor);
admin.createTable(tableDescriptor);
}
}
private static void addUserFollower(Connection connection, String user, String follower) 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(USER_QUALIFIER), Bytes.toBytes(user));
put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER), Bytes.toBytes(follower));
table.put(put);
table.close();
}
private static void removeUserFollower(Connection connection, String user, String follower) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Delete delete = new Delete(Bytes.toBytes(user));
delete.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER), Bytes.toBytes(follower));
table.delete(delete);
table.close();
}
private static void scanUserFollowers(Connection connection, String user) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER));
scan.setRowPrefixFilter(Bytes.toBytes(user));
ResultScanner scanner = table.getScanner(scan);
System.out.println(user + " 的粉丝:");
for (Result result : scanner) {
byte[] followerBytes = result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER));
String follower = Bytes.toString(followerBytes);
System.out.println(follower);
}
scanner.close();
scan = new Scan();
scan.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER));
scan.setRowPrefixFilter(Bytes.toBytes(user));
scanner = table.getScanner(scan);
System.out.println(user + " 关注的人:");
for (Result result : scanner) {
byte[] userBytes = result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER));
String followedUser = Bytes.toString(userBytes);
System.out.println(followedUser);
}
scanner.close();
table.close();
}
private static void scanTable(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] userBytes = result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(USER_QUALIFIER));
String user = Bytes.toString(userBytes);
byte[] followerBytes = result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(FOLLOWER_QUALIFIER));
String follower = Bytes.toString(followerBytes);
System.out.println(user + " 的粉丝:" + follower);
}
scanner.close();
table.close();
}
}
```
阅读全文