1、创建Stu_Class表并展示所有表:

Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();

String tableName = 'Stu_Class';
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor('info'));

admin.createTable(tableDescriptor);

HBaseAdmin.listTables(config).forEach(System.out::println);

admin.close();
connection.close();

2、将数据插入表中:

Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf('Stu_Class'));

Put put = new Put(Bytes.toBytes('1001'));
put.addColumn(Bytes.toBytes('info'), Bytes.toBytes('name'), Bytes.toBytes('Tom'));
put.addColumn(Bytes.toBytes('info'), Bytes.toBytes('age'), Bytes.toBytes('25'));
put.addColumn(Bytes.toBytes('info'), Bytes.toBytes('sex'), Bytes.toBytes('male'));
put.addColumn(Bytes.toBytes('info'), Bytes.toBytes('cid'), Bytes.toBytes('3-105'));
put.addColumn(Bytes.toBytes('info'), Bytes.toBytes('cname'), Bytes.toBytes('计算机导论'));

table.put(put);

table.close();
connection.close();

3、查询表中所有数据:

hbase(main):001:0> scan 'Stu_Class'

4、修改1003行的cname为网络技术并查询修改后的数据:

hbase(main):002:0> put 'Stu_Class', '1003', 'info:cname', '网络技术'
hbase(main):003:0> get 'Stu_Class', '1003'

5、查询选修课程为3-245的学生信息:

Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf('Stu_Class'));

Scan scan = new Scan();
scan.addColumn(Bytes.toBytes('info'), Bytes.toBytes('name'));
scan.addColumn(Bytes.toBytes('info'), Bytes.toBytes('age'));
scan.addColumn(Bytes.toBytes('info'), Bytes.toBytes('sex'));
scan.addColumn(Bytes.toBytes('info'), Bytes.toBytes('cid'));
scan.addColumn(Bytes.toBytes('info'), Bytes.toBytes('cname'));

Filter filter = new SingleColumnValueFilter(Bytes.toBytes('info'), Bytes.toBytes('cid'), CompareOperator.EQUAL, Bytes.toBytes('3-245'));
scan.setFilter(filter);

ResultScanner scanner = table.getScanner(scan);

for (Result result : scanner) {
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('info'), Bytes.toBytes('name'))));
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('info'), Bytes.toBytes('age'))));
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('info'), Bytes.toBytes('sex'))));
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('info'), Bytes.toBytes('cid'))));
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('info'), Bytes.toBytes('cname'))));
}

scanner.close();
table.close();
connection.close();

原文地址: https://www.cveoy.top/t/topic/owAL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录