Apache HBase JavaAPI 查询数据
文章目录
初始化一批数据到HBase当中用于查询
package com.czxy.demo01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test03 {
public static void main(String[] args) throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put0 = new Put("0001".getBytes());
put0.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
put0.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("曹操"));
put0.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(30));
put0.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
put0.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("沛国谯县"));
put0.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("16888888888"));
put0.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("helloworld"));
Put put = new Put("0002".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(30));
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<Put> listPut = new ArrayList<Put>();
listPut.add(put);
listPut.add(put0);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuser.put(listPut);
//关闭连接
myuser.close();
connection.close();
}
}
查看插入结果:
hbase(main):006:0> scan 'myuser'
ROW COLUMN+CELL
0001 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00\x1E
0001 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x01
0001 column=f1:name, timestamp=1576570427115, value=\xE6\x9B\xB9\xE6\x93\x8D
0001 column=f2:address, timestamp=1576570427115, value=\xE6\xB2\x9B\xE5\x9B\xBD\xE8\xB0\xAF\xE5\x8
E\xBF
0001 column=f2:phone, timestamp=1576570427115, value=16888888888
0001 column=f2:say, timestamp=1576570427115, value=helloworld
0001 column=f2:sex, timestamp=1576570427115, value=1
0002 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00\x1E
0002 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x01
0002 column=f1:name, timestamp=1576570427115, value=\xE6\x9B\xB9\xE6\x93\x8D
0002 column=f2:address, timestamp=1576570427115, value=\xE6\xB2\x9B\xE5\x9B\xBD\xE8\xB0\xAF\xE5\x8
E\xBF
0002 column=f2:phone, timestamp=1576570427115, value=16888888888
0002 column=f2:say, timestamp=1576570427115, value=helloworld
0002 column=f2:sex, timestamp=1576570427115, value=1
0003 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00
0003 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x02
0003 column=f1:name, timestamp=1576570427115, value=\xE5\x88\x98\xE5\xA4\x87
0003 column=f2:address, timestamp=1576570427115, value=\xE5\xB9\xBD\xE5\xB7\x9E\xE6\xB6\xBF\xE9\x8
3\xA1\xE6\xB6\xBF\xE5\x8E\xBF
0003 column=f2:phone, timestamp=1576570427115, value=17888888888
0003 column=f2:say, timestamp=1576570427115, value=talk is cheap , show me the code
0003 column=f2:sex, timestamp=1576570427115, value=1
0004 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00#
0004 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x03
0004 column=f1:name, timestamp=1576570427115, value=\xE5\xAD\x99\xE6\x9D\x83
0004 column=f2:address, timestamp=1576570427115, value=\xE4\xB8\x8B\xE9\x82\xB3
0004 column=f2:phone, timestamp=1576570427115, value=12888888888
0004 column=f2:say, timestamp=1576570427115, value=what are you \xE5\xBC\x84\xE5\x95\xA5\xE5\x98\x
9E\xEF\xBC\x81
0004 column=f2:sex, timestamp=1576570427115, value=1
0005 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00\x1C
0005 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x04
0005 column=f1:name, timestamp=1576570427115, value=\xE8\xAF\xB8\xE8\x91\x9B\xE4\xBA\xAE
0005 column=f2:address, timestamp=1576570427115, value=\xE5\x9B\x9B\xE5\xB7\x9D\xE9\x9A\x86\xE4\xB
8\xAD
0005 column=f2:phone, timestamp=1576570427115, value=14888888888
0005 column=f2:say, timestamp=1576570427115, value=\xE5\x87\xBA\xE5\xB8\x88\xE8\xA1\xA8\xE4\xBD\xA
0\xE8\x83\x8C\xE4\xBA\x86\xE5\x98\x9B
0005 column=f2:sex, timestamp=1576570427115, value=1
0006 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00\x1B
0006 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x05
0006 column=f1:name, timestamp=1576570427115, value=\xE5\x8F\xB8\xE9\xA9\xAC\xE6\x87\xBF
0006 column=f2:address, timestamp=1576570427115, value=\xE5\x93\xAA\xE9\x87\x8C\xE4\xBA\xBA\xE6\x9
C\x89\xE5\xBE\x85\xE8\x80\x83\xE7\xA9\xB6
0006 column=f2:phone, timestamp=1576570427115, value=15888888888
0006 column=f2:say, timestamp=1576570427115, value=\xE8\xB7\x9F\xE8\xAF\xB8\xE8\x91\x9B\xE4\xBA\xA
E\xE6\xAD\xBB\xE6\x8E\x90
0006 column=f2:sex, timestamp=1576570427115, value=1
0007 column=f1:age, timestamp=1576570427115, value=\x00\x00\x00\x1C
0007 column=f1:id, timestamp=1576570427115, value=\x00\x00\x00\x05
0007 column=f1:name, timestamp=1576570427115, value=xiaobubu\xE2\x80\x94\xE5\x90\x95\xE5\xB8\x83
0007 column=f2:address, timestamp=1576570427115, value=\xE5\x86\x85\xE8\x92\x99\xE4\xBA\xBA
0007 column=f2:phone, timestamp=1576570427115, value=15788888888
0007 column=f2:say, timestamp=1576570427115, value=\xE8\xB2\x82\xE8\x9D\x89\xE5\x8E\xBB\xE5\x93\xA
A\xE4\xBA\x86
0007 column=f2:sex, timestamp=1576570427115, value=1
7 row(s) in 0.0380 seconds
按照rowkey进行查询获取所有列的所有值
查询主键rowkey为0003的人
package com.czxy.demo01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class Test04 {
public static void main(String[] args) throws IOException {
//按照rowkey进行查询获取所有列的所有值
//查询主键rowkey为0003的人
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Get get = new Get(Bytes.toBytes("0003"));
Result result = myuser.get(get);
Cell[] cells = result.rawCells();
//获取所有的列名称以及列的值
for (Cell cell : cells) {
//注意,如果列属性是int类型,那么这里就不会显示
System.out.println(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println(Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
}
myuser.close();
connection.close();
}
}
按照rowkey查询指定列族下面的指定列的值
package com.czxy.demo01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Test05 {
public static void main(String[] args)throws Exception {
//通过rowkey查询指定列族下面的指定列的值
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//通过rowKey进行查询
Get get = new Get("0003".getBytes());
get.addColumn("f1".getBytes(),"id".getBytes());
//这里只设置了查询id,所以把下面那两行注释解开,是查询不到的,查询出为null使用Bytes.toInt()会报错
Result result = myuser.get(get);
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())));
myuser.close();
connection.close();
}
}
通过startRowKey和endRowKey进行扫描
package com.czxy.demo01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Test06 {
/**
* 通过startRowKey和endRowKey进行扫描查询
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
//包括0004 0005 不包括0006
scan.setStartRow("0004".getBytes());
scan.setStopRow("0006".getBytes());
ResultScanner resultScanner = myuser.getScanner(scan);
for (Result result : resultScanner) {
//获取rowkey
System.out.println(Bytes.toString(result.getRow()));
//遍历获取得到所有的列族以及所有的列的名称
KeyValue[] raw = result.raw();
for (KeyValue keyValue : raw) {
//获取所属列族
System.out.println(Bytes.toString(keyValue.getFamilyArray(),keyValue.getFamilyOffset(),keyValue.getFamilyLength()));
System.out.println(Bytes.toString(keyValue.getQualifierArray(),keyValue.getQualifierOffset(),keyValue.getQualifierLength()));
}
//指定列族以及列打印列当中的数据出来
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
}
//关闭连接
myuser.close();
connection.close();
}
}
通过scan进行全表扫描
package com.czxy.demo01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Test07 {
/**
* 全表扫描scan
* @param args
*/
public static void main(String[] args)throws Exception {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
ResultScanner resultScanner = myuser.getScanner(scan);
for (Result result : resultScanner) {
//获取rowkey
System.out.println(Bytes.toString(result.getRow()));
//指定列族以及列打印列当中的数据出来
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
System.out.println(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
}
myuser.close();
connection.close();
}
}