给大家详细介绍一个常用HBASEAPI
废话不多说 ,代码如下!
* 创建结构表
* createtable();
static void createtable() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
// 实例Admin管理员 创建 删除hbase表时需要的admin
Admin admin = connection.getAdmin();
// 实例表名和表结构
// 表名
TableName tableName = TableName.valueOf("myuser2");
// 设置表结构
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
// 设置列族
HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("df1");
HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("df2");
// 建立表与列族之间的关系
hTableDescriptor.addFamily(hColumnDescriptor1);
hTableDescriptor.addFamily(hColumnDescriptor2);
admin.createTable(hTableDescriptor);
admin.close();
// 关闭连接
connection.close();
}
向表中添加数据
putdata();
static void putdata() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
// 实例数据
Put put = new Put("0001".getBytes());
put.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("zhangsan"));
put.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes("0001"));
put.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes("19"));
put.addColumn("df1".getBytes(), "sex".getBytes(), Bytes.toBytes("男"));
myuser.put(put);
connection.close();
}
添加多条数据
* putdatas();
static void putdatas() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Put put = new Put("0002".getBytes());
put.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
put.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("曹操"));
put.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(30));
put.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("沛国谯县"));
put.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("16888888888"));
put.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(2));
put2.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("刘备"));
put2.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(32));
put2.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put2.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("17888888888"));
put2.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(3));
put3.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("孙权"));
put3.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(35));
put3.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put3.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("下邳"));
put3.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("12888888888"));
put3.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(4));
put4.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("诸葛亮"));
put4.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(28));
put4.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put4.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("四川隆中"));
put4.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("14888888888"));
put4.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0005".getBytes());
put5.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(5));
put5.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("司马懿"));
put5.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(27));
put5.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put5.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("哪里人有待考究"));
put5.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("15888888888"));
put5.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0006".getBytes());
put6.addColumn("df1".getBytes(), "id".getBytes(), Bytes.toBytes(5));
put6.addColumn("df1".getBytes(), "name".getBytes(), Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("df1".getBytes(), "age".getBytes(), Bytes.toBytes(28));
put6.addColumn("df2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put6.addColumn("df2".getBytes(), "address".getBytes(), Bytes.toBytes("内蒙人"));
put6.addColumn("df2".getBytes(), "phone".getBytes(), Bytes.toBytes("15788888888"));
put6.addColumn("df2".getBytes(), "say".getBytes(), Bytes.toBytes("貂蝉去哪了"));
List<Put> list = new ArrayList<Put>();
list.add(put);
list.add(put2);
list.add(put3);
list.add(put4);
list.add(put5);
list.add(put6);
myuser.put(list);
connection.close();
}
* 获取数据
* getdatas();
static void getdatas() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
// 获取报表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
// 设置要读取的数据
Get get = new Get("0004".getBytes());
// #####查询某一个列族的数据
// get.addFamily("df1".getBytes());
// #####某一个列族下的特定列 查询
// get.addColumn("df1".getBytes(), "name".getBytes());
// 读取数据 一个result 就是一行数据
Result result = myuser.get(get);
// 遍历数据
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.print(Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
System.out.print(Bytes.toInt(CellUtil.cloneValue(cell)) + "\t");
} else {
// System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
System.out.print(Bytes.toString(CellUtil.cloneValue(cell)) + "\t");
}
}
myuser.close();
connection.close();
}
* 全表扫描
* scandatas();
static void scandatas() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
String rowkey = Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue("df1".getBytes(), "name".getBytes()));
System.out.println(rowkey + " " + name);
}
myuser.close();
connection.close();
}
* 设置区间扫描
* rowkeyFilter();
/**
* hbase行键过滤器RowFilter
* hbase列族过滤器FamilyFilter
* hbase列过滤器
* hbase值过滤器
* 查询包含8的列值
*/
static void rowkeyFilter() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
// scan.setStartRow("1".getBytes());
// scan.setStopRow("100".getBytes());
// RowFilter rowFilter = new RowFilter(LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("0005"))); rowKey
// FamilyFilter familyFilter = new FamilyFilter(EQUAL, new BinaryComparator(Bytes.toBytes("df2"))); 列族
// QualifierFilter qualifierFilter = new QualifierFilter(EQU AL, new BinaryComparator(Bytes.toBytes("name"))); 整列
ValueFilter valueFilter = new ValueFilter(EQUAL, new SubstringComparator("8")); // 列值
scan.setFilter(valueFilter);
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
String rowkey = Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue("df1".getBytes(), "name".getBytes()));
String age = Bytes.toString(result.getValue("df2".getBytes(), "phone".getBytes()));
String id = Bytes.toString(result.getValue("df1".getBytes(), "id".getBytes()));
System.out.println(rowkey + " " + id + " " + age + " " + name);
}
myuser.close();
connection.close();
}
* 单列值过滤器,返回满足条件的整行数据; (列值排除过滤器,与SingleColumnFilter相反,会排除指定的 列,其他的列全部返回)
* singleColumnFilter();
static void singleColumnFilter() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), EQUAL, "刘备".getBytes());
scan.setFilter(singleColumnValueFilter);
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
// 获取rowkey
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toInt(result.getValue("df1".getBytes(), "id".getBytes())));
System.out.println(Bytes.toString(result.getValue("df1".getBytes(), "name".getBytes())));
System.out.println(Bytes.toString(result.getValue("df2".getBytes(), "phone".getBytes())));
}
myuser.close();
connection.close();
}
* 查询以00开头的所有前缀的rowkey
* preFilter();
static void preFilter() throws IOException {
//获取连接
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
ResultScanner resultScanner = myuser.getScanner(scan);
for (Result result : resultScanner) {
//获取rowkey
System.out.println(Bytes.toString(result.getRow()));
//指定列族以及列打印列当中的数据出来
System.out.println(Bytes.toInt(result.getValue("df1".getBytes(), "id".getBytes())));
System.out.println(Bytes.toString(result.getValue("df1".getBytes(), "name".getBytes())));
System.out.println(Bytes.toString(result.getValue("df2".getBytes(), "phone".getBytes())));
;
}
myuser.close();
connection.close();
}
* 根据rowkey删除数据
* deleteByRowKey()
static void deleteByRowKey() throws IOException {
//获取连接
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(conf);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Delete delete = new Delete("0001".getBytes());
myuser.delete(delete);
myuser.close();
connection.close();
}
* 删除表操作
* deleteTable() ;
static void deleteTable() throws IOException {
//获取连接
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();
connection.close();
}
今天的分享到此结束~ 感谢捧场