1. 程式人生 > >Java操作Hbase進行建表、刪表以及對資料進行增刪改查,條件查詢

Java操作Hbase進行建表、刪表以及對資料進行增刪改查,條件查詢

1、搭建環境

  新建JAVA專案,新增的包有:

   有關Hadoop的hadoop-core-0.20.204.0.jar

   有關Hbase的hbase-0.90.4.jar、hbase-0.90.4-tests.jar以及Hbase資源包中lib目錄下的所有jar包

2、主要程式

Hbase基本使用示例:

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. import java.io.IOException;   
  2. import java.util.ArrayList;   
  3. import java.util.List;   
  4. import org.apache.hadoop.conf.Configuration;   
  5. import org.apache.hadoop.hbase.HBaseConfiguration;   
  6. import org.apache.hadoop.hbase.HColumnDescriptor;   
  7. import org.apache.hadoop.hbase.HTableDescriptor;   
  8. import org.apache.hadoop.hbase.KeyValue;   
  9. import org.apache.hadoop.hbase.MasterNotRunningException;   
  10. import org.apache.hadoop.hbase.ZooKeeperConnectionException;   
  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.filter.Filter;   
  21. import org.apache.hadoop.hbase.filter.FilterList;   
  22. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;   
  23. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;   
  24. import org.apache.hadoop.hbase.util.Bytes;   
  25. publicclass HbaseTest {   
  26.     publicstatic Configuration configuration;   
  27.     static {   
  28.         configuration = HBaseConfiguration.create();   
  29.         configuration.set("hbase.zookeeper.property.clientPort""2181");   
  30.         configuration.set("hbase.zookeeper.quorum""192.168.1.100");   
  31.         configuration.set("hbase.master""192.168.1.100:600000");   
  32.     }   
  33.     publicstaticvoid main(String[] args) {   
  34.         // createTable("wujintao"); 
  35.         // insertData("wujintao"); 
  36.         // QueryAll("wujintao"); 
  37.         // QueryByCondition1("wujintao"); 
  38.         // QueryByCondition2("wujintao"); 
  39.         //QueryByCondition3("wujintao"); 
  40.         //deleteRow("wujintao","abcdef"); 
  41.         deleteByCondition("wujintao","abcdef");   
  42.     }   
  43.     publicstaticvoid createTable(String tableName) {   
  44.         System.out.println("start create table ......");   
  45.         try {   
  46.             HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);   
  47.             if (hBaseAdmin.tableExists(tableName)) {// 如果存在要建立的表,那麼先刪除,再建立 
  48.                 hBaseAdmin.disableTable(tableName);   
  49.                 hBaseAdmin.deleteTable(tableName);   
  50.                 System.out.println(tableName + " is exist,detele....");   
  51.             }   
  52.             HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);   
  53.             tableDescriptor.addFamily(new HColumnDescriptor("column1"));   
  54.             tableDescriptor.addFamily(new HColumnDescriptor("column2"));   
  55.             tableDescriptor.addFamily(new HColumnDescriptor("column3"));   
  56.             hBaseAdmin.createTable(tableDescriptor);   
  57.         } catch (MasterNotRunningException e) {   
  58.             e.printStackTrace();   
  59.         } catch (ZooKeeperConnectionException e) {   
  60.             e.printStackTrace();   
  61.         } catch (IOException e) {   
  62.             e.printStackTrace();   
  63.         }   
  64.         System.out.println("end create table ......");   
  65.     }   
  66.     publicstaticvoid insertData(String tableName) {   
  67.         System.out.println("start insert data ......");   
  68.         HTablePool pool = new HTablePool(configuration, 1000);   
  69.         HTable table = (HTable) pool.getTable(tableName);   
  70.         Put put = new Put("112233bbbcccc".getBytes());// 一個PUT代表一行資料,再NEW一個PUT表示第二行資料,每行一個唯一的ROWKEY,此處rowkey為put構造方法中傳入的值 
  71.         put.add("column1".getBytes(), null"aaa".getBytes());// 本行資料的第一列 
  72.         put.add("column2".getBytes(), null"bbb".getBytes());// 本行資料的第三列 
  73.         put.add("column3".getBytes(), null"ccc".getBytes());// 本行資料的第三列 
  74.         try {   
  75.             table.put(put);   
  76.         } catch (IOException e) {   
  77.             e.printStackTrace();   
  78.         }   
  79.         System.out.println("end insert data ......");   
  80.     }   
  81.     publicstaticvoid dropTable(String tableName) {   
  82.         try {   
  83.             HBaseAdmin admin = new HBaseAdmin(configuration);   
  84.             admin.disableTable(tableName);   
  85.             admin.deleteTable(tableName);   
  86.         } catch (MasterNotRunningException e) {   
  87.             e.printStackTrace();   
  88.         } catch (ZooKeeperConnectionException e) {   
  89.             e.printStackTrace();   
  90.         } catch (IOException e) {   
  91.             e.printStackTrace();   
  92.         }   
  93.     }   
  94.      publicstaticvoid deleteRow(String tablename, String rowkey)  {   
  95.         try {   
  96.             HTable table = new HTable(configuration, tablename);   
  97.             List list = new ArrayList();   
  98.             Delete d1 = new Delete(rowkey.getBytes());   
  99.             list.add(d1);   
  100.             table.delete(list);   
  101.             System.out.println("刪除行成功!");   
  102.         } catch (IOException e) {   
  103.             e.printStackTrace();   
  104.         }   
  105.     }   
  106.      publicstaticvoid deleteByCondition(String tablename, String rowkey)  {   
  107.             //目前還沒有發現有效的API能夠實現根據非rowkey的條件刪除這個功能能,還有清空表全部資料的API操作 
  108.     }   
  109.     publicstaticvoid QueryAll(String tableName) {   
  110.         HTablePool pool = new HTablePool(configuration, 1000);   
  111.         HTable table = (HTable) pool.getTable(tableName);   
  112.         try {   
  113.             ResultScanner rs = table.getScanner(new Scan());   
  114.             for (Result r : rs) {   
  115.                 System.out.println("獲得到rowkey:" + new String(r.getRow()));   
  116.                 for (KeyValue keyValue : r.raw()) {   
  117.                     System.out.println("列:" + new String(keyValue.getFamily())   
  118.                             + "====值:" + new String(keyValue.getValue()));   
  119.                 }   
  120.             }   
  121.         } catch (IOException e) {   
  122.             e.printStackTrace();   
  123.         }   
  124.     }   
  125.     publicstaticvoid QueryByCondition1(String tableName) {   
  126.         HTablePool pool = new HTablePool(configuration, 1000);   
  127.         HTable table = (HTable) pool.getTable(tableName);   
  128.         try {   
  129.             Get scan = new Get("abcdef".getBytes());// 根據rowkey查詢 
  130.             Result r = table.get(scan);   
  131.             System.out.println("獲得到rowkey:" + new String(r.getRow()));   
  132.             for (KeyValue keyValue : r.raw()) {   
  133.                 System.out.println("列:" + new String(keyValue.getFamily())   
  134.                         + "====值:" + new String(keyValue.getValue()));   
  135.             }   
  136.         } catch (IOException e) {   
  137.             e.printStackTrace();   
  138.         }   
  139.     }   
  140.     publicstaticvoid QueryByCondition2(String tableName) {   
  141.         try {   
  142.             HTablePool pool = new HTablePool(configuration, 1000);   
  143.             HTable table = (HTable) pool.getTable(tableName);   
  144.             Filter filter = new SingleColumnValueFilter(Bytes   
  145.                     .toBytes("column1"), null, CompareOp.EQUAL, Bytes   
  146.                     .toBytes("aaa")); // 當列column1的值為aaa時進行查詢 
  147.             Scan s = new Scan();   
  148.             s.setFilter(filter);   
  149.             ResultScanner rs = table.getScanner(s);   
  150.             for (Result r : rs) {   
  151.                 System.out.println("獲得到rowkey:" + new String(r.getRow()));   
  152.                 for (KeyValue keyValue : r.raw()) {   
  153.                     System.out.println("列:" + new String(keyValue.getFamily())   
  154.                             + "====值:" + new String(keyValue.getValue()));   
  155.                 }   
  156.             }   
  157.         } catch (Exception e) {   
  158.             e.printStackTrace();   
  159.         }   
  160.     }   
  161.     publicstaticvoid QueryByCondition3(String tableName) {   
  162.         try {   
  163.             HTablePool pool = new HTablePool(configuration, 1000);   
  164.             HTable table = (HTable) pool.getTable(tableName);   
  165.             List<Filter> filters = new ArrayList<Filter>();   
  166.             Filter filter1 = new SingleColumnValueFilter(Bytes   
  167.                     .toBytes("column1"), null, CompareOp.EQUAL, Bytes   
  168.                     .toBytes("aaa"));   
  169.             filters.add(filter1);   
  170.             Filter filter2 = new SingleColumnValueFilter(Bytes   
  171.                     .toBytes("column2"), null, CompareOp.EQUAL, Bytes   
  172.                     .toBytes("bbb"));   
  173.             filters.add(filter2);   
  174.             Filter filter3 = new SingleColumnValueFilter(Bytes   
  175.                     .toBytes("column3"), null, CompareOp.EQUAL, Bytes   
  176.                     .toBytes("ccc"));   
  177.             filters.add(filter3);   
  178.             FilterList filterList1 = new FilterList(filters);   
  179.             Scan scan = new Scan();   
  180.             scan.setFilter(filterList1);   
  181.             ResultScanner rs = table.getScanner(scan);   
  182.             for (Result r : rs) {   
  183.                 System.out.println("獲得到rowkey:" + new String(r.getRow()));   
  184.                 for (KeyValue keyValue : r.raw()) {   
  185.                     System.out.println("列:" + new String(keyValue.getFamily())   
  186.                             + "====值:" + new String(keyValue.getValue()));   
  187.                 }   
  188.             }   
  189.             rs.close();   
  190.         } catch (Exception e) {   
  191.             e.printStackTrace();   
  192.         }   
  193.     }   
  194. }  
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
 
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.MasterNotRunningException; 
import org.apache.hadoop.hbase.ZooKeeperConnectionException; 
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.filter.Filter; 
import org.apache.hadoop.hbase.filter.FilterList; 
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; 
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; 
import org.apache.hadoop.hbase.util.Bytes; 
 
public class HbaseTest { 
 
    public static Configuration configuration; 
    static { 
        configuration = HBaseConfiguration.create(); 
        configuration.set("hbase.zookeeper.property.clientPort", "2181"); 
        configuration.set("hbase.zookeeper.quorum", "192.168.1.100"); 
        configuration.set("hbase.master", "192.168.1.100:600000"); 
    } 
 
    public static void main(String[] args) { 
        // createTable("wujintao"); 
        // insertData("wujintao"); 
        // QueryAll("wujintao"); 
        // QueryByCondition1("wujintao"); 
        // QueryByCondition2("wujintao"); 
        //QueryByCondition3("wujintao"); 
        //deleteRow("wujintao","abcdef"); 
        deleteByCondition("wujintao","abcdef"); 
    } 
 
     
    public static void createTable(String tableName) { 
        System.out.println("start create table ......"); 
        try { 
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration); 
            if (hBaseAdmin.tableExists(tableName)) {// 如果存在要建立的表,那麼先刪除,再建立 
                hBaseAdmin.disableTable(tableName); 
                hBaseAdmin.deleteTable(tableName); 
                System.out.println(tableName + " is exist,detele...."); 
            } 
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); 
            tableDescriptor.addFamily(new HColumnDescriptor("column1")); 
            tableDescriptor.addFamily(new HColumnDescriptor("column2")); 
            tableDescriptor.addFamily(new HColumnDescriptor("column3")); 
            hBaseAdmin.createTable(tableDescriptor); 
        } catch (MasterNotRunningException e) { 
            e.printStackTrace(); 
        } catch (ZooKeeperConnectionException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        System.out.println("end create table ......"); 
    } 
 
     
    public static void insertData(String tableName) { 
        System.out.println("start insert data ......"); 
        HTablePool pool = new HTablePool(configuration, 1000); 
        HTable table = (HTable) pool.getTable(tableName); 
        Put put = new Put("112233bbbcccc".getBytes());// 一個PUT代表一行資料,再NEW一個PUT表示第二行資料,每行一個唯一的ROWKEY,此處rowkey為put構造方法中傳入的值 
        put.add("column1".getBytes(), null, "aaa".getBytes());// 本行資料的第一列 
        put.add("column2".getBytes(), null, "bbb".getBytes());// 本行資料的第三列 
        put.add("column3".getBytes(), null, "ccc".getBytes());// 本行資料的第三列 
        try { 
            table.put(put); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        System.out.println("end insert data ......"); 
    } 
 
     
    public static void dropTable(String tableName) { 
        try { 
            HBaseAdmin admin = new HBaseAdmin(configuration); 
            admin.disableTable(tableName); 
            admin.deleteTable(tableName); 
        } catch (MasterNotRunningException e) { 
            e.printStackTrace(); 
        } catch (ZooKeeperConnectionException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
 
    } 
     
     public static void deleteRow(String tablename, String rowkey)  { 
        try { 
            HTable table = new HTable(configuration, tablename); 
            List list = new ArrayList(); 
            Delete d1 = new Delete(rowkey.getBytes()); 
            list.add(d1); 
             
            table.delete(list); 
            System.out.println("刪除行成功!"); 
             
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
         
 
    } 
 
      
     public static void deleteByCondition(String tablename, String rowkey)  { 
            //目前還沒有發現有效的API能夠實現根據非rowkey的條件刪除這個功能能,還有清空表全部資料的API操作 
 
    } 
 
 
     
    public static void QueryAll(String tableName) { 
        HTablePool pool = new HTablePool(configuration, 1000); 
        HTable table = (HTable) pool.getTable(tableName); 
        try { 
            ResultScanner rs = table.getScanner(new Scan()); 
            for (Result r : rs) { 
                System.out.println("獲得到rowkey:" + new String(r.getRow())); 
                for (KeyValue keyValue : r.raw()) { 
                    System.out.println("列:" + new String(keyValue.getFamily()) 
                            + "====值:" + new String(keyValue.getValue())); 
                } 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
 
     
    public static void QueryByCondition1(String tableName) { 
 
        HTablePool pool = new HTablePool(configuration, 1000); 
        HTable table = (HTable) pool.getTable(tableName); 
        try { 
            Get scan = new Get("abcdef".getBytes());// 根據rowkey查詢 
            Result r = table.get(scan); 
            System.out.println("獲得到rowkey:" + new String(r.getRow())); 
            for (KeyValue keyValue : r.raw()) { 
                System.out.println("列:" + new String(keyValue.getFamily()) 
                        + "====值:" + new String(keyValue.getValue())); 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
 
     
    public static void QueryByCondition2(String tableName) { 
 
        try { 
            HTablePool pool = new HTablePool(configuration, 1000); 
            HTable table = (HTable) pool.getTable(tableName); 
            Filter filter = new SingleColumnValueFilter(Bytes 
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes 
                    .toBytes("aaa")); // 當列column1的值為aaa時進行查詢 
            Scan s = new Scan(); 
            s.setFilter(filter); 
            ResultScanner rs = table.getScanner(s); 
            for (Result r : rs) { 
                System.out.println("獲得到rowkey:" + new String(r.getRow())); 
                for (KeyValue keyValue : r.raw()) { 
                    System.out.println("列:" + new String(keyValue.getFamily()) 
                            + "====值:" + new String(keyValue.getValue())); 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
 
    } 
 
     
    public static void QueryByCondition3(String tableName) { 
 
        try { 
            HTablePool pool = new HTablePool(configuration, 1000); 
            HTable table = (HTable) pool.getTable(tableName); 
 
            List<Filter> filters = new ArrayList<Filter>(); 
 
            Filter filter1 = new SingleColumnValueFilter(Bytes 
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes 
                    .toBytes("aaa")); 
            filters.add(filter1); 
 
            Filter filter2 = new SingleColumnValueFilter(Bytes 
                    .toBytes("column2"), null, CompareOp.EQUAL, Bytes 
                    .toBytes("bbb")); 
            filters.add(filter2); 
 
            Filter filter3 = new SingleColumnValueFilter(Bytes 
                    .toBytes("column3"), null, CompareOp.EQUAL, Bytes 
                    .toBytes("ccc")); 
            filters.add(filter3); 
 
            FilterList filterList1 = new FilterList(filters); 
 
            Scan scan = new Scan(); 
            scan.setFilter(filterList1); 
            ResultScanner rs = table.getScanner(scan); 
            for (Result r : rs) { 
                System.out.println("獲得到rowkey:" + new String(r.getRow())); 
                for (KeyValue keyValue : r.raw()) { 
                    System.out.println("列:" + new String(keyValue.getFamily()) 
                            + "====值:" + new String(keyValue.getValue())); 
                } 
            } 
            rs.close(); 
 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
 
    } 
 
}

Hbase資料獲取示例:

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. /* 
  2.  * Need Packages: 
  3.  * commons-codec-1.4.jar 
  4.  * 
  5.  * commons-logging-1.1.1.jar 
  6.  * 
  7.  * hadoop-0.20.2-core.jar 
  8.  * 
  9.  * hbase-0.90.2.jar 
  10.  * 
  11.  * log4j-1.2.16.jar 
  12.  * 
  13.  * zookeeper-3.3.2.jar 
  14.  * 
  15.  */
  16. import java.io.IOException;  
  17. import java.util.ArrayList;  
  18. import java.util.List;  
  19. import org.apache.hadoop.conf.Configuration;  
  20. import org.apache.hadoop.hbase.HBaseConfiguration;  
  21. import org.apache.hadoop.hbase.KeyValue;  
  22. import org.apache.hadoop.hbase.client.Get;  
  23. import org.apache.hadoop.hbase.client.HTable;  
  24. import org.apache.hadoop.hbase.client.Result;  
  25. import org.apache.hadoop.hbase.client.ResultScanner;  
  26. import org.apache.hadoop.hbase.client.Scan;  
  27. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;  
  28. import org.apache.hadoop.hbase.filter.FilterList;  
  29. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;  
  30. import org.apache.hadoop.hbase.util.Bytes;  
  31. publicclass HbaseSelecter  
  32. {  
  33.     publicstatic Configuration configuration = null;  
  34.     static
  35.     {  
  36.         configuration = HBaseConfiguration.create();  
  37.         //configuration.set("hbase.master", "192.168.0.201:60000");
  38.         configuration.set("hbase.zookeeper.quorum""idc01-hd-nd-03,idc01-hd-nd-04,idc01-hd-nd-05");  
  39.         //configuration.set("hbase.zookeeper.property.clientPort", "2181");
  40.     }  
  41.     publicstaticvoid selectRowKey(String tablename, String rowKey) throws IOException  
  42.     {  
  43.         HTable table = new HTable(configuration, tablename);  
  44.         Get g = new Get(rowKey.getBytes());  
  45.         Result rs = table.get(g);  
  46.         for (KeyValue kv : rs.raw())  
  47.         {  
  48.             System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");  
  49.             System.out.println("Column Family: " + new String(kv.getFamily()));  
  50.             System.out.println("Column       :" + new String(kv.getQualifier()));  
  51.             System.out.println("value        : " + new String(kv.getValue()));  
  52.         }  
  53.     }  
  54.     publicstaticvoid selectRowKeyFamily(String tablename, String rowKey, String family) throws IOException  
  55.     {  
  56.         HTable table = new HTable(configuration, tablename);  
  57.         Get g = new Get(rowKey.getBytes());  
  58.         g.addFamily(Bytes.toBytes(family));  
  59.         Result rs = table.get(g);  
  60.         for (KeyValue kv : rs.raw())  
  61.         {  
  62.             System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");  
  63.             System.out.println("Column Family: " + new String(kv.getFamily()));  
  64.             System.out.println("Column       :" + new String(kv.getQualifier()));  
  65.             System.out.println("value        : " + new String(kv.getValue()));  
  66.         }  
  67.     }  
  68.     publicstaticvoid selectRowKeyFamilyColumn(String tablename, String rowKey, String family, String column)  
  69.             throws IOException  
  70.     {  
  71.         HTable table = new HTable(configuration, tablename);  
  72.         Get g = new Get(rowKey.getBytes());  
  73.         g.addColumn(family.getBytes(), column.getBytes());  
  74.         Result rs = table.get(g);  
  75.         for (KeyValue kv : rs.raw())  
  76.         {  
  77.             System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");  
  78.             System.out.println("Column Family: " + new String(kv.getFamily()));  
  79.             System.out.println("Column       :" + new String(kv.getQualifier()));  
  80.             System.out.println("value        : " + new String(kv.getValue()));  
  81.         }  
  82.     }  
  83.     publicstaticvoid selectFilter(String tablename, List<String> arr) throws IOException  
  84.     {  
  85.         HTable table = new HTable(configuration, tablename);  
  86.         Scan scan = new Scan();// 例項化一個遍歷器
  87.         FilterList filterList = new FilterList(); // 過濾器List
  88.         for (String v : arr)  
  89.         { // 下標0為列簇,1為列名,3為條件
  90.             String[] wheres = v.split(",");  
  91.             filterList.addFilter(new SingleColumnValueFilter(// 過濾器
  92.                     wheres[0].getBytes(), wheres[1].getBytes(),  
  93.                     CompareOp.EQUAL,// 各個條件之間是" and "的關係
  94.                     wheres[2].getBytes()));  
  95.         }  
  96.         scan.setFilter(filterList);  
  97.         ResultScanner ResultScannerFilterList = table.getScanner(scan);  
  98.         for (Result rs = ResultScannerFilterList.next(); rs != null; rs = ResultScannerFilterList.next())  
  99.         {  
  100.             for (KeyValue kv : rs.list())  
  101.             {  
  102.                 System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");  
  103.                 System.out.println("Column Family: " + new String(kv.getFamily()));  
  104.                 System.out.println("Column       :" + new String(kv.getQualifier()));  
  105.                 System.out.println("value        : " + new String(kv.getValue()));  
  106.             }  
  107.         }  
  108.     }  
  109.     publicstaticvoid main(String[] args) throws Exception  
  110.     {  
  111.         if(args.length < 2){  
  112.             System.out.println("Usage: HbaseSelecter table key");  
  113.             System.exit(-1);  
  114.         }  
  115.         System.out.println("Table: " + args[0] + " , key: " + args[1]);  
  116.         selectRowKey(args[0], args[1]);  
  117.         /* 
  118.         System.out.println("------------------------行鍵  查詢----------------------------------"); 
  119.         selectRowKey("b2c", "yihaodian1002865"); 
  120.         selectRowKey("b2c", "yihaodian1003396"); 
  121.         System.out.println("------------------------行鍵+列簇 查詢----------------------------------"); 
  122.         selectRowKeyFamily("riapguh", "使用者A", "user"); 
  123.         selectRowKeyFamily("riapguh", "使用者B", "user"); 
  124.         System.out.println("------------------------行鍵+列簇+列名 查詢----------------------------------"); 
  125.         selectRowKeyFamilyColumn("riapguh", "使用者A", "user", "user_code"); 
  126.         selectRowKeyFamilyColumn("riapguh", "使用者B", "user", "user_code"); 
  127.         System.out.println("------------------------條件 查詢----------------------------------"); 
  128.         List<String> arr = new ArrayList<String>(); 
  129.         arr.add("dpt,dpt_code,d_001"); 
  130.         arr.add("user,user_code,u_0001"); 
  131.         selectFilter("riapguh", arr); 
  132.         */
  133.     }  
  134. }  
/*
 * Need Packages:
 * commons-codec-1.4.jar
 *
 * commons-logging-1.1.1.jar
 *
 * hadoop-0.20.2-core.jar
 *
 * hbase-0.90.2.jar
 *
 * log4j-1.2.16.jar
 *
 * zookeeper-3.3.2.jar
 *
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseSelecter
{
    public static Configuration configuration = null;
    static
    {
        configuration = HBaseConfiguration.create();
        //configuration.set("hbase.master", "192.168.0.201:60000");
        configuration.set("hbase.zookeeper.quorum", "idc01-hd-nd-03,idc01-hd-nd-04,idc01-hd-nd-05");
        //configuration.set("hbase.zookeeper.property.clientPort", "2181");
    }

    public static void selectRowKey(String tablename, String rowKey) throws IOException
    {
        HTable table = new HTable(configuration, tablename);
        Get g = new Get(rowKey.getBytes());
        Result rs = table.get(g);

        for (KeyValue kv : rs.raw())
        {
            System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");
            System.out.println("Column Family: " + new String(kv.getFamily()));
            System.out.println("Column       :" + new String(kv.getQualifier()));
            System.out.println("value        : " + new String(kv.getValue()));
        }
    }

    public static void selectRowKeyFamily(String tablename, String rowKey, String family) throws IOException
    {
        HTable table = new HTable(configuration, tablename);
        Get g = new Get(rowKey.getBytes());
        g.addFamily(Bytes.toBytes(family));
        Result rs = table.get(g);
        for (KeyValue kv : rs.raw())
        {
            System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");
            System.out.println("Column Family: " + new String(kv.getFamily()));
            System.out.println("Column       :" + new String(kv.getQualifier()));
            System.out.println("value        : " + new String(kv.getValue()));
        }
    }

    public static void selectRowKeyFamilyColumn(String tablename, String rowKey, String family, String column)
            throws IOException
    {
        HTable table = new HTable(configuration, tablename);
        Get g = new Get(rowKey.getBytes());
        g.addColumn(family.getBytes(), column.getBytes());

        Result rs = table.get(g);

        for (KeyValue kv : rs.raw())
        {
            System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");
            System.out.println("Column Family: " + new String(kv.getFamily()));
            System.out.println("Column       :" + new String(kv.getQualifier()));
            System.out.println("value        : " + new String(kv.getValue()));
        }
    }

    public static void selectFilter(String tablename, List<String> arr) throws IOException
    {
        HTable table = new HTable(configuration, tablename);
        Scan scan = new Scan();// 例項化一個遍歷器
        FilterList filterList = new FilterList(); // 過濾器List

        for (String v : arr)
        { // 下標0為列簇,1為列名,3為條件
            String[] wheres = v.split(",");

            filterList.addFilter(new SingleColumnValueFilter(// 過濾器
                    wheres[0].getBytes(), wheres[1].getBytes(),

                    CompareOp.EQUAL,// 各個條件之間是" and "的關係
                    wheres[2].getBytes()));
        }
        scan.setFilter(filterList);
        ResultScanner ResultScannerFilterList = table.getScanner(scan);
        for (Result rs = ResultScannerFilterList.next(); rs != null; rs = ResultScannerFilterList.next())
        {
            for (KeyValue kv : rs.list())
            {
                System.out.println("--------------------" + new String(kv.getRow()) + "----------------------------");
                System.out.println("Column Family: " + new String(kv.getFamily()));
                System.out.println("Column       :" + new String(kv.getQualifier()));
                System.out.println("value        : " + new String(kv.getValue()));
            }
        }
    }

    public static void main(String[] args) throws Exception
    {
        if(args.length < 2){
            System.out.println("Usage: HbaseSelecter table key");
            System.exit(-1);
        }

        System.out.println("Table: " + args[0] + " , key: " + args[1]);
        selectRowKey(args[0], args[1]);

        /*
        System.out.println("------------------------行鍵  查詢----------------------------------");
        selectRowKey("b2c", "yihaodian1002865");
        selectRowKey("b2c", "yihaodian1003396");

        System.out.println("------------------------行鍵+列簇 查詢----------------------------------");
        selectRowKeyFamily("riapguh", "使用者A", "user");
        selectRowKeyFamily("riapguh", "使用者B", "user");

        System.out.println("------------------------行鍵+列簇+列名 查詢----------------------------------");
        selectRowKeyFamilyColumn("riapguh", "使用者A", "user", "user_code");
        selectRowKeyFamilyColumn("riapguh", "使用者B", "user", "user_code");

        System.out.println("------------------------條件 查詢----------------------------------");
        List<String> arr = new ArrayList<String>();
        arr.add("dpt,dpt_code,d_001");
        arr.add("user,user_code,u_0001");
        selectFilter("riapguh", arr);
        */
    }
}

Hbase 匯出特定列 示例(小量資料):

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. /* 
  2.  * Need Packages: 
  3.  * commons-codec-1.4.jar 
  4.  * 
  5.  * commons-logging-1.1.1.jar 
  6.  * 
  7.  * hadoop-0.20.2-core.jar 
  8.  * 
  9.  * hbase-0.90.2.jar 
  10.  * 
  11.  * log4j-1.2.16.jar 
  12.  * 
  13.  * zookeeper-3.3.2.jar 
  14.  * 
  15.  * Example: javac -classpath ./:/data/chenzhenjing/code/panama/lib/hbase-0.90.2.jar:/data/chenzhenjing/code/panama/lib/hadoop-core-0.20-append-for-hbase.jar:/data/chenzhenjing/code/panama/lib/commons-logging-1.0.4.jar:/data/chenzhenjing/code/panama/lib/commons-lang-2.4.jar:/data/chenzhenjing/code/panama/lib/commons-io-1.2.jar:/data/chenzhenjing/code/panama/lib/zookeeper-3.3.2.jar:/data/chenzhenjing/code/panama/lib/log4j-1.2.15.jar:/data/chenzhenjing/code/panama/lib/commons-codec-1.3.jar   DiffHbase.java    
  16.  */