Java对hbase的操作

来源:互联网 发布:淘宝店铺详情页模板 编辑:程序博客网 时间:2024/05/21 09:46


HBase提供了Java API对其进行管理,包括对表的管理、数据的操作等。
1. HBaseAdmin —— 对表的创建、删除、显示以及修改等;
  
2. HTable —— 通过HTable的实例来访问表并进行数据的操作,获取表实例如下两种方法:
   方法一:直接获取
   HTable table = new HTable(config, tableName);
   方法二:用表连接池的方式
   HTablePool pool = new HTablePool(config,1000);
   HTable table = (HTable) pool.getTable(tableName);

3.插入数据:
创建一个Put对象,并指定参数为一个行(Row)值(即指定给哪一列增加数据以及当前的时间戳等值),然后调用HTable.put(Put)。

4.获取数据
创建一个Get对象,在构造的时候传入行值,表示取第几行的数据,然后调用HTable.get(Get)。

5.删除数据
创建一个Delete对象,参数为一个行(Row)值,然后调用HTable.delete(Delete)。

6. 浏览数据
通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个。
通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。
HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。
Result是一个KeyValue的链表,遍历过程当中保存当前行信息。

以下是一个java对hbase的基本操作实例(hadoop-1.0.3 && hbase-0.90.3)
Java代码 复制代码 收藏代码Java对hbase的操作
  1. package tool;   
  2.   
  3. import java.io.IOException;   
  4. import java.util.Scanner;   
  5.   
  6. import org.apache.hadoop.conf.Configuration;   
  7. import org.apache.hadoop.hbase.HBaseConfiguration;   
  8. import org.apache.hadoop.hbase.HColumnDescriptor;   
  9. import org.apache.hadoop.hbase.HTableDescriptor;   
  10. import org.apache.hadoop.hbase.KeyValue;   
  11. import org.apache.hadoop.hbase.client.Delete;   
  12. import org.apache.hadoop.hbase.client.Get;   
  13. import org.apache.hadoop.hbase.client.HBaseAdmin;   
  14. import org.apache.hadoop.hbase.client.HTable;   
  15. import org.apache.hadoop.hbase.client.HTablePool;   
  16. import org.apache.hadoop.hbase.client.Put;   
  17. import org.apache.hadoop.hbase.client.Result;   
  18. import org.apache.hadoop.hbase.client.ResultScanner;   
  19. import org.apache.hadoop.hbase.client.Scan;   
  20. import org.apache.hadoop.hbase.util.Bytes;   
  21.   
  22. public class Hbase {   
  23.     //initial  
  24.     static Configuration config null;   
  25.     static {   
  26.         config HBaseConfiguration.create();   
  27.         config.set("hbase.zookeeper.quorum""192.168.21.20, 192.168.21.21, 192.168.21.22");   
  28.         config.set("hbase.zookeeper.property.clientPort""2181");   
  29.     }   
  30.        
  31.     //create new table  
  32.     public void createTable(String tableName, String[] familys) throws IOException {   
  33.         HBaseAdmin admin new HBaseAdmin(config);   
  34.         if (admin.tableExists(tableName)) {   
  35.             System.out.println(tableName is already exists,Please create another table!");   
  36.         else {   
  37.             HTableDescriptor  desc new HTableDescriptor(tableName);   
  38.             for (int 0familys.length; i++) {   
  39.                 HColumnDescriptor family new HColumnDescriptor(familys[i]);              
  40.                 desc.addFamily(family);   
  41.             }   
  42.             admin.createTable(desc);   
  43.             System.out.println("Create table \'" tableName "\' OK!");   
  44.         }   
  45.            
  46.     }   
  47.   
  48.     //delete table  
  49.     public void deleteTable(String tableName) throws IOException {   
  50.         HBaseAdmin admin new HBaseAdmin(config);   
  51.         if (!admin.tableExists(tableName)) {   
  52.             System.out.println(tableName is not exists!");   
  53.         else {   
  54.             Scanner new Scanner(System.in);   
  55.             System.out.print("Are you sure to delete(y/n)?");   
  56.             String str s.nextLine();   
  57.             if (str.equals("y"|| str.equals("Y")) {   
  58.                 admin.disableTable(tableName);   
  59.                 admin.deleteTable(tableName);   
  60.                 System.out.println(tableName is delete!");   
  61.             else {   
  62.                 System.out.println(tableName is not delete!");   
  63.             }   
  64.                  
  65.     }   
  66.        
  67.     //Get table example  
  68.     public void getTable(String tableName) throws IOException {   
  69.         //Method I:  
  70. //      HTable table new HTable(config, tableName);  
  71.         //Method II: better than I.  
  72.         HTablePool pool new HTablePool(config,1000);   
  73.         @SuppressWarnings("unused")   
  74.         HTable table (HTable) pool.getTable(tableName);   
  75.     }   
  76.   
  77.     //add data  
  78.     public void insertData(String tableName, String rowKey, String family, String qualifier, String value) {   
  79.         try {   
  80.             HTablePool pool new HTablePool(config, 1000);   
  81.             HTable table (HTable)pool.getTable(tableName);   
  82.             Put put new Put(Bytes.toBytes(rowKey));   
  83.             put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));   
  84.             table.put(put);   
  85.             System.out.println("insert data successful!");   
  86.         catch (IOException e) {   
  87.             e.printStackTrace();   
  88.         }   
  89.     }   
  90.   
  91.     //delete data  
  92.     public void deleteData(String tableName, String rowKey) {   
  93.         try {   
  94.             HTablePool pool new HTablePool(config, 1000);   
  95.             HTable table (HTable)pool.getTable(tableName);   
  96.             Delete del new Delete(Bytes.toBytes(rowKey));   
  97.             table.delete(del);   
  98.             System.out.println("delete data successful");   
  99.             table.close();   
  100.         catch (IOException e) {   
  101.             e.printStackTrace();   
  102.         }   
  103.     }   
  104.   
  105.     //Query all data  
  106.     public void queryAll(String tableName) {   
  107.         try {   
  108.             HTablePool pool new HTablePool(config, 1000);   
  109.             HTable table (HTable) pool.getTable(tableName);   
  110.             Scan scan new Scan();   
  111.             ResultScanner scanner table.getScanner(scan);   
  112.   
  113.             for (Result row scanner) {   
  114.                 System.out.println("\nRowkey: " new String(row.getRow()));   
  115.                 for (KeyValue kv row.raw()) {   
  116.                     System.out.print(new String(kv.getRow()) ");    //same as above  
  117.                     System.out.print(new String(kv.getFamily()) ":");   
  118.                     System.out.print(new String(kv.getQualifier()) ");   
  119.                     System.out.print(new String(kv.getValue()));   
  120.                     System.out.print(timestamp " kv.getTimestamp() "\n");   
  121.                 }   
  122.             }   
  123.             table.close();   
  124.         catch (IOException e) {   
  125.             e.printStackTrace();   
  126.         }   
  127.     }   
  128.     //query by rowkey  
  129.     public void queryByRowKey(String tableName, String rowKey) {   
  130.         try {   
  131.             HTablePool pool new HTablePool(config, 1000);   
  132.             HTable table (HTable)pool.getTable(tableName);   
  133.             Get get new Get(rowKey.getBytes());   
  134.             Result row table.get(get);   
  135.             for (KeyValue kv row.raw()) {   
  136.                 System.out.print(new String(kv.getRow()) ");       
  137.                 System.out.print(new String(kv.getFamily()) ":");   
  138.                 System.out.print(new String(kv.getQualifier()) ");   
  139.                 System.out.print(new String(kv.getValue()));   
  140.                 System.out.print(timestamp " kv.getTimestamp() "\n");   
  141.             }   
  142.             table.close();   
  143.         catch (IOException e) {   
  144.             e.printStackTrace();   
  145.         }   
  146.     }   
  147.   
  148.       
  149.     public static void main(String[] args) throws IOException {   
  150.         Hbase hbase new Hbase();   
  151.         //Create new table  
  152.         String tableName "test";   
  153.         System.out.println("=======delete table======");   
  154.         hbase.deleteTable(tableName);   
  155.            
  156.         System.out.println("=======create table======");   
  157.         String[] familys {"info""scores"};         
  158.         hbase.createTable(tableName, familys);   
  159.            
  160.         System.out.println("=======insert data=======");   
  161.         //insert Jim  
  162.         hbase.insertData(tableName, "Jim""info""sex""male");   
  163.         hbase.insertData(tableName, "Jim""info""age""18");   
  164.         hbase.insertData(tableName, "Jim""scores""Chinese""98");   
  165.         hbase.insertData(tableName, "Jim""scores""English""90");   
  166.         hbase.insertData(tableName, "Jim""scores""Math""100");   
  167.         //insert Ann  
  168.         hbase.insertData(tableName, "Ann""info""sex""female");   
  169.         hbase.insertData(tableName, "Ann""info""age""18");   
  170.         hbase.insertData(tableName, "Ann""scores""Chinese""97");   
  171.         hbase.insertData(tableName, "Ann""scores""Math""95");        
  172.   
  173.         System.out.println("=======query all data=======");   
  174.         hbase.queryAll(tableName);   
  175.            
  176.         System.out.println("=======query by rowkey=======");   
  177.         String rowKey "Ann";   
  178.         hbase.queryByRowKey(tableName, rowKey);   
  179.            
  180.         System.out.println("=======delete data=======");   
  181.         hbase.deleteData(tableName, rowKey);   
  182.         hbase.queryAll(tableName);         
  183.     }   
  184.        
  185.  
package tool; import java.io.IOException; import java.util.Scanner; 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.KeyValue; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTablePool; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class Hbase { //initial static Configuration config = null; static { config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "192.168.21.20, 192.168.21.21, 192.168.21.22"); config.set("hbase.zookeeper.property.clientPort", "2181"); } //create a new table public void createTable(String tableName, String[] familys) throws IOException { HBaseAdmin admin = new HBaseAdmin(config); if (admin.tableExists(tableName)) { System.out.println(tableName + " is already exists,Please create another table!"); } else { HTableDescriptor desc = new HTableDescriptor(tableName); for (int i = 0; i < familys.length; i++) { HColumnDescriptor family = new HColumnDescriptor(familys[i]); desc.addFamily(family); } admin.createTable(desc); System.out.println("Create table \'" + tableName + "\' OK!"); } } //delete a table public void deleteTable(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(config); if (!admin.tableExists(tableName)) { System.out.println(tableName + " is not exists!"); } else { Scanner s = new Scanner(System.in); System.out.print("Are you sure to delete(y/n)?"); String str = s.nextLine(); if (str.equals("y") || str.equals("Y")) { admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(tableName + " is delete!"); } else { System.out.println(tableName + " is not delete!"); } } } //Get table example public void getTable(String tableName) throws IOException { //Method I: // HTable table = new HTable(config, tableName); //Method II: better than I. HTablePool pool = new HTablePool(config,1000); @SuppressWarnings("unused") HTable table = (HTable) pool.getTable(tableName); } //add a data public void insertData(String tableName, String rowKey, String family, String qualifier, String value) { try { HTablePool pool = new HTablePool(config, 1000); HTable table = (HTable)pool.getTable(tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); System.out.println("insert a data successful!"); } catch (IOException e) { e.printStackTrace(); } } //delete a data public void deleteData(String tableName, String rowKey) { try { HTablePool pool = new HTablePool(config, 1000); HTable table = (HTable)pool.getTable(tableName); Delete del = new Delete(Bytes.toBytes(rowKey)); table.delete(del); System.out.println("delete a data successful"); table.close(); } catch (IOException e) { e.printStackTrace(); } } //Query all data public void queryAll(String tableName) { try { HTablePool pool = new HTablePool(config, 1000); HTable table = (HTable) pool.getTable(tableName); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result row : scanner) { System.out.println("\nRowkey: " + new String(row.getRow())); for (KeyValue kv : row.raw()) { System.out.print(new String(kv.getRow()) + " "); //same as above System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " = "); System.out.print(new String(kv.getValue())); System.out.print(" timestamp = " + kv.getTimestamp() + "\n"); } } table.close(); } catch (IOException e) { e.printStackTrace(); } } //query by rowkey public void queryByRowKey(String tableName, String rowKey) { try { HTablePool pool = new HTablePool(config, 1000); HTable table = (HTable)pool.getTable(tableName); Get get = new Get(rowKey.getBytes()); Result row = table.get(get); for (KeyValue kv : row.raw()) { System.out.print(new String(kv.getRow()) + " "); System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " = "); System.out.print(new String(kv.getValue())); System.out.print(" timestamp = " + kv.getTimestamp() + "\n"); } table.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { Hbase hbase = new Hbase(); //Create a new table String tableName = "test"; System.out.println("=======delete table======"); hbase.deleteTable(tableName); System.out.println("=======create table======"); String[] familys = {"info", "scores"}; hbase.createTable(tableName, familys); System.out.println("=======insert data======="); //insert Jim hbase.insertData(tableName, "Jim", "info", "sex", "male"); hbase.insertData(tableName, "Jim", "info", "age", "18"); hbase.insertData(tableName, "Jim", "scores", "Chinese", "98"); hbase.insertData(tableName, "Jim", "scores", "English", "90"); hbase.insertData(tableName, "Jim", "scores", "Math", "100"); //insert Ann hbase.insertData(tableName, "Ann", "info", "sex", "female"); hbase.insertData(tableName, "Ann", "info", "age", "18"); hbase.insertData(tableName, "Ann", "scores", "Chinese", "97"); hbase.insertData(tableName, "Ann", "scores", "Math", "95"); System.out.println("=======query all data======="); hbase.queryAll(tableName); System.out.println("=======query by rowkey======="); String rowKey = "Ann"; hbase.queryByRowKey(tableName, rowKey); System.out.println("=======delete a data======="); hbase.deleteData(tableName, rowKey); hbase.queryAll(tableName); } }



程序运行结果:
=======delete table======
Are you sure to delete(y/n)?y
test is delete!
=======create table======
Create table 'test' OK!
=======insert data=======
insert a data successful!
=======query all data=======
Rowkey: Ann
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160

Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052
=======query by rowkey=======
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160
=======delete a data=======
delete a data successful
Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052

 

0 0
原创粉丝点击