Hbase的API案例实战
2021-03-19 14:24
标签:chm ase ali size The filters one htable 通过 e(delete); Hbase的API案例实战 标签:chm ase ali size The filters one htable 通过 原文地址:https://blog.51cto.com/10312890/2472152
如下内容作为maven工程中pom.xml的repositories的内容
2、创建表
/**
* 操作数据库 第一步:获取连接 第二步:获取客户端对象 第三步:操作数据库 第四步:关闭
* 创建myuser表,有两个列族 f1 f2
* @throws IOException
*/
@Test
public void createTable() throws IOException {
//获得连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//创建连接对象
Connection connection = ConnectionFactory.createConnection(configuration);
//操作:建表、删除表、修改表 -> 管理员;创建管理员对象
Admin admin = connection.getAdmin();
//添加了表名信息
HTableDescriptor myuser = new HTableDescriptor(TableName.valueOf("myuser"));
//给表添加列族
myuser.addFamily(new HColumnDescriptor("f1"));
myuser.addFamily(new HColumnDescriptor("f2"));
//创建表
admin.createTable(myuser);
//关闭连接
admin.close();
connection.close();
}
3、添加数据
private Connection connection;
private Table table;
//建立连接
@BeforeTest
public void init() throws IOException {
//获得连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//configuration.set("zookeeper.znode.parent", "/HBase");
connection = ConnectionFactory.createConnection(configuration);
//获得表
table = connection.getTable(TableName.valueOf("myuser"));
}
@AfterTest
public void close() throws IOException {
table.close();
connection.close();
}
//向表中添加数据
@Test
public void putData() throws IOException {
Put put = new Put("0001".getBytes());//创建Put对象,并指定rowkey值
//添加cell值:列族名称+列名+值
put.addColumn("f1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(18));
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(25));
put.addColumn("f1".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
table.put(put);
}
4、查询数据
@Test
public void batchInsert() throws IOException {
//创建put对象,并指定rowkey
Put put = new Put("0002".getBytes());
//向f1列族添加数据
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
//向f2列族添加数据
put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0006".getBytes());
put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0007".getBytes());
put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));
List
4.1 Get查询
//查询rowkey为0003的数据
@Test
public void getDataByRowkey() throws IOException {
//通过get对象,指定rowkey
Get get = new Get("0003".getBytes());
//获取某列族
get.addFamily("f1".getBytes());//限定查询f1列族下面所有列的值
get.addColumn("f2".getBytes(), "say".getBytes());//查询f2列族say列的值
//通过get查询,返回0003行,f1列族、f2:say列的所有cell的值,封装到一个Result对象
Result result = table.get(get);
//获得Result中的所有Cell
List
4.2 Scan查询
/**
* 不知道rowkey的具体值,我想查询rowkey范围值是0003 到0006
* select * from myuser where age > 30 and id cells = result.listCells();
for (Cell cell : cells) {
byte[] family_name = CellUtil.cloneFamily(cell);
byte[] qualifier_name = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] value = CellUtil.cloneValue(cell);
//判断id和age字段,这两个字段是整形值
if("age".equals(Bytes.toString(qualifier_name)) || "id".equals(Bytes.toString(qualifier_name))){
System.out.println("数据的rowkey为" + Bytes.toString(rowkey) +"======数据的列族为" + Bytes.toString(family_name)+"======数据的列名为" + Bytes.toString(qualifier_name) + "==========数据的值为" +Bytes.toInt(value));
}else{
System.out.println("数据的rowkey为" + Bytes.toString(rowkey) +"======数据的列族为" + Bytes.toString(family_name)+"======数据的列名为" + Bytes.toString(qualifier_name) + "==========数据的值为" +Bytes.toString(value));
}
}
}
}
5、HBase的删除操作
5.1、根据rowkey删除数据
/**
* 删除数据
*/
@Test
public void deleteData() throws IOException {
Delete delete = new Delete("0003".getBytes());
delete.addFamily("f1".getBytes());
delete.addColumn("f2".getBytes(), "phone".getBytes());
table.delete(delete);
}
5.2、删除表操作
/**
* 删除表
*/
@Test
public void deleteTable() throws IOException {
//获取管理员对象,用于表的删除
Admin admin = connection.getAdmin();
//删除一张表之前,需要先禁用表
admin.disableTable(TableName.valueOf("myuser"));
admin.deleteTable(TableName.valueOf("myuser"));
}
}
## 5.2、删除表操作
```java
/**
* 删除表
*/
@Test
public void deleteTable() throws IOException {
//获取管理员对象,用于表的删除
Admin admin = connection.getAdmin();
//删除一张表之前,需要先禁用表
admin.disableTable(TableName.valueOf("myuser"));
admin.deleteTable(TableName.valueOf("myuser"));
}