HBase Java API 操作
Tips:
其实每一个操作都可以简化为:
1、配置并连接数据库
2、编写 Java API 的 HBase 的操作
3、使用权限 执行操作
要对一个Hbase数据库进行操作的话,首先我们要进行一系列准备操作
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection conn=ConnectionFactory.createConnection(conf);
Admin admin=conn.getAdmin();
创建表
我们需要知道的是,一个表的结构由tableName(表名)、rowkey(行键)、columnFamily(列族)构成,timestamp(时间戳)。
创建一个表需要设置的 只有表名和列族。
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection conn=ConnectionFactory.createConnection(conf);
Admin admin=conn.getAdmin();
TableName tableName=TableName.valueOf("table2");
HTableDescriptor desc=new HTableDescriptor(tableName);
HColumnDescriptor family=new HColumnDescriptor("familyName1");
HColumnDescriptor family2=new HColumnDescriptor("familyName2");
desc.addFamily(family);
desc.addFamily(family2);
admin.createTable(desc);
添加数据
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection conn=ConnectionFactory.createConnection(conf);
TableName tableName=TableName.valueOf("table1");
Table table=conn.getTable(tableName);
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
Bytes.toBytes("xiaoming24"));
put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
Bytes.toBytes("33"));
put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
Bytes.toBytes("beijing"));
Put put2 = new Put(Bytes.toBytes("row8"));
put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
Bytes.toBytes("xiaoming23333"));
put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
Bytes.toBytes("30"));
put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
Bytes.toBytes("beijing2"));
Put put3 = new Put(Bytes.toBytes("row9"));
put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
Bytes.toBytes("zsh"));
put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
Bytes.toBytes("31"));
put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
Bytes.toBytes("beijing3"));
table.put(put);
table.put(put2);
table.put(put3);
table.close();
从数据库获取数据
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection conn=ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("table1"));
Get get = new Get("row1".getBytes());
Result r = table.get(get);
for (Cell cell : r.rawCells()) {
String family=new String(CellUtil.cloneFamily(cell));
String qualifier=new String(CellUtil.cloneQualifier(cell));
String value=new String(CellUtil.cloneValue(cell));
System.out.println("列:" + family+":"+qualifier + "--值:" + value);
}
获取表的描述
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.153.130:2181,192.168.153.131:2181,192.168.153.132:2181");
Connection conn=ConnectionFactory.createConnection(conf);
Admin admin=conn.getAdmin();
TableName tableName=TableName.valueOf("table2");
HTableDescriptor desc = admin.getTableDescriptor(tableName);
Object[] arr = desc.getFamilies().toArray();
for (Object o : arr) {
System.out.println(o);
}
SCAN 扫描
Configuration conf=new Configuration();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table mytable = connection.getTable(TableName.valueOf("table1"));
Scan scan=new Scan("row1".getBytes(),"".getBytes());
ResultScanner scanner = mytable.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
System.out.println();
while(iterator.hasNext()){
Result result = iterator.next();
String name = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(),
"name".getBytes())));
String address = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(), "address".getBytes())));
String age = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(), "age".getBytes())));
System.out.println(name+", "+age+", "+address);
}
connection.close();
Delete
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
Connection conn=ConnectionFactory.createConnection(conf);
TableName tableName=TableName.valueOf("t2");
Table table=conn.getTable(tableName);
Delete delete=new Delete(Bytes.toBytes("row1"));
table.delete(delete);
table.close();
Table table=conn.getTable(tableName);
Delete delete=new Delete(Bytes.toBytes("row1"));
table.delete(delete);
table.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)