hbasedeveloperAPI1.22版是怎么样的
这篇文章主要介绍了hbase developer API 1.22版是怎么样的,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联2013年至今,先为阿勒泰等服务建站,阿勒泰等地企业,进行企业商务咨询服务。为阿勒泰企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
示例代码
package com.woozoom.hbase.test;import java.io.IOException;import java.util.*;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;public class BaseOperation {private static final String TABLE_NAME = "demo_table";public static Configuration conf = null;public HTable table = null;public HBaseAdmin admin = null;static {conf = HBaseConfiguration.create(); }/** * 创建一张表 */ public static void creatTable(String strTN, String[] familys)throws Exception {Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); TableName tn = TableName.valueOf(strTN);if (admin.tableExists(tn)) { System.out.println("table already exists!"); } else { HTableDescriptor tableDesc = new HTableDescriptor(tn);for (String colFamily : familys) { tableDesc.addFamily(new HColumnDescriptor(colFamily)); } admin.createTable(tableDesc); System.out.println("create table " + strTN + " ok."); } }/** * 删除表 */ public static void deleteTable(String strTN) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); TableName tn = TableName.valueOf(strTN);admin.disableTable(tn); admin.deleteTable(tn); System.out.println("delete table " + strTN + " ok."); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } }/** * 插入一行记录 */ public static void addRecord(String strTN, String rowKey, String family, String qualifier, String value) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn);Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); System.out.println("insert recored " + rowKey + " to table " + strTN + " ok."); } catch (IOException e) { e.printStackTrace(); } }/** * 删除一行记录 */ public static void delRecord(String strTN, String rowKey)throws IOException {Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn);List list = new ArrayList(); Delete del = new Delete(rowKey.getBytes()); list.add(del); table.delete(list); System.out.println("del recored " + rowKey + " ok."); }/** * 查找一行记录 */ public static void getOneRecord(String strTN, String rowKey)throws IOException { Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn); Get get = new Get(rowKey.getBytes()); Result rs = table.get(get);for (Cell cell : rs.listCells()) { KeyValue kv = (KeyValue)cell; System.out.println("### " + new String(kv.getKey()) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyOffset() + cell.getFamilyLength())) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength())) + " ###"); System.out.println("### " + new String(Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength())) + " ###"); System.out.println("### " + cell.getTimestamp() + " ###"); }NavigableMap> nm = rs.getNoVersionMap();for(byte[] family : nm.keySet()){ System.out.println(new String(family)); NavigableMap nmSon = nm.get(family);for (byte[] qualifier : nmSon.keySet()){ System.out.println(new String(qualifier));byte[] value = nmSon.get(qualifier); System.out.println(new String(value)); } } }/** * 显示所有数据 */ public static void getAllRecord(String strTN) {try { Connection conn = ConnectionFactory.createConnection(conf); TableName tn = TableName.valueOf(strTN); Table table = conn.getTable(tn); Scan s = new Scan(); ResultScanner ss = table.getScanner(s);for (Result r : ss) {for (Cell cell : r.listCells()) { KeyValue kv = (KeyValue)cell; System.out.print(new String(kv.getKey()) + " "); System.out.print(new String(Arrays.copyOfRange(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyOffset() + kv.getFamilyLength())) + ":"); System.out.print(new String(Arrays.copyOfRange(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierOffset() + kv.getQualifierLength())) + " "); System.out.print(kv.getTimestamp() + " "); System.out.println(new String(Arrays.copyOfRange(kv.getValueArray(), kv.getValueOffset(), kv.getValueOffset() + kv.getValueLength()))); } }} catch (IOException e) { e.printStackTrace(); } }public static void main(String[] agrs) {try { String tablename = "scores1"; String[] familys = {"grade", "course"}; BaseOperation.creatTable(tablename, familys);// add record zkb BaseOperation.addRecord(tablename, "zkb", "grade", "", "5"); BaseOperation.addRecord(tablename, "zkb", "course", "", "90"); BaseOperation.addRecord(tablename, "zkb", "course", "math", "97"); BaseOperation.addRecord(tablename, "zkb", "course", "art", "87");// add record baoniu BaseOperation.addRecord(tablename, "baoniu", "grade", "", "4"); BaseOperation .addRecord(tablename, "baoniu", "course", "math", "89"); System.out.println("===========get one record========"); BaseOperation.getOneRecord(tablename, "zkb"); System.out.println("===========show all record========"); BaseOperation.getAllRecord(tablename); System.out.println("===========del one record========"); BaseOperation.delRecord(tablename, "baoniu"); BaseOperation.getAllRecord(tablename); System.out.println("===========show all record========"); BaseOperation.getAllRecord(tablename); } catch (Exception e) { e.printStackTrace(); } } }
感谢你能够认真阅读完这篇文章,希望小编分享的“hbase developer API 1.22版是怎么样的”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
文章标题:hbasedeveloperAPI1.22版是怎么样的
转载来于:http://scjbc.cn/article/ppccih.html