hbase开发实例

时间:2024-01-09 15:53:44

1、put/checkAndPut

 package com.testdata;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes; public class TestPut { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf); HTableInterface table = conn.getTable("testdata");
Put testput = new Put(Bytes.toBytes("row1"));
testput.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("E"));
table.put(testput);
//使用checkAndPut
table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf"),Bytes.toBytes("col5"),Bytes.toBytes("E"),testput);
table.close();
conn.close(); } }

使用checkAndPut,需要先对数据进行验证,上面的例子中,向row1中的cf:col1写入数据"E",而验证的是row1中的cf:col5的值是否为"E",注意这一点,相当于加了条件。

hbase开发实例

2、使用get读取数据

 package com.testdata;

 import java.io.IOException;
import java.util.List; 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.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; public class TestGet { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Get testget = new Get(Bytes.toBytes("row1"));
Result row1 = table.get(testget);
String value = new String(row1.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col1")));
System.out.println(value);
//下面限定到具体的列
testget.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col2"));
Result result = table.get(testget);
if(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")) != null){
String value2 = new String(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")));
System.out.println(value2);
}
//另外一种读取方式
List<Cell> cells = row1.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); } //注意要关闭
table.close();
conn.close(); } }

参考结果:

hbase开发实例

3、使用scan获取数据

 package com.testdata;

 import java.io.IOException;
import java.util.List;
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.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan; public class TestScan { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan testscan =new Scan();
ResultScanner rs = table.getScanner(testscan);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); }
}
rs.close();
table.close();
conn.close(); } }

4、delete/checkAndDelete

 package com.testdata;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import java.io.IOException; import org.apache.hadoop.hbase.util.Bytes; public class TestDelete { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Delete testdelete = new Delete(Bytes.toBytes("row1"));
testdelete.deleteColumns(Bytes.toBytes("cf"), Bytes.toBytes("col1"));
//区别只是checkAndDelete需要进行验证,相当于加了前提条件
//table.delete(testdelete);
table.checkAndDelete(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("col2"),Bytes.toBytes("BC"), testdelete);
table.close();
conn.close(); } }

5、append

 package com.testdata;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Append;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.util.Bytes; public class TestAppend { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Append testappend = new Append(Bytes.toBytes("row1"));
testappend.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("F"));
table.append(testappend);
table.close();
conn.close();
} }

下面是结果,注意append是在原有的值之上附加,先前的值为"E",现在变为"EF"

hbase开发实例

 6、计数器

计数器可以用于统计用户数,点击量等信息

 package com.testdata;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.util.Bytes; public class TestIncrement { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); long result = table.incrementColumnValue(Bytes.toBytes("row1"),Bytes.toBytes("cf"),Bytes.toBytes("coli"), 10); System.out.println(result);
table.close();
conn.close(); } }

注意 long result = table.incrementColumnValue(Bytes.toBytes("row1"),Bytes.toBytes("cf"),Bytes.toBytes("coli"), 10);

最后一个参数,可以为0,意味着读取,也可以是负数。

可以使用get_counter可以获取对应的计数器的值,也可以使用以下命令进行操作

 incr '<table>', '<row>', '<column>', |<increment-value>|

hbase开发实例

7、filter

使用时注意性能

 package com.testdata;

 import java.io.IOException;
import java.util.List;
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.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
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.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes; public class TestSimplefilter { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();
sc.setCacheBlocks(false);
//行过滤器,判断"row1"与行的key是否相等
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row1")));
//是否以"row"为前缀
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("row")));
//是否包含"row"
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("row")); //列过滤器,与行类似
Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("col1"))); sc.setFilter(filter); ResultScanner rs = table.getScanner(sc);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);
}
}
rs.close();
table.close();
conn.close();
}
}

使用filterlist

 package com.testdata;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
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.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes; public class TestFilterList { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();
Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row2")));
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("B"))); SingleColumnValueFilter filter3 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new RegexStringComparator("B|C"));
filter2.setFilterIfMissing(true);
filter3.setFilterIfMissing(true); //List<Filter> filters = new ArrayList<Filter>();
//filters.add(filter1);
//filters.add(filter2);
//FilterList filterlist = new FilterList(filters); //也可以这样写,MUST_PASS_ALL标识满足所有的filter,当然也可以使用MUST_PASS_ONE,标识只需要满足一个
FilterList filterlist = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterlist.addFilter(filter1);
filterlist.addFilter(filter2);
filterlist.addFilter(filter3);
sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"));
sc.setFilter(filterlist); ResultScanner rs = table.getScanner(sc);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);
}
}
rs.close();
table.close();
conn.close();
}
}

以上一组filter标识了这样的条件,即行的key必须为"row2",列名必须为"col1",值必须为"B"

结果参考:

如果没有 sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"));这一句,结果会是下面的样子

rowkey:row2 family:cf column:col1 value:B
rowkey:row2 family:cf column:colb value:U

问题出在 SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("B")));这一句,如果打印Bytes.toBytes("B")与Bytes.toBytes("U"),会发现都是以"B"开头的。即使换成BinaryComparator,也不会解决问题。

这里是值得注意的地方,搜索网络可以发现一样的结论,使用时务必使用sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"))类似的语句。

rowkey:row2 family:cf column:col1 value:B