使用spring-hbaseTemplate过滤器

来源:互联网 发布:淘宝分销食品要证吗 编辑:程序博客网 时间:2024/05/22 08:14

hbase过滤器的总结就不在这贴了,可以参考下面的链接,我这边把我工作时候用到的一些方法贴过来,主要使用到过滤器有:
1.FilterList 过滤器的集合,用于存放各种过滤器
2.RowFilter 行健正则过滤
3.PageFilter 分页过滤器,可以理解为过滤条数
4.设置开始的key和结束的key(也算是一种过滤吧)

    Scan scan = new Scan();    scan.setStartRow(startKey.getBytes(encoding));// 开始的key    scan.setStopRow(stopKey.getBytes(encoding));// 结束的key

详细的过滤器总结:http://blog.csdn.net/u012185296/article/details/47338549
以下是在spring-hbaseTemplate的基础上封装的代码:

HbaseService

package com.sinorail.srcloud.monitor.service.hbase;import java.io.IOException;import java.util.List;import org.apache.hadoop.hbase.client.Result;public interface HbaseService {    /**     * 查询全表的数据     * @param tablename     * @return     */    public List<Result> scaner(String tablename);    /**     * 根据rowKey查询单条记录     * @param tableName     * @param rowKey     * @return     */    public Result getRow(String tableName, String rowKey);     /**     * 根据regxKey正则匹配数据     * @param tableName     * @param regxKey     * @return     */    public List<Result> getRegexRow(String tableName,String regxKey);    /**     * 根据regxKey正则匹配数据,取出num条     * @param tableName     * @param regxKey      * @param num     * @return     */    public List<Result> getRegexRow(String tableName,String regxKey,int num);    /**     * 根据startKey和endKey的范围匹配数据     * @param tableName     * @param startKey     * @param stopKey     * @return     */    public List<Result> getStartRowAndEndRow(String tableName, String startKey, String stopKey);    /**     * 确定startKey和endKey的范围,根据regKey匹配数据     * @param tableName     * @param startKey     * @param stopKey     * @param regxKey     * @return     */    public List<Result> getRegexRow(String tableName, String startKey, String stopKey, String regxKey);    /**     * 确定startKey和endKey的范围,根据regKey匹配数据,取出num条     * @param tableName     * @param startKey     * @param stopKey     * @param regxKey     * @param num     * @return     */    public List<Result> getRegexRow(String tableName, String startKey, String stopKey, String regxKey,int num);    /**     * 添加数据     * @param rowKey     * @param tableName     * @param column     * @param value     */    public void addData(String rowKey, String tableName, String[] column, String[] value);    /**     * 删除记录     * @param tableName     * @param rowKeys     */    public void delRecord(String tableName, String... rowKeys);    /**     * 修改一条数据     * @param tableName     * @param rowKey     * @param familyName     * @param column     * @param value     * @throws IOException     */    public void updateTable(String tableName, String rowKey,String familyName, String column[], String value[]) throws IOException;    /**     * 查找最新的一条数据,或者说倒序查询     * @param tableName     * @return     */    public Result getNewRow(String tableName);    /**     * 正则查出所有匹配的key     * @param tableName     * @param regxKey     * @return     */    public List<String> queryKeys(String tableName,String regxKey);    /**     * 增加表中对应字段的值     * @param tableName     * @param cf     * @param rowKey     * @param column     * @param num     * @return     */    long incrQualifier(String tableName, String cf, String rowKey, String column, long num);    Result getNewRow(String tableName, String regxKey);}

HbaseServiceImpl

package com.sinorail.srcloud.monitor.service.hbase.impl;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HTableInterface;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.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.PageFilter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.RegexStringComparator;import org.apache.hadoop.hbase.filter.RowFilter;import org.apache.hadoop.hbase.util.Bytes;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.hadoop.hbase.HbaseTemplate;import org.springframework.data.hadoop.hbase.TableCallback;import org.springframework.stereotype.Service;import com.sinorail.srcloud.monitor.service.hbase.HbaseService;@Servicepublic class HbaseServiceImpl implements HbaseService {    @Autowired    private HbaseTemplate hbaseTemplate;    private final String encoding = "utf-8";    @Override    public List<Result> scaner(final String tableName) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                Scan scan = new Scan();                ResultScanner rs = table.getScanner(scan);                for(Result result:rs){                    list.add(result);                }                return list;            }        });    }    @Override    public Result getRow(final String tableName,  final String rowKey) {        return hbaseTemplate.execute(tableName, new TableCallback<Result>() {            @Override            public Result doInTable(HTableInterface table) throws Throwable {                Get get = new Get(rowKey.getBytes(encoding));                return table.get(get);            }        });    }    @Override    public List<Result> getRegexRow(final String tableName, final String regxKey) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                RegexStringComparator rc = new RegexStringComparator(regxKey);                RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, rc);                Scan scan = new Scan();                scan.setFilter(rowFilter);                ResultScanner rs = table.getScanner(scan);                for(Result result:rs){                    list.add(result);                }                return list;            }        });    }    @Override    public List<Result> getRegexRow(final String tableName, final String regxKey, final int num) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);                RegexStringComparator rc = new RegexStringComparator(regxKey);                RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);                if(num > 0){// 过滤获取的条数                    Filter filterNum = new PageFilter(num);// 每页展示条数                    fl.addFilter(filterNum);                    }                // 过滤器的添加                fl.addFilter(rf);                Scan scan = new Scan();                scan.setFilter(fl);// 为查询设置过滤器的list                ResultScanner rscanner = table.getScanner(scan);                for(Result result : rscanner){                    list.add(result);                }                return list;            }        });    }    @Override    public List<Result> getStartRowAndEndRow(final String tableName, final String startKey, final String stopKey) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                // 过滤器的添加                Scan scan = new Scan();                scan.setStartRow(startKey.getBytes(encoding));// 开始的key                scan.setStopRow(stopKey.getBytes(encoding));// 结束的key                ResultScanner rscanner = table.getScanner(scan);                for(Result result : rscanner){                    list.add(result);                }                return list;            }        });    }    @Override    public List<Result> getRegexRow(final String tableName, final String startKey, final String stopKey, final String regxKey) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                // 设置正则过滤器                RegexStringComparator rc = new RegexStringComparator(regxKey);                RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);                // 过滤器的添加                Scan scan = new Scan();                scan.setStartRow(startKey.getBytes(encoding));// 开始的key                scan.setStopRow(stopKey.getBytes(encoding));// 结束的key                scan.setFilter(rf);// 为查询设置过滤器的list                ResultScanner rscanner = table.getScanner(scan);                for(Result result : rscanner){                    list.add(result);                }                return list;            }        });    }    @Override    public List<Result> getRegexRow(final String tableName, final String startKey, final String stopKey, final String regxKey,final int num) {        return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {            List<Result> list = new ArrayList<>();            @Override            public List<Result> doInTable(HTableInterface table) throws Throwable {                FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);                // 设置正则过滤器                RegexStringComparator rc = new RegexStringComparator(regxKey);                RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);                if(num > 0){// 过滤获取的条数                    Filter filterNum = new PageFilter(num);// 每页展示条数                    fl.addFilter(filterNum);                    }                // 过滤器的添加                fl.addFilter(rf);                // 过滤器的添加                Scan scan = new Scan();                scan.setStartRow(startKey.getBytes(encoding));// 开始的key                scan.setStopRow(stopKey.getBytes(encoding));// 结束的key                scan.setFilter(fl);// 为查询设置过滤器的list                ResultScanner rscanner = table.getScanner(scan);                for(Result result : rscanner){                    list.add(result);                }                return list;            }        });    }    @Override    public void addData(final String rowKey, final String tableName, final String[] column, final String[] value) {         hbaseTemplate.execute(tableName, new TableCallback<String>() {            @Override            public String doInTable(HTableInterface table) throws Throwable {                Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey                for (int j = 0; j < column.length; j++) {                    put.add(Bytes.toBytes("cf1"), Bytes.toBytes(column[j]), Bytes.toBytes(value[j]));                }                table.put(put);                return "ok";            }        });    }public static void main(String[] args) throws UnsupportedEncodingException {    System.out.println(new String(Bytes.toBytes("土豆"),"utf-8"));}    @Override    public void delRecord(final String tableName, final String... rowKeys) {        hbaseTemplate.execute(tableName, new TableCallback<String>() {            @Override            public String doInTable(HTableInterface table) throws Throwable {                List<Delete> list = new ArrayList<>();                for(String rowKey : rowKeys){                    Delete del = new Delete(Bytes.toBytes(rowKey));                    list.add(del);                }                table.delete(list);                return "ok";            }        });    }    @Override    public void updateTable(final String tableName, final String rowKey, final String familyName, final String[] column, final String[] value)            throws IOException {        hbaseTemplate.execute(tableName, new TableCallback<String>() {            @Override            public String doInTable(HTableInterface table) throws Throwable {                Put put = new Put(Bytes.toBytes(rowKey));                for (int j = 0; j < column.length; j++) {                put.add(Bytes.toBytes(familyName), Bytes.toBytes(column[j]),                        Bytes.toBytes(value[j]));                }                table.put(put);                return "ok";            }        });    }    @Override    public Result getNewRow(final String tableName) {        return hbaseTemplate.execute(tableName, new TableCallback<Result>() {            @Override            public  Result doInTable(HTableInterface table) throws Throwable {                Filter filterNum = new PageFilter(1);// 每页展示条数                Scan scan = new Scan();                scan.setFilter(filterNum);                scan.setReversed(true);                ResultScanner scanner = table.getScanner(scan);                return scanner.next();            }        });    }    @Override    public Result getNewRow(final String tableName,final String regxKey) {        return hbaseTemplate.execute(tableName, new TableCallback<Result>() {            @Override            public  Result doInTable(HTableInterface table) throws Throwable {                FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);                RegexStringComparator rc = new RegexStringComparator(regxKey);                RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);                Filter filterNum = new PageFilter(1);// 每页展示条数                fl.addFilter(rf);                fl.addFilter(filterNum);                Scan scan = new Scan();                scan.setFilter(fl);                scan.setReversed(true);                ResultScanner scanner = table.getScanner(scan);                return scanner.next();            }        });    }    @Override    public List<String> queryKeys(final String tableName, final String regxKey) {        // TODO Auto-generated method stub        return hbaseTemplate.execute(tableName, new TableCallback<List<String>>() {            List<String> list = new ArrayList<>();            @Override            public List<String> doInTable(HTableInterface table) throws Throwable {                PrefixFilter filter = new PrefixFilter(regxKey.getBytes(encoding));                Scan scan = new Scan();                scan.setFilter(filter);                ResultScanner scanner = table.getScanner(scan);                for (Result rs : scanner) {                    list.add(new String(rs.getRow()));                }                return list;            }        });    }    @Override    public long incrQualifier(final String tableName, final String cf,final String rowKey,final String column,final long num) {        // TODO Auto-generated method stub        return hbaseTemplate.execute(tableName, new TableCallback<Long>() {            @Override            public Long doInTable(HTableInterface table) throws Throwable {                long qualifie =  table.incrementColumnValue(rowKey.getBytes(encoding), cf.getBytes(encoding), column.getBytes(encoding), num);                return qualifie;            }        });    }}

pom文件加入spring-habse相关的配置

<dependency>    <groupId>org.apache.hbase</groupId>    <artifactId>hbase-client</artifactId>    <version>0.98.5-hadoop2</version></dependency><dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-hadoop</artifactId>    <version>2.4.0.RELEASE</version></dependency><dependency>    <groupId>org.apache.hadoop</groupId>    <artifactId>hadoop-hdfs</artifactId>    <version>2.2.0</version></dependency>

srping整合hbase的xml文件

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:hdp="http://www.springframework.org/schema/hadoop"      xmlns:beans="http://www.springframework.org/schema/beans"      xmlns:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="      http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/hadoop       http://www.springframework.org/schema/hadoop/spring-hadoop.xsd      http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.1.xsd">      <!-- 默认的hadoopConfiguration,默认ID为hadoopConfiguration,且对于file-system等不需指定ref,自动注入hadoopConfiguration -->      <hdp:configuration>          fs.defaultFS=hdfs://sinorail/hbase        dfs.client.socket-timeout=600000          ha.zookeeper.quorum=w-pc-nf5280m4-69:2181,n-pc-nf5280m4-94:2181,n-pc-x3850x6-13:2181         ha.zookeeper.session-timeout.ms=300000          dfs.nameservices=sinorail          dfs.ha.namenodes.sinorail=nn1,nn2        dfs.namenode.rpc-address.sinorail.nn1=w-pc-nf5280m4-49:8020        dfs.namenode.rpc-address.sinorail.nn2=w-pc-nf5280m4-71:8020        dfs.namenode.http-address.sinorail.nn1=w-pc-nf5280m4-49:50070        dfs.namenode.http-address.sinorail.nn2=w-pc-nf5280m4-71:50070        dfs.client.failover.proxy.provider.sinorail=org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider      </hdp:configuration>  <!--     hadoop hdfs 操作类FileSystem,用来读写HDFS文件   -->         <hdp:file-system id="hadoop-cluster" configuration-ref="hadoopConfiguration" />    <!-- 配置zookeeper地址和端口 -->      <hdp:hbase-configuration configuration-ref="hadoopConfiguration" zk-quorum="w-pc-nf5280m4-69:2181,n-pc-nf5280m4-94:2181,n-pc-x3850x6-13:2181" zk-port="2181">          hbase.rootdir=hdfs://sinorail/hbase        hbase.cluster.distributed=true          zookeeper.session.timeout=30000          hbase.hregion.majorcompaction=0          hbase.regionserver.regionSplitLimit=1          dfs.client.socket-timeout=600000      </hdp:hbase-configuration>      <!-- 配置HbaseTemplate -->      <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">          <property name="configuration" ref="hbaseConfiguration" />      </bean>  </beans>  

有什么错误和不足,欢迎大家指正!

1 0