hbase1.2.4 API改動

來源:互聯網
上載者:User

標籤:new   key   name   script   ati   ##   for   fan   min   

  1. 使用ConnectionFactory來串連資料庫
  2. 表名使用特定的類TableName而不是字串
  3. 棄用HAdmin使用Admin,通過Connection擷取
  4. 啟用HTable使用Table,通過Connection擷取
  5. Put.add()方法過時,使用Put.addColumn()
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.Map;import java.util.NavigableMap;/** * Created by hadoop on 16-11-24. */public class HBaseTestCase { static Configuration cfg = HBaseConfiguration.create(); //建立一張表,通過Admin HTableDescriptor 來建立 public static void createTable(Connection connection,TableName tableName, String columnFamily)throws Exception{ Admin admin = connection.getAdmin(); if(admin.tableExists(tableName)){ System.out.println("table Exists!"); }else{ HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("create table success!"); } } //添加一條資料,通過HTable Put 為已經存在的表來添加資料 public static void put(Connection connection,TableName tableName,String row,String columnFamily,String column,String data)throws Exception{ Table table = connection.getTable(tableName); Put p1 = new Put(Bytes.toBytes(row)); p1.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(data)); table.put(p1); } //根據row key擷取表中的該行資料 public static void get(Connection connection,TableName tableName,String row)throws IOException { Table table = connection.getTable(tableName); Get g = new Get(Bytes.toBytes(row)); Result result = table.get(g); NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> navigableMap = result.getMap(); for(Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry:navigableMap.entrySet()){ System.out.print(Bytes.toString(entry.getKey())+"#"); NavigableMap<byte[], NavigableMap<Long, byte[]>> map =entry.getValue(); for(Map.Entry<byte[], NavigableMap<Long, byte[]>> en:map.entrySet()){ System.out.print(Bytes.toString(en.getKey())+"##"); NavigableMap<Long, byte[]> ma = en.getValue(); for(Map.Entry<Long, byte[]>e: ma.entrySet()){ System.out.print(e.getKey()+"###"); System.out.println(Bytes.toString(e.getValue())); } } } // System.out.println("Get:"+result); } //根據TableName擷取整張表中的資料 public static void scan(Connection connection,TableName tableName)throws Exception{ Table table = connection.getTable(tableName); Scan s = new Scan(); ResultScanner rs = table.getScanner(s); //遍曆列印表中的資料 for(Result r:rs){ NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> navigableMap = r.getMap(); for(Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry:navigableMap.entrySet()){ System.out.print(Bytes.toString(r.getRow())+":"); System.out.print(Bytes.toString(entry.getKey())+"#"); NavigableMap<byte[], NavigableMap<Long, byte[]>> map =entry.getValue(); for(Map.Entry<byte[], NavigableMap<Long, byte[]>> en:map.entrySet()){ System.out.print(Bytes.toString(en.getKey())+"##"); NavigableMap<Long, byte[]> ma = en.getValue(); for(Map.Entry<Long, byte[]>e: ma.entrySet()){ System.out.print(e.getKey()+"###"); System.out.println(Bytes.toString(e.getValue())); } } } System.out.println(); } } //刪除表中的資料 public static boolean delete(Connection connection,TableName tableName) throws IOException{ Admin admin = connection.getAdmin(); if(admin.tableExists(tableName)){ try{ admin.disableTable(tableName); admin.deleteTable(tableName); }catch(Exception ex){ ex.printStackTrace(); return false; } } return true; } public static void main(String args[]){ TableName tableName = TableName.valueOf("hbase_tb"); String columnFamily = "cf"; try{ cfg.set("hbase.zookeeper.property.clientPort", "2181"); cfg.set("hbase.zookeeper.quorum", "fang-ubuntu,fei-ubuntu,kun-ubuntu"); Connection connection = ConnectionFactory.createConnection(cfg); HBaseTestCase.createTable(connection,tableName,columnFamily); HBaseTestCase.put(connection,tableName,"row1",columnFamily,"cl1","data"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl1","fang"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl2","fang"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl3","fang"); HBaseTestCase.put(connection,tableName,"row3",columnFamily,"cl4","fang"); HBaseTestCase.get(connection,tableName,"row1"); HBaseTestCase.scan(connection,tableName); if(true==HBaseTestCase.delete(connection,tableName)){ System.out.println("Delete table:"+tableName.toString()+" success!"); } }catch(Exception e){ e.printStackTrace(); } }}

 

 

代碼執行個體中使用到了HBase的基本操作。

hbase1.2.4 API改動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.