maven進行項目的管理
引入下面依賴的jar包
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.11</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.4.11</version>
</dependency>
<!-- 指定JDK工具包的位置,需要本地配置好環境變量JAVA_HOME-->
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
一:建立表t2, 列族f2,代碼如下:
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
/**
* 建立表:在HBase中建立一張表
* 1.4.11版本
*
*/
public class HBaseCreateTable {
public static void main( String[] args ) throws IOException {
// 建立HBase配置對象
Configuration configuration = HBaseConfiguration.create();
// 指定zookeeper叢集位址
//configuration.set("hbase.zookeeper,quorum","node-1:1281,node-2:1281,node-3:1281"); // 使用域名一直不見建立成功輸出
configuration.set("hbase.zookeeper.quorum",
"192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 得到資料庫管理者對象
Admin admin = connection.getAdmin();
System.out.println("=========================");
// 建立描述,并指定表名
TableName tableName = TableName.valueOf("t2");
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
// 建立列族描述
HColumnDescriptor family = new HColumnDescriptor("f2");
// 指定列族
hTableDescriptor.addFamily(family);
// 建立表
admin.createTable(hTableDescriptor);
System.out.println("create table success");
admin.close();
connection.close();
}
}
二: 向表t2中添加七條資料,代碼如下:
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
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;
/**
* 向t2表中添加三條資料
*/
public class HBasePutData {
public static void main(String[] args) throws IOException {
// 建立HBase配置對象
Configuration configuration = HBaseConfiguration.create();
// 指定zookeeper叢集位址
configuration.set("hbase.zookeeper.quorum",
"192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// Table負責與記錄相關的操作,如增删改查等
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 設定rowKey
Put put1 = new Put(Bytes.toBytes("row1"));
// 添加列資料,指定列族,列名與列值
put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("小明"));
put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("30"));
put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京大學"));
put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("北京市海澱區"));
Put put2 = new Put(Bytes.toBytes("row2"));
// 添加列資料,指定列族,列名與列值
put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("張三"));
put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("20"));
put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("貴州大學"));
put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("貴州貴陽"));
Put put3 = new Put(Bytes.toBytes("row3"));
// 添加列資料,指定列族,列名與列值
put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("李四"));
put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("50"));
put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京外國語大學"));
put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("北京市昌平區"));
Put put4 = new Put(Bytes.toBytes("row4"));
// 添加列資料,指定列族,列名與列值
put4.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("王五"));
put4.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("67"));
put4.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("西安電子科技大學"));
put4.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("北京市昌平區"));
Put put5 = new Put(Bytes.toBytes("row5"));
// 添加列資料,指定列族,列名與列值
put5.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("趙六"));
put5.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("55"));
put5.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("四川音樂學院"));
put5.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("貴州貴陽市"));
Put put6 = new Put(Bytes.toBytes("row6"));
// 添加列資料,指定列族,列名與列值
put6.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("周小明"));
put6.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("32"));
put6.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京外國語大學"));
put6.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("重慶"));
Put put7 = new Put(Bytes.toBytes("row7"));
// 添加列資料,指定列族,列名與列值
put7.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("王二麻子"));
put7.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("80"));
put7.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京外國語大學聯合學院"));
put7.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("天津市"));
// 執行添加資料
table.put(put1);
table.put(put2);
table.put(put3);
table.put(put4);
table.put(put5);
table.put(put6);
table.put(put7);
// 釋放資源
table.close();
System.out.println("put data success!!");
}
}
三: 查詢資料,查詢表t2中行鍵為row1的一整條資料
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
/**
* 查詢表t1中的行鍵為row1的一行資料
*/
public class HBaseGetData {
public static void main(String[] args) throws IOException {
// 建立HBase配置對象
Configuration configuration = HBaseConfiguration.create();
// 指定zookeeper叢集位址
configuration.set("hbase.zookeeper.quorum",
"192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 擷取Table對象,指定查詢表名,Table負責與記錄相關的操作,如增删改查等
Table table = connection.getTable(TableName.valueOf("t2"));
// 建立GET對象,根據rowKey查詢,rowKey=row1
Get get = new Get("row1".getBytes());
// 查詢資料,取得結果集
Result result = table.get(get);
// 循環輸出每個單元格的資料
for(Cell cell : result.rawCells()){
// 取得目前單元格所屬的列名稱
String family = new String(CellUtil.cloneFamily(cell));
// 取得目前單元格所屬的列名稱
String qualifier = new String(CellUtil.cloneQualifier(cell));
// 取得目前單元格的列值
String value = new String(CellUtil.cloneValue(cell));
System.out.println("列:"+family+":"+qualifier+"值:"+value);
}
connection.close();
}
}
四: 删除表t2中行鍵為row3的一整條資料,代碼如下:
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
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.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 删除表t2中行鍵為row1的一整條資料
*/
public class HBaseDeleteData {
public static void main(String[] args) throws IOException {
// 指定HBase配置對象
Configuration configuration = HBaseConfiguration.create();
// 指定zookeeper叢集位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 擷取連接配接資料庫對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 擷取Table對象,指定表名,Table負責與記錄相關的操作,如增删改查等
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
//建立删除對象Delete,根據rowkey删除一整條
Delete delete = new Delete(Bytes.toBytes("row3"));
table.delete(delete);
// 釋放資源
table.close();
connection.close();
System.out.println("delete data success!!");
}
}
五,各種過濾器使用
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 行鍵過濾器: 行鍵過濾器是通過一定的規則過濾行鍵,達到篩選資料的目的
* 使用二進制比較器BinaryComparator結合運算符可以篩選出具有某個行鍵的行,
* 或者通過改變比較運算符來篩選出行鍵符合某一條件的多條資料。
*/
public class HBaseRowFilterData { // 1528
public static void main(String[] args) throws IOException {
// 建立HBase配置對象
Configuration configuration = HBaseConfiguration.create();
// 設定Zookeeper叢集位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 擷取資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 擷取Table對象,指定表名,Table負責與記錄相關的操作,如增删改查等,
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立scan對象
Scan scan = new Scan();
// 建立一個過濾器,篩選行鍵等于row1的資料
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new
BinaryComparator(Bytes.toBytes("row1"))); // 二進制比較器
// 設定過濾器
scan.setFilter(filter);
// 查詢資料,傳回結果資料集
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner){
byte[] name= result.getValue("f2".getBytes(),"name".getBytes());
byte[] age= result.getValue("f2".getBytes(),"age".getBytes());
byte[] college= result.getValue("f2".getBytes(),"college".getBytes());
byte[] address= result.getValue("f2".getBytes(),"address".getBytes());
System.out.println("name:"+new String(name)+"\tage:"+new String(age)+"\tcollege:"+new String(college)+"\taddress:"+new String(address));
}
resultScanner.close();
connection.close();
}
}
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 列過濾器: 列過濾器是通過列進行篩選,進而得到符合條件的所有資料
* 該例子,篩選出包含列name的所有資料。在HBase中,不同的行可以有不同的列,是以允許根據列進行篩選。
*/
public class HBaseQualiferData {
public static void main(String[] args) throws IOException {
// 建立配置對象
Configuration configuration = HBaseConfiguration.create();
// 設定Zookeeper的叢集位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 建立Table對象,table對象負責與記錄進行互動,比如增删改查等操作
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立Scan對象
Scan scan = new Scan();
// 建立過濾器
Filter filter =
new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("name")));
scan.setFilter(filter);
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
List<Cell> cells = result.listCells();
for(Cell cell:cells){
String row = Bytes.toString(result.getRow());// 獲得一行字元串資料
String family =Bytes.toString(CellUtil.cloneFamily(cell));// 獲得列族
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 獲得辨別符
String value = Bytes.toString(CellUtil.cloneValue(cell)); // 獲得單元格的值
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"]"+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
resultScanner.close();
connection.close();
}
}
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 單列值過濾器: 單列值過濾器是通過對某一列的值進行篩選,進而得到符合條件的所有資料.
* 例如,篩選出name列的值不包含"小明"的所有資料,
* 該例子使用不等于運算符NOT_EQUAL和字元串包含比較符SubstringComparator
*/
public class HBaseSingleColumnFilterData {
public static void main(String[] args) throws IOException {
// 建立配置對象
Configuration configuration = HBaseConfiguration.create();
// 設定zookeeper的叢集位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 建立Table對象
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立Scan對象
Scan scan = new Scan();
// 建立單例值過濾器
Filter filter =
new SingleColumnValueFilter(Bytes.toBytes("f2"),Bytes.toBytes("name"),
CompareFilter.CompareOp.NOT_EQUAL,new SubstringComparator("小明"));
scan.setFilter(filter);
// 查詢資料,傳回結果資料集
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
List<Cell> cells = result.listCells();
for(Cell cell:cells){
String row = Bytes.toString(result.getRow());// 獲得一行字元串資料
String family =Bytes.toString(CellUtil.cloneFamily(cell));// 獲得列族
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 獲得辨別符
String value = Bytes.toString(CellUtil.cloneValue(cell)); // 獲得單元格的值
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"]"+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
resultScanner.close();
connection.close();
}
}
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 值過濾器: 值過濾器是通過對單元格中的值進行篩選,
* 進而得到符合條件的所有單元格的資料:如篩選出包含"小明"的所有單元格的資料
*/
public class HBaseValueFilterData {
public static void main(String[] args) throws IOException {
// 建立配置對象
Configuration configuration = HBaseConfiguration.create();
// 設定zookeeper的位址
configuration.set("hbase.zookeeper.quorum", "192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 建立Table對象,table對象負責與資料記錄進行互動
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立掃描對象scan
Scan scan = new Scan();
// 建立值過濾器
Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("小明"));
scan.setFilter(filter);
// 查詢資料,傳回結果資料集
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
List<Cell> cells = result.listCells();
for(Cell cell:cells){
String row = Bytes.toString(result.getRow());// 獲得一行字元串資料
String family =Bytes.toString(CellUtil.cloneFamily(cell));// 獲得列族
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 獲得辨別符
String value = Bytes.toString(CellUtil.cloneValue(cell)); // 獲得單元格的值
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"]"+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
resultScanner.close();
connection.close();
}
}
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 列族過濾器: 列族過濾器是通過列族進行篩選,進而得到符合條件的所有列族資料.
* 該程式篩選出列族為f2的所有資料
*/
public class HBaseFamilyFilterData {
public static void main(String[] args) throws IOException {
// 建立配置對象
Configuration configuration = HBaseConfiguration.create();
// 設定zookeeper叢集位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 建立Table對象,指定表名,Table負責與記錄相關的操作,如增删改查等
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立scan對象
Scan sc = new Scan();
// 建立一個過濾器,篩選列族為f2的所有資料
Filter filter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("f2")));
sc.setFilter(filter);
ResultScanner resultScanner = table.getScanner(sc);
for(Result result : resultScanner){
List<Cell> cells = result.listCells();
for(Cell cell:cells){
String row = Bytes.toString(result.getRow());// 獲得一行字元串資料
String family =Bytes.toString(CellUtil.cloneFamily(cell));// 獲得列族
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 獲得辨別符
String value = Bytes.toString(CellUtil.cloneValue(cell)); // 獲得單元格的值
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"]"+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
resultScanner.close();
connection.close();
}
}
package org.jy.data.yh.bigdata.drools.hadoop.hbase;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* 多條件組合查詢: 多條件過濾
* 多條件過濾,即将多條件過濾器組合進行查詢。
* 例如下面的例子,使用單列值過濾器篩選出年齡在18到30歲之間的所有資料
*/
public class HBaseMultiesConditionData {
public static void main(String[] args) throws IOException {
// 建立配置對象
Configuration configuration = HBaseConfiguration.create();
// 配置zookeeper的位址
configuration.set("hbase.zookeeper.quorum","192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
// 建立資料庫連接配接對象
Connection connection = ConnectionFactory.createConnection(configuration);
// 建立Table對象
TableName tableName = TableName.valueOf("t2");
Table table = connection.getTable(tableName);
// 建立Scan對象
Scan scan = new Scan();
// 建立過濾器1,查詢年齡小于等于30歲的所有資料
Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("f2"),Bytes.toBytes("age"),
CompareFilter.CompareOp.LESS_OR_EQUAL,Bytes.toBytes("50"));
// 建立過濾器2,查詢年齡大于等于18歲的所有資料
Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("f2"),Bytes.toBytes("age"),
CompareFilter.CompareOp.GREATER_OR_EQUAL,Bytes.toBytes("18"));
// 建立過濾器集合對象
FilterList filterList = new FilterList();
filterList.addFilter(filter1);
filterList.addFilter(filter2);
// 設定過濾器
scan.setFilter(filterList);
// 查詢資料,傳回結果資料集
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
List<Cell> cells = result.listCells();
for(Cell cell:cells){
String row = Bytes.toString(result.getRow());// 獲得一行字元串資料
String family =Bytes.toString(CellUtil.cloneFamily(cell));// 獲得列族
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); // 獲得辨別符
String value = Bytes.toString(CellUtil.cloneValue(cell)); // 獲得單元格的值
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"]"+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
resultScanner.close();
connection.close();
}
}
結尾: 各基本操作可以在HBase Shell中檢視效果