1.创建表myuser
Configuration configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取连接对象,创建一张表
//获取管理员对象,来对数据库进行DDL的操作
Admin admin = connection.getAdmin();
//指定我们的表名
TableName myuser = TableName.valueOf("myuser");
HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
//指定两个列族
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
admin.createTable(hTableDescriptor);
admin.close();
2.向表中添加数据
//创建put对象,并指定rowkey
Put put = new Put("0001".getBytes());
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(18));
put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("158741025aa"));
//插入数据
myuser.put(put);
3.查询数据
Get get = new Get(Bytes.toBytes("0003"));
Result result = myuser.get(get);
Cell[] cells = result.rawCells();
//获取所有的列名称以及列的值
for (Cell cell : cells) {
//注意,如果列属性是int类型,那么这里就不会显示
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id" )|| Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
按照rowkey查询指定列族下面的指定列的值
Get get=new Get("0003".getBytes());
// get.addFamily("f1".getBytes());
get.addColumn("f1".getBytes(),"name".getBytes());
Result result = myuser.get(get);
4.通过startRowKey和endRowKey进行扫描
Scan scan =new Scan();
scan.setStartRow("0002".getBytes());
scan.setStopRow("0006".getBytes());
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
System.out.println("rowkey "+Bytes.toString(result.getRow()));
/*
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"id".getBytes())));
System.out.println(Bytes.toString(result.getValue("f1".getBytes(),"name".getBytes())));
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"age".getBytes())));
*/
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id" )|| Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
}else {
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
}
5.过滤器查询
hbase过滤器的比较运算符 | 运算符 |
---|---|
LESS | < |
LESS_OR_EQUAL | <= |
EQUAL | = |
NOT_EQUAL | <> |
GREATER_OR_EQUAL | >= |
GREATER | > |
NO_OP | 排除所有 |
----------------------------------------------------------------------------------------------------------------------------
Hbase过滤器的专用过滤器 | 指定比较机制 |
---|---|
BinaryComparator | 按字节索引顺序比较指定字节数组,采用Bytes.compareTo(byte[]) |
BinaryPrefixComparator | 跟前面相同,只是比较左端的数据是否相同 |
NullComparator | 判断给定的是否为空 |
BitComparator | 按位比较 |
RegexStringComparator | 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL |
SubstringComparator | 判断提供的子串是否出现在value中 |
6.rowKey过滤器RowFilter
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("0003")));
7.列族过滤器FamilyFilte
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator("f2"));
8.列过滤器QualifierFilter
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
9.列值过滤器ValueFilter
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
10.专用过滤器 单列值过滤器SingleColumnValueFilter
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
11.列值排除过滤器SingleColumnValueExcludeFilter
12.rowkey前缀过滤器PrefixFilter
查询以00开头的所有前缀的rowkey
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
13.多过滤器综合查询FilterList
FilterList filterList = new FilterList();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
filterList.addFilter(singleColumnValueFilter);
filterList.addFilter(prefixFilter);
14.根据rowkey删除数据
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Delete delete = new Delete("0001".getBytes());
myuser.delete(delete);
myuser.close();
15.删除表操作
//获取连接
Configuration conf =new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
admin.disableTable(TableName.valueOf("myuser"));
admin.deleteTable(TableName.valueOf("myuser"));
admin.close();
文章评论