用過之後,總得寫個總結,否則,就忘嘍。java
1、尋找操做的jar包。服務器
java操做hbase,首先要考慮到使用hbase的jar包。函數
由於咱裝的是CDH5,比較方便,使用SecureCRT工具,遠程鏈接到你安裝的那臺服務器上。工具
jar包的存放位置在/opt/cloudera/parcels/CDH/lib/hbase,找到,下載下來。oop
在當前路徑下,有一個lib包,裏面是支持hbase的hadoop的jar包,根據需求,能夠下載下來。spa
2、找一個API文檔當成手冊,哪裏不會查哪裏code
百度分享,http://pan.baidu.com/s/1jICqdgy,能夠下載。對象
3、java操做Hbase。blog
構造函數:繼承
public static Configuration configuration; static{ configuration = HBaseConfiguration.create(); configuration.set("hbase.master","ip1:60000"); configuration.set("hbase.zookeeper.quorum", "ip1:2181,ip2:2181") ; }
一、如何建立一個hbase表並put數據。
public static void creaTable(String tablename) throws Exception{ HBaseAdmin admin = new HBaseAdmin(configuration); if(admin.tableExists(tablename)){ admin.disableTable(tablename); admin.deleteTable(tablename); System.out.println("開始建立表!"); } System.out.println("新的表正在建立中!!!"); HTableDescriptor tableDescriptor = new HTableDescriptor(tablename); tableDescriptor.addFamily(new HColumnDescriptor("cf1")); admin.createTable(tableDescriptor); Put put = new Put("123".getBytes()); put.add("cf1".getBytes(), "colum1".getBytes(), "value1".getBytes()) ; put.add("cf1".getBytes(), "colum2".getBytes(), "value2".getBytes()) ; put.add("cf1".getBytes(), "colum3".getBytes(), "value3".getBytes()) ; Put put2 = new Put("234".getBytes()) ; put2.add("cf1".getBytes(), "colum1".getBytes(), "value1".getBytes()) ; put2.add("cf1".getBytes(), "colum2".getBytes(), "value2".getBytes()) ; put2.add("cf1".getBytes(), "colum3".getBytes(), "value3".getBytes()) ; HTable table = new HTable(configuration, tablename); table.put(put); table.put(put2); }
二、刪除hbase中的table裏面的rowkey
1 public static void deleteRow(String tableName,String rowKey) throws Exception{ 2 HTable hTable = new HTable(configuration,tableName); 3 Delete delete = new Delete(rowKey.getBytes()); 4 List<Delete> list = new ArrayList<Delete>(); 5 list.add(delete); 6 hTable.delete(list); 7 }
三、查詢row = rowKey的數據
1 /** 2 * 查詢row = rowKey的數據 3 * @param tableName 4 * @param rowKey 5 * @throws Exception 6 */ 7 public static void getRow(String tableName,String rowKey) throws Exception{ 8 HTable hTable = new HTable(configuration, tableName); 9 Get get = new Get(rowKey.getBytes()); 10 Result result = hTable.get(get); 11 for(KeyValue value:result.raw()){ 12 System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue())); 13 } 14 }
四、查詢rowkey在startRow和endRow之間的數據,及rowkey的範圍查詢
Put、Delete與Get對象都是Row的子類,從該繼承關係中咱們就能夠了解到Get、Delete與Pu對象自己就只能進行單行的操做,
HBase客戶端還提供了一套可以進行全表掃描的API,方便用戶可以快速對整張表進行掃描,以獲取想要的結果---scan、
1 /** 2 * 查詢rowkey在startRow和endRow之間的數據 3 * @param tablename 4 * @param startRow 5 * @param endRow 6 * @throws Exception 7 */ 8 public static void getBetweenRow(String tableName,String startRow,String stopRow) throws Exception{ 9 HTable table = new HTable(configuration, tableName); 10 Scan scan = new Scan(); 11 scan.addColumn("cf1".getBytes(), "colum1".getBytes()); 12 scan.addColumn("cf1".getBytes(), "colum2".getBytes()); 13 scan.addColumn("cf1".getBytes(), "colum3".getBytes()); 14 15 scan.setStartRow(startRow.getBytes()); 16 scan.setStopRow(stopRow.getBytes()); 17 18 ResultScanner scanner = table.getScanner(scan); 19 20 for(Result result:scanner){ 21 for(KeyValue value:result.raw()){ 22 System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue())); 23 } 24 } 25 }