使用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;使用Table table = connection.getTable(TableName.valueOf(tableName));创建表
时间: 2023-08-05 22:06:32 浏览: 282
代码如下:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseExample {
private static Configuration conf = null;
private static Connection connection = null;
static {
conf = HBaseConfiguration.create();
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
String tableName = "userfollower";
createTable(tableName);
addUserFollower("Mary", "Alice");
addUserFollower("Jack", "Bob");
addUserFollower("Jack", "Ben");
addUserFollower("Jack", "Mary");
System.out.println("Jack is followed by: " + getFollowers("Jack"));
System.out.println("Jack follows: " + getFollowed("Jack"));
removeUserFollower("Bob", "Jack");
scanTable(tableName);
}
private static void createTable(String tableName) throws IOException {
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
if (!admin.tableExists(table)) {
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(table);
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf")).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
admin.createTable(tableDescriptorBuilder.build());
}
}
private static void addUserFollower(String user, String follower) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Put put = new Put(Bytes.toBytes(user));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("follower"), Bytes.toBytes(follower));
table.put(put);
table.close();
}
private static void removeUserFollower(String user, String follower) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Delete delete = new Delete(Bytes.toBytes(user));
delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("follower"), Bytes.toBytes(follower));
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);
byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("follower"));
table.close();
return Bytes.toString(value);
}
private static String getFollowed(String user) throws IOException {
Table table = connection.getTable(TableName.valueOf("userfollower"));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("follower"));
ResultScanner resultScanner = table.getScanner(scan);
StringBuilder sb = new StringBuilder();
for (Result result : resultScanner) {
byte[] row = result.getRow();
byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("follower"));
String follower = Bytes.toString(value);
if (follower.equals(user)) {
sb.append(Bytes.toString(row)).append(" ");
}
}
resultScanner.close();
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 resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("follower"));
System.out.println(Bytes.toString(row) + " follows " + Bytes.toString(value));
}
resultScanner.close();
table.close();
}
}
```
需要注意的是,程序运行前需要先启动 HBase 的服务。
阅读全文