全部課程
發(fā)布時間: 2018-02-03 01:13:23
?maven配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.togogo.hbasedemo</groupId> <artifactId>hbasedemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hbasedemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>2.8.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerVersion>1.8</compilerVersion> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project> HBaseConfiguration 包:org.apache.hadoop.hbase.HBaseConfiguration 作用:通過此類可以對HBase進行配置 用法實例: Configuration config = HBaseConfiguration.create(); 說明: HBaseConfiguration.create() 默認(rèn)會從classpath 中查找 hbase-site.xml 中的配置信息,初始化 Configuration。 使用方法: static Configuration config = null; static { config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "slave1,slave2,slave3"); config.set("hbase.zookeeper.property.clientPort", "2181"); }
Configuration conf = HBaseConfiguration.create(); @Before public void init() { conf.set("hbase.rootdir", "hdfs://192.168.195.139:9000/hbase"); // 設(shè)置Zookeeper,直接設(shè)置IP地址 conf.set("hbase.zookeeper.quorum", "192.168.195.138,192.168.195.139,192.168.195.140"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } 表描述類 HTableDescriptor 包:org.apache.hadoop.hbase.HTableDescriptor 作用:HTableDescriptor 類包含了表的名字以及表的列族信息 表的schema(設(shè)計) 用法: HTableDescriptor htd =new HTableDescriptor(tablename); htd.addFamily(new HColumnDescriptor(“myFamily”));
列族的描述類 HColumnDescriptor 包:org.apache.hadoop.hbase.HColumnDescriptor 作用:HColumnDescriptor 維護列族的信息 用法: htd.addFamily(new HColumnDescriptor(“myFamily”)); 創(chuàng)建表的操作 CreateTable(一般我們用shell創(chuàng)建表) @Test // 創(chuàng)建表 public void testCreateTable() throws Exception { String tablename = "company"; String columnFamily1 = "emp"; String columnFamily2 = "room"; Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); TableName tableNameObj = TableName.valueOf(tablename); if (admin.tableExists(tableNameObj)) { System.out.println("Table exists!"); System.exit(0); } else { HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename)); tableDesc.addFamily(new HColumnDescriptor(columnFamily1)); tableDesc.addFamily(new HColumnDescriptor(columnFamily2)); admin.createTable(tableDesc); System.out.println("create table success!"); } admin.close(); connection.close(); }
?
@Test
// 刪除表
public void testDeleteTable() {
String tableName = "company";
try {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
admin.disableTable(table);
admin.deleteTable(table);
System.out.println("delete table " + tableName + " ok.");
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
?
@Test
public void truncateTable() throws IOException{
String tableName = "company";
try {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName table = TableName.valueOf(tableName);
admin.disableTable(table);
admin.truncateTable(table, true);
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
單條插入數(shù)據(jù)Put
包:org.apache.hadoop.hbase.client.Put
作用:插入數(shù)據(jù)
用法:
Put put = new Put(row);
p.add(family,qualifier,value);
說明:向表 tablename 添加 “family,qualifier,value”指定的值。
?
// 插入一行記錄
public void addRecord(String tableName, String rowKey, String family, String qualifier, String value){
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
table.close();
connection.close();
System.out.println("insert recored " + rowKey + " to table " + tableName + " ok.");
} catch (IOException e) {
e.printStackTrace();
}
}
// 批量插入行記錄
public void addRecords(String tableName, String[] rowKeys, String family, String qualifier, String[] values){
try {
List<Put> puts = new ArrayList<Put>();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
for(int i=0;i<rowKeys.length;i++){
Put put = new Put(Bytes.toBytes(rowKeys[i]));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(values[i]));
puts.add(put);
}
table.put(puts);
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
?
刪除數(shù)據(jù)Delete
包:org.apache.hadoop.hbase.client.Delete
作用:刪除給定rowkey的數(shù)據(jù)
用法:
Delete del= new Delete(Bytes.toBytes(rowKey));
table.delete(del);
代碼實例
@Test
public void testDeleteRecord(){
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("company"));
Delete delete = new Delete(Bytes.toBytes("row2"));
table.delete(delete);
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
單條查詢Get
包:org.apache.hadoop.hbase.client.Get
作用:獲取單個行的數(shù)據(jù)
@Test
public void testQueryTableByRowKey(){
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("company"));
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = CellUtil.cloneFamily(cell);
byte[] qualifierArray = CellUtil.cloneQualifier(cell);
byte[] valueArray = CellUtil.cloneValue(cell);
System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray)
+"\t" + Bytes.toString(valueArray));
}
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
批量查詢ResultScanner
包:org.apache.hadoop.hbase.client.ResultScanner
作用:獲取值的接口
用法:
ResultScanner scanner = table.getScanner(scan);
For(Result rowResult : scanner){
Bytes[] str = rowResult.getValue(family,column);
}
說明:循環(huán)獲取行中列值。
代碼示例:
@Test
public void testScanTable(){
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("company"));
ResultScanner scanner = table.getScanner(new Scan());
for(Result result:scanner){
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = CellUtil.cloneFamily(cell);
byte[] qualifierArray = CellUtil.cloneQualifier(cell);
byte[] valueArray = CellUtil.cloneValue(cell);
System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray)
+"\t" + Bytes.toString(valueArray));
}
}
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
?? FilterList FilterList 代表一個過濾器列表,可以添加多個過濾器進行查詢,多個過濾器之間的關(guān)系有: 與關(guān)系(符合所有):FilterList.Operator.MUST_PASS_ALL 或關(guān)系(符合任一):FilterList.Operator.MUST_PASS_ONE 使用方法: FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); Scan s1 = new Scan(); filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”), CompareOp.EQUAL,Bytes.toBytes(“v1”) ) ); filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”), Bytes.toBytes(“c2”), CompareOp.EQUAL,Bytes.toBytes(“v2”) ) ); // 添加下面這一行后,則只返回指定的cell,同一行中的其他cell不返回 s1.addColumn(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”)); s1.setFilter(filterList); //設(shè)置filter ResultScanner ResultScannerFilterList = table.getScanner(s1); //返回結(jié)果列表 @Test public void queryTableByCondition(){ try { Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("company")); // 創(chuàng)建一個查詢過濾器 Filter filter = new SingleColumnValueFilter(Bytes.toBytes("emp"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("stone")); // 創(chuàng)建一個數(shù)據(jù)表掃描器 Scan scan = new Scan(); // 將查詢過濾器加入到數(shù)據(jù)表掃描器對象 scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for(Result result:scanner){ byte[] row = result.getRow(); System.out.println("row key is:" + new String(row)); List<Cell> listCells = result.listCells(); for (Cell cell : listCells) { byte[] familyArray = CellUtil.cloneFamily(cell); byte[] qualifierArray = CellUtil.cloneQualifier(cell); byte[] valueArray = CellUtil.cloneValue(cell); System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray) +"\t" + Bytes.toString(valueArray)); } } table.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } 過濾器的種類過濾器的種類: 列植過濾器—SingleColumnValueFilter 過濾列植的相等、不等、范圍等 列名前綴過濾器—ColumnPrefixFilter 過濾指定前綴的列名 多個列名前綴過濾器—MultipleColumnPrefixFilter 過濾多個指定前綴的列名 rowKey過濾器—RowFilter 通過正則,過濾rowKey值。 列植過濾器—SingleColumnValueFilterSingleColumnValueFilter 列值判斷 相等 (CompareOp.EQUAL ), 不等(CompareOp.NOT_EQUAL), 范圍 (e.g., CompareOp.GREATER)………… 下面示例檢查列值和字符串'values' 相等... SingleColumnValueFilter f = new SingleColumnValueFilter( Bytes.toBytes("cFamily") Bytes.toBytes("column"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("values")); s1.setFilter(f); 注意:如果過濾器過濾的列在數(shù)據(jù)表中有的行中不存在,那么這個過濾器對此行無法過濾。 列名前綴過濾器—ColumnPrefixFilter過濾器—ColumnPrefixFilter ColumnPrefixFilter 用于指定列名前綴值相等 ColumnPrefixFilter f = new ColumnPrefixFilter(Bytes.toBytes("values")); s1.setFilter(f); 多個列值前綴過濾器—MultipleColumnPrefixFilterMultipleColumnPrefixFilter 和 ColumnPrefixFilter 行為差不多,但可以指定多個前綴 byte[][] prefixes = new byte[][] {Bytes.toBytes("value1"),Bytes.toBytes("value2")}; Filter f = new MultipleColumnPrefixFilter(prefixes); s1.setFilter(f); rowKey過濾器—RowFilterRowFilter 是rowkey過濾器 通常根據(jù)rowkey來指定范圍時,使用scan掃描器的StartRow和StopRow方法比較好。 Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^1234")); //匹配以1234開頭的rowkey s1.setFilter(f);