hbase thrift java util
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了hbase thrift java util,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11980字,纯文字阅读大概需要18分钟。
内容图文
转载:https://github.com/ssw239/java-thrift-hbase-io/blob/master/HBaseThriftClient.java
pom.xml
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-thrift</artifactId> <version>1.3.1</version> </dependency>
ThriftUtil.java
package hbase; import java.lang.Object; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.hadoop.hbase.thrift.generated.BatchMutation; import org.apache.hadoop.hbase.thrift.generated.Hbase; import org.apache.hadoop.hbase.thrift.generated.IOError; import org.apache.hadoop.hbase.thrift.generated.Mutation; import org.apache.hadoop.hbase.thrift.generated.TCell; import org.apache.hadoop.hbase.thrift.generated.TRowResult; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; public class ThriftUtil { protected Hbase.Client hbaseClient = null; private String hbaseAddr = ""; private Integer hbasePort = 0; private TTransport socket = null; private TProtocol protocol = null; protectedstaticfinal String CHAR_SET = "UTF-8"; public ThriftUtil(String addr, Integer port) { hbaseAddr = addr; hbasePort = port; socket = new TSocket(hbaseAddr, hbasePort); protocol = new TBinaryProtocol(socket, true, true); hbaseClient = new Hbase.Client(protocol); } publicstaticvoid main(String[] args) throws TTransportException { ThriftUtil hbaseClient = null; try { hbaseClient = new ThriftUtil("192.168.101.144", 9090); hbaseClient.openTransport(); doSomeTest(hbaseClient); } catch (Exception e) { e.printStackTrace(); } finally { hbaseClient.closeTransport(); } } publicstaticvoid doSomeTest(ThriftUtil client) throws TException { String tableName = "test5"; testScanTable(tableName, "", 1000, client); String rowKey_R1 = "r1"; Map<String, String> kvpUpdate_r1 = new HashMap<String, String>(); kvpUpdate_r1.put("cf:name", "val_20150618_0920_1"); kvpUpdate_r1.put("cf:age", "12"); // client.updateRow(tableName, rowKey_R1, kvpUpdate_r1); String rowKey_R2 = "r2"; Map<String, String> kvpUpdate_r2 = new HashMap<String, String>(); kvpUpdate_r2.put("cf:name", "val_201505181028_r2c1"); kvpUpdate_r2.put("cf:age", "13"); Map<String, Map<String, String>> rowBatchData = new HashMap<String, Map<String, String>>(); rowBatchData.put(rowKey_R1, kvpUpdate_r1); rowBatchData.put(rowKey_R2, kvpUpdate_r2); // client.updateRows(tableName, rowBatchData); Map<String, String> attributes = new HashMap<String, String>(); // List<TRowResult> rowRslts = client.getRow("test5", "r2", attributes); Map<String, String> kvpUpdate_bs = new HashMap<String, String>(); kvpUpdate_bs.put("cf:name", "val_20150519_1352"); kvpUpdate_bs.put("cf:age", "14"); // client.updateRow(tableName, "r2", kvpUpdate_bs); // client.deleteCell(tableName, "r2", "cf:name"); List<String> columns = new ArrayList<String>(); columns.add("cf:name"); columns.add("cf:age"); // client.deleteCells(tableName, "r1", columns); client.deleteRow(tableName, "r1"); testScanTable(tableName, "", 1000, client); System.out.println("Done."); } publicstaticvoid testIterateRow(String tableName, String rowKey, ThriftUtil client) throws TException { Map<String, String> attributes = new HashMap<String, String>(); List<TRowResult> results = client.getRow(tableName, rowKey, attributes); for (TRowResult rslt : results) { client.iterateResults(rslt); } } publicstaticvoid testScanTable(String tableName, String startRow, int rowCnt, ThriftUtil client) throws TException { List<String> columns = new ArrayList<String>(0); Map<String, String> attributesTest = new HashMap<String, String>(); int scannerID = client.scannerOpen(tableName, startRow, columns, attributesTest); try { List<TRowResult> scanResults = client.scannerGetList(scannerID, rowCnt); while (scanResults != null && !scanResults.isEmpty()) { for (TRowResult rslt : scanResults) { client.iterateResults(rslt); } scanResults = client.scannerGetList(scannerID, rowCnt); } } finally { client.scannerClose(scannerID); } } publicvoid listTableNames(ThriftUtil client) throws TTransportException { List<String> tblNames = client.getTableNames(); for (String name : tblNames) { System.out.println(">> " + name); } } publicvoid deleteRow(String table, String rowKey) throws TException { ByteBuffer tableName = getByteBuffer(table); ByteBuffer row = getByteBuffer(rowKey); hbaseClient.deleteAllRow(tableName, row, getAttributesMap(new HashMap<String, String>())); } publicvoid deleteCell(String table, String rowKey, String column) throws TException { List<String> columns = new ArrayList<String>(1); columns.add(column); deleteCells(table, rowKey, columns); } publicvoid deleteCells(String table, String rowKey, List<String> columns) throws TException { boolean writeToWal = false; List<Mutation> mutations = new ArrayList<Mutation>(); for (String column : columns) { mutations.add(new Mutation(false, getByteBuffer(column), null, writeToWal)); } ByteBuffer tableName = getByteBuffer(table); ByteBuffer row = getByteBuffer(rowKey); hbaseClient.mutateRow(tableName, row, mutations, getAttributesMap(new HashMap<String, String>())); } publicvoid updateRow(String table, String rowKey, Map<String, String> rowData) throws TException { boolean writeToWal = false; Map<String, String> attributes = new HashMap<String, String>(); List<Mutation> mutations = new ArrayList<Mutation>(); for(Map.Entry<String, String> entry : rowData.entrySet()) { mutations.add(new Mutation(false, getByteBuffer(entry.getKey()), getByteBuffer(entry.getValue()), writeToWal)); } Map<ByteBuffer, ByteBuffer> wrappedAttributes = getAttributesMap(attributes); ByteBuffer tableName = getByteBuffer(table); ByteBuffer row = getByteBuffer(rowKey); hbaseClient.mutateRow(tableName, row, mutations, wrappedAttributes); } publicvoid updateRows(String table, Map<String, Map<String, String>> rowBatchData) throws TException { boolean writeToWal = false; Map<String, String> attributes = new HashMap<String, String>(); Map<ByteBuffer, ByteBuffer> wrappedAttributes = getAttributesMap(attributes); ByteBuffer tableNameByte = getByteBuffer(table); List<BatchMutation> rowBatches = new ArrayList<BatchMutation>(); for(Entry<String, Map<String, String>> batchEntry : rowBatchData.entrySet()) { List<Mutation> mutations = new ArrayList<Mutation>(); for (Map.Entry<String, String> rowData : batchEntry.getValue().entrySet()) { mutations.add(new Mutation(false, getByteBuffer(rowData.getKey()), getByteBuffer(rowData.getValue()), writeToWal)); } BatchMutation batchMutation = new BatchMutation(getByteBuffer(batchEntry.getKey()), mutations); rowBatches.add(batchMutation); } hbaseClient.mutateRows(tableNameByte, rowBatches, wrappedAttributes); } publicvoid iterateResults(TRowResult result) { Iterator<Entry<ByteBuffer, TCell>> iter = result.columns.entrySet().iterator(); System.out.println("RowKey:" + new String(result.getRow())); while (iter.hasNext()) { Entry<ByteBuffer, TCell> entry = iter.next(); System.out.println("\tCol=" + new String(toBytes(entry.getKey())) + ", Value=" + new String(entry.getValue().getValue())); } } public List<TRowResult> scannerGetList(int id, int nbRows)throws TException { return hbaseClient.scannerGetList(id, nbRows); } public List<TRowResult> scannerGet(int id) throws TException { return hbaseClient.scannerGetList(id, 1); } publicint scannerOpen(String table, String startRow, String stopRow, List<String> columns, Map<String, String> attributes) throws TException { ByteBuffer tableName = getByteBuffer(table); List<ByteBuffer> blist = getColumnsByte(columns); Map<ByteBuffer, ByteBuffer> wrappedAttributes = getAttributesMap(attributes); return hbaseClient.scannerOpenWithStop(tableName, getByteBuffer(startRow), getByteBuffer(stopRow), blist, wrappedAttributes); } publicint scannerOpen(String table, String startRow, List<String> columns, Map<String, String> attributes) throws TException { ByteBuffer tableName = getByteBuffer(table); List<ByteBuffer> blist = getColumnsByte(columns); Map<ByteBuffer, ByteBuffer> wrappedAttributes = getAttributesMap(attributes); return hbaseClient.scannerOpen(tableName, getByteBuffer(startRow), blist, wrappedAttributes); } publicvoid scannerClose(int id) throws TException { hbaseClient.scannerClose(id); } public List<ByteBuffer> getColumnsByte(List<String> columns) { List<ByteBuffer> blist = new ArrayList<ByteBuffer>(); for(String column : columns) { blist.add(getByteBuffer(column)); } return blist; } protectedbyte[] toBytes(ByteBuffer buffer) { byte[] bytes = newbyte[buffer.limit()]; for (int i = 0; i < buffer.limit(); i++) { bytes[i] = buffer.get(); } return bytes; } public List<TRowResult> getRow(String table, String row, Map<String, String> attributes) throws TException { ByteBuffer tableName = getByteBuffer(table); Map<ByteBuffer, ByteBuffer> wrappedAttributes = getAttributesMap(attributes); return hbaseClient.getRow(tableName, getByteBuffer(row), wrappedAttributes); } public List<String> getTableNames() throws TTransportException { ArrayList<String> tableNames = new ArrayList<String>(); try { for (ByteBuffer name : hbaseClient.getTableNames()) { tableNames.add(byteBufferToString(name)); } return tableNames; } catch (Exception e) { e.printStackTrace(); returnnull; } } privatestatic Map<ByteBuffer, ByteBuffer> getAttributesMap(Map<String, String> attributes) { Map<ByteBuffer, ByteBuffer> attributesMap = null; if(attributes != null && !attributes.isEmpty()) { attributesMap = new HashMap<ByteBuffer, ByteBuffer>(); for(Map.Entry<String, String> entry : attributes.entrySet()) { attributesMap.put(getByteBuffer(entry.getKey()), getByteBuffer(entry.getValue())); } } return attributesMap; } publicstatic String byteBufferToString(ByteBuffer buffer) { CharBuffer charBuffer = null; try { Charset charset = Charset.forName(CHAR_SET); CharsetDecoder decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); buffer.flip(); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); returnnull; } } publicstatic ByteBuffer getByteBuffer(String str) { return ByteBuffer.wrap(str.getBytes()); } publicvoid openTransport() throws TTransportException { if (socket != null) { socket.open(); } } publicvoid closeTransport() throws TTransportException { if (socket != null) { socket.close(); } } }
demo:
https://github.com/ssw239/java-thrift-hbase-io/blob/master/HBaseThriftClient.java
https://github.com/randomtask1155/HbaseThriftTest/blob/master/src/main/java/io/pivotal/support/HbaseThriftTest.java
https://github.com/mypgether/study-demo
https://github.com/apache/hbase/blob/master/hbase-examples/src/main/java/org/apache/hadoop/hbase/thrift/DemoClient.java
https://github.com/randomtask1155/HbaseThriftTest/blob/master/src/main/java/io/pivotal/support/HbaseThriftTest.java
相关资料:
https://wiki.apache.org/hadoop/Hbase/ThriftApi
https://thrift.apache.org/
https://hbase.apache.org/1.2/apidocs/org/apache/hadoop/hbase/thrift/package-summary.html
https://blog.csdn.net/power0405hf/article/details/50068901
https://blog.csdn.net/guxch/article/details/12163047
原文:https://www.cnblogs.com/libin2015/p/9506558.html
内容总结
以上是互联网集市为您收集整理的hbase thrift java util全部内容,希望文章能够帮你解决hbase thrift java util所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。