參考:java
http://blog.csdn.net/net19880504/article/details/17733161apache
運行Eclipse,建立一個新的Java工程「HBaseClient」,右鍵項目根目錄,選擇 「Properties」->「Java Build Path」->「Library」->「Add External JARs」,將Hbase解壓後根目錄下的hbase-0.94.1-security.jar、hbase-0.94.1-security-tests.jar和lib子目錄下全部jar 包添加到本工程的Classpath下。api
HBase提供了java api來對HBase進行一系列的管理涉及到對錶的管理、數據的操做等。經常使用的API操做有:
一、 對錶的建立、刪除、顯示以及修改等,能夠用HBaseAdmin,一旦建立了表,那麼能夠經過HTable的實例來訪問表,每次能夠往表裏增長數據。
二、 插入數據
建立一個Put對象,在這個Put對象裏能夠指定要給哪一個列增長數據,以及當前的時間戳等值,而後經過調用HTable.put(Put)來提交操做,子猴在這裏提請注意的是:在建立Put對象的時候,你必須指定一個行(Row)值,在構造Put對象的時候做爲參數傳入。
三、 獲取數據
要獲取數據,使用Get對象,Get對象同Put對象同樣有好幾個構造函數,一般在構造的時候傳入行值,表示取第幾行的數據,經過HTable.get(Get)來調用。
四、 瀏覽每一行
經過Scan能夠對錶中的行進行瀏覽,獲得每一行的信息,好比列名,時間戳等,Scan至關於一個遊標,經過next()來瀏覽下一個,經過調用HTable.getScanner(Scan)來返回一個ResultScanner對象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一個Result。Result是一個
KeyValue的鏈表。
五、 刪除
使用Delete來刪除記錄,經過調用HTable.delete(Delete)來執行刪除操做。(注:刪除這裏有些特別,也就是刪除並非立刻將數據從表中刪除。)
六、 鎖
新增、獲取、刪除在操做過程當中會對所操做的行加一個鎖,而瀏覽卻不會。
七、 簇的訪問
客戶端代碼經過ZooKeeper來訪問找到簇,也就是說ZooKeeper quorum將被使用,那麼相關的類(包)應該在客戶端的類(classes)目錄下,即客戶端必定要找到文件hbase-site.xml。 函數
新建一個類:oop
1 package com.hj.myhbase131; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.hadoop.conf.Configuration; 8 import org.apache.hadoop.hbase.client.Put; 9 import org.apache.hadoop.hbase.client.Result; 10 import org.apache.hadoop.hbase.client.ResultScanner; 11 import org.apache.hadoop.hbase.client.Scan; 12 import org.apache.hadoop.hbase.util.Bytes; 13 import org.apache.hadoop.hbase.Cell; 14 import org.apache.hadoop.hbase.CellUtil; 15 import org.apache.hadoop.hbase.HBaseConfiguration; 16 import org.apache.hadoop.hbase.HColumnDescriptor; 17 import org.apache.hadoop.hbase.HTableDescriptor; 18 import org.apache.hadoop.hbase.TableName; 19 import org.apache.hadoop.hbase.client.Connection; 20 import org.apache.hadoop.hbase.client.ConnectionFactory; 21 import org.apache.hadoop.hbase.client.Delete; 22 import org.apache.hadoop.hbase.client.Get; 23 import org.apache.hadoop.hbase.client.HBaseAdmin; 24 import org.apache.hadoop.hbase.client.HTable; 25 26 /** 27 * Hello world! 28 * 29 */ 30 public class App 31 { 32 private static Configuration conf = null; 33 private static Connection conn = null; 34 35 static { 36 conf = HBaseConfiguration.create(); 37 try { 38 conn = ConnectionFactory.createConnection(conf); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 /**建立表 46 * @param tableName 47 * @param familys 48 * @throws Exception 49 */ 50 public static void creatTable(String tableName, String[] familys) throws Exception { 51 HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); 52 if (admin.tableExists(tableName)) { 53 System.out.println("table already exists"); 54 }else { 55 HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName)); 56 for (int i = 0; i < familys.length; i++) { 57 tableDesc.addFamily(new HColumnDescriptor(familys[i])); 58 } 59 admin.createTable(tableDesc); 60 System.out.println("create table " + tableName + " ok."); 61 } 62 } 63 64 /**刪除表 65 * @param tableName 66 * @throws Exception 67 */ 68 public static void deleteTable(String tableName) throws Exception { 69 HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); 70 admin.disableTable(tableName); 71 admin.deleteTable(tableName); 72 System.out.println("delete table " + tableName + " ok."); 73 } 74 75 /**插入一行記錄 76 * @param tableName 77 * @param rowKey 78 * @param family 79 * @param qualifier 80 * @param value 81 * @throws Exception 82 */ 83 public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value) 84 throws Exception{ 85 try { 86 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 87 Put put = new Put(Bytes.toBytes(rowKey)); 88 put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value)); 89 table.put(put); 90 System.out.println("insert recored " + rowKey + " to table " + tableName +" ok."); 91 } catch (IOException e) { 92 e.printStackTrace(); 93 } 94 } 95 96 /** 97 * 刪除一行記錄 98 */ 99 public static void delRecord (String tableName, String rowKey) throws IOException{ 100 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 101 List <Delete>list = new ArrayList<Delete>(); 102 Delete del = new Delete(rowKey.getBytes()); 103 list.add(del); 104 table.delete(list); 105 System.out.println("del recored " + rowKey + " ok."); 106 } 107 108 /** 109 * 查找一行記錄 110 */ 111 public static void getOneRecord (String tableName, String rowKey) throws IOException{ 112 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 113 Get get = new Get(rowKey.getBytes()); 114 Result rs = table.get(get); 115 for(Cell cell : rs.listCells()){ 116 // 一個Cell就是一個單元格,經過下面的方法能夠得到這個單元格的各個值 117 System.out.print("行健: " + new String(CellUtil.cloneRow(cell))); 118 System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell))); 119 System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell))); 120 System.out.print(" 值: " + new String(CellUtil.cloneValue(cell))); 121 System.out.println("時間戳: " + cell.getTimestamp()); 122 } 123 } 124 125 /** 126 * 顯示全部數據 127 */ 128 public static void getAllRecord (String tableName) { 129 try{ 130 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 131 Scan s = new Scan(); 132 ResultScanner ss = table.getScanner(s); 133 for(Result r:ss){ 134 for(Cell cell : r.listCells()){ 135 System.out.print(Bytes.toString(CellUtil.cloneRow(cell)) + " " ); 136 System.out.print(Bytes.toString(CellUtil.cloneFamily(cell)) + ":" ); 137 System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell)) + " " ); 138 System.out.println(Bytes.toString(CellUtil.cloneValue(cell))); 139 } 140 } 141 } catch (IOException e){ 142 e.printStackTrace(); 143 } 144 } 145 146 public static void main( String[] args ) throws Exception 147 { 148 try { 149 String tablename = "scores"; 150 String[] familys = {"grade", "course"}; 151 App.creatTable(tablename, familys); 152 153 //add record zkb 154 App.addRecord(tablename,"zkb","grade","","5"); 155 App.addRecord(tablename,"zkb","course","","90"); 156 App.addRecord(tablename,"zkb","course","math","97"); 157 App.addRecord(tablename,"zkb","course","art","87"); 158 //add record baoniu 159 App.addRecord(tablename,"baoniu","grade","","4"); 160 App.addRecord(tablename,"baoniu","course","math","89"); 161 162 System.out.println("===========get one record========"); 163 App.getOneRecord(tablename, "zkb"); 164 165 System.out.println("===========show all record========"); 166 App.getAllRecord(tablename); 167 168 System.out.println("===========del one record========"); 169 App.delRecord(tablename, "baoniu"); 170 App.getAllRecord(tablename); 171 172 System.out.println("===========show all record========"); 173 App.getAllRecord(tablename); 174 } catch (Exception e) { 175 e.printStackTrace(); 176 } 177 } 178 }
結果顯示爲:ui
table already exists
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored baoniu to table scores ok.
insert recored baoniu to table scores ok.
===========get one record========
行健: zkb列簇: course 列: 值: 90時間戳: 1501034629784
行健: zkb列簇: course 列: art 值: 87時間戳: 1501034629794
行健: zkb列簇: course 列: math 值: 97時間戳: 1501034629787
行健: zkb列簇: grade 列: 值: 5時間戳: 1501034629768
===========show all record========
baoniu course:math 89
baoniu grade: 4
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5
===========del one record========
del recored baoniu ok.
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5
===========show all record========
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5spa