hbase开发实例
1、put/checkAndPut
package com.testdata; import java.io.IOException;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.client.Put;<br/> import org.apache.hadoop.hbase.util.Bytes; public class TestPut { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf); HTableInterface table = conn.getTable("testdata");<br/> Put testput = new Put(Bytes.toBytes("row1"));<br/> testput.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("E"));<br/> table.put(testput);<br/> //使用checkAndPut<br/> table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf"),Bytes.toBytes("col5"),Bytes.toBytes("E"),testput);<br/> table.close();<br/> conn.close(); } }
使用checkAndPut,需要先对数据进行验证,上面的例子中,向row1中的cf:col1写入数据”E”,而验证的是row1中的cf:col5的值是否为”E”,注意这一点,相当于加了条件。
2、使用get读取数据
package com.testdata; import java.io.IOException;<br/> import java.util.List; import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.Cell;<br/> import org.apache.hadoop.hbase.CellUtil;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.Get;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> 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 {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Get testget = new Get(Bytes.toBytes("row1"));<br/> Result row1 = table.get(testget);<br/> String value = new String(row1.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col1")));<br/> System.out.println(value);<br/> //下面限定到具体的列<br/> testget.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col2"));<br/> Result result = table.get(testget);<br/> if(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")) != null){<br/> String value2 = new String(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")));<br/> System.out.println(value2);<br/> }<br/> //另外一种读取方式<br/> List<Cell> cells = row1.listCells();<br/> for(Cell cell : cells){<br/> String rowkey = new String(CellUtil.cloneRow(cell));<br/> String family = new String(CellUtil.cloneFamily(cell));<br/> String collumn = new String(CellUtil.cloneQualifier(cell));<br/> String cvalue = new String(CellUtil.cloneValue(cell));<br/> System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); } //注意要关闭<br/> table.close();<br/> conn.close(); } }
参考结果:
3、使用scan获取数据
package com.testdata; import java.io.IOException;<br/> import java.util.List;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.Cell;<br/> import org.apache.hadoop.hbase.CellUtil;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.client.Result;<br/> import org.apache.hadoop.hbase.client.ResultScanner;<br/> import org.apache.hadoop.hbase.client.Scan; public class TestScan { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Scan testscan =new Scan();<br/> ResultScanner rs = table.getScanner(testscan);<br/> for(Result r : rs ){<br/> List<Cell> cells = r.listCells();<br/> for(Cell cell : cells){<br/> String rowkey = new String(CellUtil.cloneRow(cell));<br/> String family = new String(CellUtil.cloneFamily(cell));<br/> String collumn = new String(CellUtil.cloneQualifier(cell));<br/> String cvalue = new String(CellUtil.cloneValue(cell));<br/> System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); }<br/> }<br/> rs.close();<br/> table.close();<br/> conn.close(); } }
4、delete/checkAndDelete
package com.testdata; import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.Delete;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import java.io.IOException; import org.apache.hadoop.hbase.util.Bytes; public class TestDelete { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Delete testdelete = new Delete(Bytes.toBytes("row1"));<br/> testdelete.deleteColumns(Bytes.toBytes("cf"), Bytes.toBytes("col1"));<br/> //区别只是checkAndDelete需要进行验证,相当于加了前提条件<br/> //table.delete(testdelete);<br/> table.checkAndDelete(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("col2"),Bytes.toBytes("BC"), testdelete);<br/> table.close();<br/> conn.close(); } }
5、append
package com.testdata; import java.io.IOException;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.Append;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.util.Bytes; public class TestAppend { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Append testappend = new Append(Bytes.toBytes("row1"));<br/> testappend.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("F"));<br/> table.append(testappend);<br/> table.close();<br/> conn.close();<br/> } }
下面是结果,注意append是在原有的值之上附加,先前的值为”E”,现在变为”EF”
6、计数器
计数器可以用于统计用户数,点击量等信息
package com.testdata; import java.io.IOException;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.util.Bytes; public class TestIncrement { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); long result = table.incrementColumnValue(Bytes.toBytes("row1"),Bytes.toBytes("cf"),Bytes.toBytes("coli"), 10); System.out.println(result);<br/> table.close();<br/> 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>|
7、filter
使用时注意性能
package com.testdata; import java.io.IOException;<br/> import java.util.List;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.Cell;<br/> import org.apache.hadoop.hbase.CellUtil;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.client.Result;<br/> import org.apache.hadoop.hbase.client.ResultScanner;<br/> import org.apache.hadoop.hbase.client.Scan;<br/> import org.apache.hadoop.hbase.filter.BinaryComparator;<br/> import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;<br/> import org.apache.hadoop.hbase.filter.CompareFilter;<br/> import org.apache.hadoop.hbase.filter.Filter;<br/> import org.apache.hadoop.hbase.filter.QualifierFilter;<br/> import org.apache.hadoop.hbase.filter.RowFilter;<br/> import org.apache.hadoop.hbase.filter.SubstringComparator;<br/> import org.apache.hadoop.hbase.util.Bytes; public class TestSimplefilter { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();<br/> sc.setCacheBlocks(false);<br/> //行过滤器,判断"row1"与行的key是否相等<br/> //Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row1")));<br/> //是否以"row"为前缀<br/> //Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("row")));<br/> //是否包含"row"<br/> //Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("row")); //列过滤器,与行类似<br/> Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("col1"))); sc.setFilter(filter); ResultScanner rs = table.getScanner(sc);<br/> for(Result r : rs ){<br/> List<Cell> cells = r.listCells();<br/> for(Cell cell : cells){<br/> String rowkey = new String(CellUtil.cloneRow(cell));<br/> String family = new String(CellUtil.cloneFamily(cell));<br/> String collumn = new String(CellUtil.cloneQualifier(cell));<br/> String cvalue = new String(CellUtil.cloneValue(cell));<br/> System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);<br/> }<br/> }<br/> rs.close();<br/> table.close();<br/> conn.close();<br/> }<br/> }
使用filterlist
package com.testdata; import java.io.IOException;<br/> import java.util.ArrayList;<br/> import java.util.List;<br/> import org.apache.hadoop.conf.Configuration;<br/> import org.apache.hadoop.hbase.Cell;<br/> import org.apache.hadoop.hbase.CellUtil;<br/> import org.apache.hadoop.hbase.HBaseConfiguration;<br/> import org.apache.hadoop.hbase.client.HConnection;<br/> import org.apache.hadoop.hbase.client.HConnectionManager;<br/> import org.apache.hadoop.hbase.client.HTableInterface;<br/> import org.apache.hadoop.hbase.client.Result;<br/> import org.apache.hadoop.hbase.client.ResultScanner;<br/> import org.apache.hadoop.hbase.client.Scan;<br/> import org.apache.hadoop.hbase.filter.BinaryComparator;<br/> import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;<br/> import org.apache.hadoop.hbase.filter.CompareFilter;<br/> import org.apache.hadoop.hbase.filter.Filter;<br/> import org.apache.hadoop.hbase.filter.FilterList;<br/> import org.apache.hadoop.hbase.filter.QualifierFilter;<br/> import org.apache.hadoop.hbase.filter.RegexStringComparator;<br/> import org.apache.hadoop.hbase.filter.RowFilter;<br/> import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;<br/> import org.apache.hadoop.hbase.filter.SubstringComparator;<br/> import org.apache.hadoop.hbase.util.Bytes; public class TestFilterList { public static void main(String[] args) throws IOException {<br/> Configuration conf = HBaseConfiguration.create();<br/> conf.set("hbase.zookeeper.quorum","localhost");<br/> conf.set("hbase.zookeeper.property.clientPort","2181");<br/> HConnection conn = HConnectionManager.createConnection(conf);<br/> HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();<br/> Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row2")));<br/> 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"));<br/> filter2.setFilterIfMissing(true);<br/> filter3.setFilterIfMissing(true); //List<Filter> filters = new ArrayList<Filter>();<br/> //filters.add(filter1);<br/> //filters.add(filter2);<br/> //FilterList filterlist = new FilterList(filters); //也可以这样写,MUST_PASS_ALL标识满足所有的filter,当然也可以使用MUST_PASS_ONE,标识只需要满足一个<br/> FilterList filterlist = new FilterList(FilterList.Operator.MUST_PASS_ALL);<br/> filterlist.addFilter(filter1);<br/> filterlist.addFilter(filter2);<br/> filterlist.addFilter(filter3);<br/> sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"));<br/> sc.setFilter(filterlist); ResultScanner rs = table.getScanner(sc);<br/> for(Result r : rs ){<br/> List<Cell> cells = r.listCells();<br/> for(Cell cell : cells){<br/> String rowkey = new String(CellUtil.cloneRow(cell));<br/> String family = new String(CellUtil.cloneFamily(cell));<br/> String collumn = new String(CellUtil.cloneQualifier(cell));<br/> String cvalue = new String(CellUtil.cloneValue(cell));<br/> System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);<br/> }<br/> }<br/> rs.close();<br/> table.close();<br/> conn.close();<br/> }<br/> }
以上一组filter标识了这样的条件,即行的key必须为”row2″,列名必须为”col1″,值必须为”B”
结果参考:
如果没有 sc.addColumn(Bytes.toBytes(“cf”),Bytes.toBytes(“col1”));这一句,结果会是下面的样子
rowkey:row2 family:cf column:col1 value:B<br/> 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
转发申明:
本文转自互联网,由小站整理并发布,在于分享相关技术和知识。版权归原作者所有,如有侵权,请联系本站 top8488@163.com,将在24小时内删除。谢谢