使用hbase Java api完成以下操作: 1.建表userfllower 2.用户Mary有一个粉丝Alice,Alice关注列表中也存在Mary 3.用户Jack有两个粉丝Bob和Ben 4.Jack还关注了Mary 5.查看Jack被哪些人关注,Jack关注了谁 6.Bob取关了Jack 7.扫描全表数据 导入的包
时间: 2024-02-25 21:51:41 浏览: 19
以下是使用HBase Java API完成上述操作的代码示例:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDemo {
private static Configuration conf = null;
private static Connection conn = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
createTable("userfollower", new String[] {"follower", "following"});
addFollower("Mary", "Alice");
addFollower("Jack", "Bob");
addFollower("Jack", "Ben");
addFollowing("Jack", "Mary");
getFollowers("Jack");
getFollowing("Jack");
removeFollower("Bob", "Jack");
scanTable("userfollower");
}
private static void createTable(String tableName, String[] columnFamilies) throws IOException {
Admin admin = conn.getAdmin();
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(Bytes.toBytes(tableName));
for (String columnFamily : columnFamilies) {
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily)).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
admin.createTable(tableDescriptor);
admin.close();
}
private static void addFollower(String user, String follower) throws IOException {
Table table = conn.getTable(Bytes.toBytes("userfollower"));
Put put = new Put(Bytes.toBytes(user));
put.addColumn(Bytes.toBytes("follower"), Bytes.toBytes(follower), Bytes.toBytes("1"));
table.put(put);
table.close();
}
private static void addFollowing(String user, String following) throws IOException {
Table table = conn.getTable(Bytes.toBytes("userfollower"));
Put put = new Put(Bytes.toBytes(user));
put.addColumn(Bytes.toBytes("following"), Bytes.toBytes(following), Bytes.toBytes("1"));
table.put(put);
table.close();
}
private static void getFollowers(String user) throws IOException {
Table table = conn.getTable(Bytes.toBytes("userfollower"));
Get get = new Get(Bytes.toBytes(user));
Result result = table.get(get);
System.out.println("User " + Bytes.toString(result.getRow()) + " has followers:");
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String follower = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println(follower);
}
table.close();
}
private static void getFollowing(String user) throws IOException {
Table table = conn.getTable(Bytes.toBytes("userfollower"));
Get get = new Get(Bytes.toBytes(user));
Result result = table.get(get);
System.out.println("User " + Bytes.toString(result.getRow()) + " is following:");
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String following = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println(following);
}
table.close();
}
private static void removeFollower(String user, String follower) throws IOException {
Table table = conn.getTable(Bytes.toBytes("userfollower"));
Delete delete = new Delete(Bytes.toBytes(user));
delete.addColumn(Bytes.toBytes("follower"), Bytes.toBytes(follower));
table.delete(delete);
table.close();
}
private static void scanTable(String tableName) throws IOException {
Table table = conn.getTable(Bytes.toBytes(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Row key: " + Bytes.toString(result.getRow()));
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String columnFamily = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("Column family: " + columnFamily + ", Qualifier: " + qualifier + ", Value: " + value);
}
}
table.close();
}
}
```
其中,需要导入的包有:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
```