文章目錄
初始化一批數據到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();
}
}