码迷,mamicode.com
首页 > Windows程序 > 详细

HBase Client API使用入门(二)---查询及过滤器

时间:2014-05-28 01:43:17      阅读:574      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

相关知识


 

 

创建表插入数据删除等见:http://www.cnblogs.com/wishyouhappy/p/3735077.html

HBase API简介见:http://www.cnblogs.com/wishyouhappy/p/3753347.html

 

按行、列簇等查询


 

 

bubuko.com,布布扣
package wish.hbase;

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.HTablePool;
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 HBaseQuerydata
{
    static Configuration cfg = HBaseConfiguration.create();
    static {
        
        cfg.set("hbase.zookeeper.quorum", "192.168.1.95");
        cfg.set("hbase.zookeeper.property.clientPort", "2181");
    }

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

        for (KeyValue kv : rs.raw())
        {
            System.out.println("rowkey:        " + 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 queryByRowKeyFamily(String tablename, String rowKey, String family) throws IOException
    {
        HTable table = new HTable(cfg, 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("rowkey:        " + 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 queryByRowKeyFamilyColumn(String tablename, String rowKey, String family, String column)
            throws IOException
    {
        HTable table = new HTable(cfg, 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("rowkey:        " + 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 queryAll(String tableName) { 
        HTablePool pool = new HTablePool(cfg, 1000); 
        try { 
            ResultScanner rs = pool.getTable(tableName).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 main(String[] args) throws Exception
    {
      System.out.println("******************************queryall******************************");
        queryAll("wishtest1");System.out.println("******************************query by rowkey******************************");
        queryByRowKey("wishTest1", "2");
        System.out.println("******************************query by rowkey family******************************");
        queryByRowKeyFamily("wishTest1", "2","name");
        System.out.println("******************************query by rowkey family column******************************");
        queryByRowKeyFamilyColumn("wishTest1", "6","score","Chinese");
    }
}
bubuko.com,布布扣

 

输出如下:

bubuko.com,布布扣
******************************queryall******************************
rowkey:****1121
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:*1111
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:1
列:age     值:20
列:gender     值:male
列:name     值:rain
rowkey:2
列:age     值:20
列:gender     值:female
列:name     值:wish
rowkey:3
列:age     值:20
列:gender     值:male
列:name     值:Joey
rowkey:4
列:age     值:20
列:gender     值:male
列:name     值:Jenny
列:score     值:90
列:score     值:100
列:score     值:100
列:score     值:90
rowkey:5
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:6
列:age     值:20
列:gender     值:male
列:name     值:Joey
列:score     值:100
列:score     值:100
列:score     值:90
******************************query by rowkey******************************
rowkey:        2
Column Family: age
Column       : 
value        : 20
rowkey:        2
Column Family: gender
Column       : 
value        : female
rowkey:        2
Column Family: name
Column       : 
value        : wish
******************************query by rowkey family******************************
rowkey:        2
Column Family: name
Column       : 
value        : wish
******************************query by rowkey family column******************************
rowkey:        6
Column Family: score
Column       : Chinese
value        : 100
bubuko.com,布布扣

 

行过滤器


 

 

使用过滤器可以提高操作表的效率,HBase中两种数据读取函数get()和scan()都支持过滤器

可以使用预定义好的过滤器或者是实现自定义过滤器

过滤器在客户端创建,通过RPC传送到服务器端,在服务器端执行过滤操作

 

行过滤器简单例子如下:

测试table中所有数据如下:

bubuko.com,布布扣
rowkey:****1121
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:*1111
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:1
列:age     值:20
列:gender     值:male
列:name     值:rain
rowkey:2
列:age     值:20
列:gender     值:female
列:name     值:wish
rowkey:3
列:age     值:20
列:gender     值:male
列:name     值:Joey
rowkey:4
列:age     值:20
列:gender     值:male
列:name     值:Jenny
列:score     值:90
列:score     值:100
列:score     值:100
列:score     值:90
rowkey:5
列:age     值:20
列:gender     值:male
列:name     值:Chander
列:score     值:120
列:score     值:100
列:score     值:99
rowkey:6
列:age     值:20
列:gender     值:male
列:name     值:Joey
列:score     值:100
列:score     值:100
列:score     值:90
bubuko.com,布布扣

 

eg1:

bubuko.com,布布扣
public static void rowFilterLESS_OR_EQUAL(String tableName, String columnFamily, String column, String condition){
        System.out.println("***********************start rowFilterLESS_OR_EQUAL***************************");
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
        
        Filter f = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes(condition)));
        scan.setFilter(f);
        HTablePool pool = new HTablePool(cfg, 1000); 
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
                for (; rs != null; rs = resultScanner.next())
                {
                    for (KeyValue kv : rs.list())
                    {
                        System.out.println("-------------------------------");
                        System.out.println("rowkey:        " + 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()));
                    }
                }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("***********************end rowFilterLESS_OR_EQUAL***************************");

    }
bubuko.com,布布扣
public static void main(String[] args) {
        String tableName = "wishTest1";
        HBaseFilter.rowFilterLESS_OR_EQUAL(tableName, "score", "Math", "4");
    }

 

输出:

bubuko.com,布布扣
***********************start rowFilterLESS_OR_EQUAL***************************

-------------------------------
rowkey:        ****1121
Column Family: score
Column       :Math
value        : 99
-------------------------------
rowkey:        *1111
Column Family: score
Column       :Math
value        : 99
-------------------------------
rowkey:        4
Column Family: score
Column       :Math
value        : 90
-------------------------------
rowkey:        5
Column Family: score
Column       :Math
value        : 99
***********************end rowFilterLESS_OR_EQUAL***************************
bubuko.com,布布扣

 

eg2:

bubuko.com,布布扣
    /*
     * rowFilterRegex
     */
    public static void rowFilterRegex(String tableName, String columnFamily, String column, String condition){
        System.out.println("***********************start rowFilterRegex***************************");
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
        
        Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("\\*."));
        scan.setFilter(f);
        HTablePool pool = new HTablePool(cfg, 1000); 
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
                for (; rs != null; rs = resultScanner.next())
                {
                    for (KeyValue kv : rs.list())
                    {
                        System.out.println("-------------------------------");
                        System.out.println("rowkey:        " + 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()));
                    }
                }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("***********************end rowFilterRegex***************************");

    }
bubuko.com,布布扣
public static void main(String[] args) {
        String tableName = "wishTest1";
        HBaseFilter.rowFilterRegex(tableName, "score", "Math", "\\*");
    }

输出:

bubuko.com,布布扣
***********************start rowFilterRegex***************************
-------------------------------
rowkey:        ****1121
Column Family: score
Column       :Math
value        : 99
-------------------------------
rowkey:        *1111
Column Family: score
Column       :Math
value        : 99
***********************end rowFilterRegex***************************
bubuko.com,布布扣

 

eg3:

bubuko.com,布布扣
/*
     * rowFilterSubstring
     */
    public static void rowFilterSubstring(String tableName, String columnFamily, String column, String condition){
        System.out.println("***********************start rowFilterSubstring***************************");
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
        
        Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("\\*"));
        scan.setFilter(f);
        HTablePool pool = new HTablePool(cfg, 1000); 
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
            for (; rs != null; rs = resultScanner.next())
            {
                for (KeyValue kv : rs.list())
                {
                    System.out.println("-------------------------------");
                    System.out.println("rowkey:        " + 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()));
                }
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("***********************end rowFilterSubstring***************************");
        
    }
bubuko.com,布布扣
    public static void main(String[] args) {
        String tableName = "wishTest1";
        HBaseFilter.rowFilterSubstring(tableName, "score", "Math", "\\*");
    }

输出同上

 

 列簇过滤器


 

 

bubuko.com,布布扣
public static void familyFilterLess(String tableName, String condition){
        Filter filter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
        Scan scan = new Scan();
        scan.setFilter(filter);
        HTablePool pool = new HTablePool(cfg,100);
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
            for (; rs != null; rs = resultScanner.next())
            {
                for (KeyValue kv : rs.list())
                {
                    System.out.println("-------------------------------");
                    System.out.println("rowkey:        " + 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()));
                }
            }
            
            resultScanner.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
    
    
    public static void main(String[] args) {
        String tableName = "wishTest1";
        HBaseFilter.familyFilterLess(tableName, "gender");
        //HBaseFilter.rowFilterSubstring(tableName, "score", "Math", "\\*");
    }
bubuko.com,布布扣

 

输出:

bubuko.com,布布扣
-------------------------------
rowkey:        ****1121
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        *1111
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        1
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        2
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        3
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        4
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        5
Column Family: age
Column       :
value        : 20
-------------------------------
rowkey:        6
Column Family: age
Column       :
value        : 20
bubuko.com,布布扣

 

 

列名过滤器


 

 

bubuko.com,布布扣
    /*
     * 列名过滤器
     */
    
    public static void qualifierFilterLess(String tableName, String condition){
        Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
        Scan scan = new Scan();
        scan.setFilter(filter);
        HTablePool pool = new HTablePool(cfg,100);
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
            for (; rs != null; rs = resultScanner.next())
            {
                for (KeyValue kv : rs.list())
                {
                    System.out.println("-------------------------------");
                    System.out.println("rowkey:        " + 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()));
                }
            }
            
            resultScanner.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
    
bubuko.com,布布扣

 

 值过滤器


 

 

bubuko.com,布布扣
    /*
     * 值过滤器
     */
    
    public static void valueFilterLess(String tableName, String condition){
        Filter filter = new ValueFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
        Scan scan = new Scan();
        scan.setFilter(filter);
        HTablePool pool = new HTablePool(cfg,100);
        try {
            ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
            Result rs = resultScanner.next();
            for (; rs != null; rs = resultScanner.next())
            {
                for (KeyValue kv : rs.list())
                {
                    System.out.println("-------------------------------");
                    System.out.println("rowkey:        " + 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()));
                }
            }
            
            resultScanner.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
bubuko.com,布布扣

 

 

HBase Client API使用入门(二)---查询及过滤器,布布扣,bubuko.com

HBase Client API使用入门(二)---查询及过滤器

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/wishyouhappy/p/3753760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!