使用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;使用以下模板
时间: 2023-08-05 20:06:32 浏览: 38
```
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 HBaseDemo {
private static Configuration conf;
private static Connection connection;
private static Admin admin;
public static void main(String[] args) {
try {
init();
// 建表userfollower
createTable("userfollower", new String[]{"follower", "following"});
// 用户Mary有一个粉丝Alice,Alice关注列表中也存在Mary
addFollowing("Mary", "Alice");
addFollowing("Alice", "Mary");
// 用户Jack有两个粉丝Bob和Ben
addFollower("Jack", "Bob");
addFollower("Jack", "Ben");
// Jack还关注了Mary
addFollowing("Jack", "Mary");
// 查看Jack被哪些人关注,Jack关注了谁
System.out.println("Jack被哪些人关注:" + getFollowers("Jack"));
System.out.println("Jack关注了谁:" + getFollowing("Jack"));
// Bob取关了Jack
removeFollowing("Bob", "Jack");
// 扫描全表数据
scanTable("userfollower");
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
private static void init() throws IOException {
conf = HBaseConfiguration.create();
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
private static void close() {
try {
if (admin != null) {
admin.close();
}
if (connection != null) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createTable(String tableName, String[] columnFamilies) throws IOException {
if (admin.tableExists(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for (String columnFamily : columnFamilies) {
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily)).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
admin.createTable(tableDescriptorBuilder.build());
}
private static void addFollowing(String user, String following) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Put put = new Put(Bytes.toBytes(user));
put.addColumn(Bytes.toBytes("following"), Bytes.toBytes(following), Bytes.toBytes(""));
table.put(put);
table.close();
}
private static void addFollower(String user, String follower) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Put put = new Put(Bytes.toBytes(follower));
put.addColumn(Bytes.toBytes("follower"), Bytes.toBytes(user), Bytes.toBytes(""));
table.put(put);
table.close();
}
private static void removeFollowing(String user, String following) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Delete delete = new Delete(Bytes.toBytes(user));
delete.addColumn(Bytes.toBytes("following"), Bytes.toBytes(following));
table.delete(delete);
table.close();
}
private static String getFollowers(String user) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Get get = new Get(Bytes.toBytes(user));
Result result = table.get(get);
StringBuilder sb = new StringBuilder();
for (Cell cell : result.getColumnCells(Bytes.toBytes("follower"), null)) {
sb.append(Bytes.toString(CellUtil.cloneQualifier(cell))).append(",");
}
table.close();
return sb.toString();
}
private static String getFollowing(String user) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Get get = new Get(Bytes.toBytes(user));
Result result = table.get(get);
StringBuilder sb = new StringBuilder();
for (Cell cell : result.getColumnCells(Bytes.toBytes("following"), null)) {
sb.append(Bytes.toString(CellUtil.cloneQualifier(cell))).append(",");
}
table.close();
return sb.toString();
}
private static void scanTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
for (Cell cell : result.listCells()) {
System.out.println(" ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell))
+ " Column: " + Bytes.toString(CellUtil.cloneQualifier(cell))
+ " Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
}
}
```