solr-hbase二级索引及查询解决方案(二)

11 篇文章 0 订阅
2 篇文章 0 订阅

上一篇搭建了hbase的二级索引功能,只要hbase中有写数据,其就会自动的增量同步索引.
接下来是根据索引查询hbase中数据.
实测,查询索引数据,确实相当快,main方法测试,7200条数据,只需要1秒.查询hbase数据,同样数量数据,25个列,用了5秒,之前我们在3台集群上测试hbase查询,1万条,用了3秒,这里应该还有提升空间.
一般情况下,如果设置分页,那么这种速度是够用的,不过我们的情况多是把查询出的数据作为下一步计算的中间数据,因此,对查询效率要求比较高.还有,数据的导出,也会需要尽量快.

solr查询工具类:

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

import com.util.Logger;

public class SolrQueryUtils {
    protected static Logger logger = Logger.getLogger(SolrQueryUtils.class);
     /**
     * solr http服务地址
     */
    private static String SOLR_URL;

    /**
     * solr的core
     */
    private static String SOLR_CORE;

    static {

        String configName = "/hbase_solr.properties";
        PropertiesLoader propertiesLoader = new PropertiesLoader(configName);
        try {
            SOLR_URL = propertiesLoader.getProperty("SOLR_URL");
            SOLR_CORE = propertiesLoader.getProperty("SOLR_CORE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 主函数入口
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // 1.测试插入文档
        /*Map<String, String> map = new HashMap<String, String>();
        map.put("id", "00001");
        map.put("name", "lijie");
        map.put("age", "24");
        map.put("addr", "深圳");
        addDocument(map, SOLR_CORE);

        // 2.通过bean添加document
        List<FlightEntity> persons = new ArrayList<FlightEntity>();
        persons.add(new FlightEntity());
        persons.add(new FlightEntity());
        addDocumentByBean(persons, SOLR_CORE);

        // 3.根据id集合删除索引
        List<String> ids = new ArrayList<String>();
        ids.add("00001");
        ids.add("00002");
        ids.add("00003");
        deleteDocumentByIds(ids, SOLR_CORE);*/

        Map<String, String> queryMap = new HashMap<String, String>();
        // 4.查询
        getDocument(queryMap);


    }

    /**
     * 获取solr服务
     * 
     * @return
     */
    public static HttpSolrClient getSolrClient(String core) {
        HttpSolrClient hsc = new HttpSolrClient(SOLR_URL + core);
        return hsc;
    }

    /**
     * 添加文档
     * 
     * @param map
     * @param core
     * @throws Exception
     */
    public static void addDocument(Map<String, String> map, String core)
            throws Exception {
        SolrInputDocument sid = new SolrInputDocument();
        for (Entry<String, String> entry : map.entrySet()) {
            sid.addField(entry.getKey(), entry.getValue());
        }
        HttpSolrClient solrClient = getSolrClient("/" + core);
        solrClient.add(sid);
        commitAndCloseSolr(solrClient);
    }

    /**
     * 添加文档,通过bean方式
     * 
     * @param persons
     * @param core
     * @throws Exception
     */
    public static void addDocumentByBean(List<FlightEntity> persons, String core)
            throws Exception {
        HttpSolrClient solrClient = getSolrClient("/" + core);
        solrClient.addBeans(persons);
        commitAndCloseSolr(solrClient);
    }

    /**
     * 根据id集合删除索引
     * 
     * @param ids
     * @param core
     * @throws Exception
     */
    public static void deleteDocumentByIds(List<String> ids, String core)
            throws Exception {
        HttpSolrClient solrClient = getSolrClient("/" + core);
        solrClient.deleteById(ids);
        commitAndCloseSolr(solrClient);
    }

    /**   
     * @Title: getDocument   
     * @Description: TODO(查询)   
     * @param: @param core
     * @param: @throws Exception      
     * @return: void      
     * @throws   
     */
    public static Map<String, Object> getDocument(Map<String, String> queryMap) throws Exception {

        HttpSolrClient solrClient = getSolrClient("/" + SOLR_CORE);
        SolrQuery sq = new SolrQuery();
        Map<String, String> getQueryConditionMap = getQueryCondition(queryMap);
        // q查询
        sq.set("q", getQueryConditionMap.get("queryParams"));

        // filter查询
        sq.addFilterQuery(getQueryConditionMap.get("filterParams"));
        //sq.addFilterQuery("id:[1 to 3]");

        // 排序
        sq.setSort("id", SolrQuery.ORDER.asc);

        // 分页 从第0条开始取,取一条
        sq.setStart(Integer.parseInt(getQueryConditionMap.get("startRow")));
        sq.setRows(Integer.parseInt(getQueryConditionMap.get("rowCount")));

        // 设置高亮
        sq.setHighlight(true);

        // 设置高亮的字段
        sq.addHighlightField(getQueryConditionMap.get("highlightField"));

        // 设置高亮的样式
        sq.setHighlightSimplePre("<font color='red'>");
        sq.setHighlightSimplePost("</font>");

        QueryResponse result = solrClient.query(sq);

        SolrDocumentList results = result.getResults();

        logger.info("getDocument方法:一共查询到" + results.getNumFound() + "条记录");

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        for (SolrDocument solrDocument : results) {
            Map<String, Object> jsonObj = new HashMap<String, Object>();
            jsonObj.put("id", solrDocument.get("id"));      
            dataList.add(jsonObj);
            jsonObj = null;
        }
        resultMap.put("queryStatus", "1001");
        resultMap.put("queryDataDesc", "");
        // 获取查询的条数
        resultMap.put("queryDataCount", results.getNumFound());
        resultMap.put("queryDataContent", dataList);

        commitAndCloseSolr(solrClient);
        return resultMap;
    }


    /**   
     * @Title: getDocumentNoPage   
     * @Description: TODO(查询,默认取消分页)   
     * @param: @param queryMap
     * @param: @return
     * @param: @throws Exception      
     * @return: Map<String,Object>      
     * @throws   
     */
    public static Map<String, Object> getDocumentNoPage(Map<String, String> queryMap) throws Exception {

        HttpSolrClient solrClient = getSolrClient("/" + SOLR_CORE);
        SolrQuery sq = new SolrQuery();
        Map<String, String> getQueryConditionMap = getQueryCondition(queryMap);
        // q查询
        sq.set("q", getQueryConditionMap.get("queryParams"));

        // filter查询
        //sq.addFilterQuery(getQueryConditionMap.get("filterParams"));
        //sq.addFilterQuery("id:[1 to 3]");

        // 排序
        sq.setSort("id", SolrQuery.ORDER.asc);

        // 分页 从第0条开始取,取一条
        sq.setStart(Integer.parseInt(getQueryConditionMap.get("startRow")));
        Integer maxValue = 100000000;
        sq.setRows(maxValue);

        // 设置高亮
        sq.setHighlight(true);

        // 设置高亮的字段
        sq.addHighlightField(getQueryConditionMap.get("highlightField"));

        // 设置高亮的样式
        sq.setHighlightSimplePre("<font color='red'>");
        sq.setHighlightSimplePost("</font>");

        QueryResponse result = solrClient.query(sq);

        SolrDocumentList results = result.getResults();

        logger.info("getDocumentNoPage方法:一共查询到" + results.getNumFound() + "条记录");

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        for (SolrDocument solrDocument : results) {
            Map<String, Object> jsonObj = new HashMap<String, Object>();
            jsonObj.put("id", solrDocument.get("id"));     
            dataList.add(jsonObj);
            jsonObj = null;
        }
        resultMap.put("queryStatus", "1001");
        resultMap.put("queryDataDesc", "");
        // 获取查询的条数
        resultMap.put("queryDataCount", results.getNumFound());
        resultMap.put("queryDataContent", dataList);

        commitAndCloseSolr(solrClient);
        return resultMap;
    }


    /**
     * 提交以及关闭服务
     * 
     * @param solrClient
     * @throws Exception
     */
    private static void commitAndCloseSolr(HttpSolrClient solrClient)
            throws Exception {
        solrClient.commit();
        solrClient.close();
    }

    /**   
     * @Title: getQueryCondition   
     * @Description: TODO(获取查询条件)   
     * @param: @param queryCondition
     * @param: @return      
     * @return: String      
     * @throws   
     */
    public static Map<String, String> getQueryCondition(Map<String, String> queryMap){
        return queryMap;
    }
}

hbase查询工具类:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.utils.common.PropertiesLoader;

public class HBaseQueryUtils2 {

    public static Logger log = Logger.getLogger(HBaseQueryUtils2.class);
    private Map<String, HTableInterface> HTableInterfaces = new HashMap<String, HTableInterface>();
    private Object lockObj = StringUtil.getEmptyString();
    private Configuration conf = null;
    private HBaseAdmin admin = null;
    private HConnection connection = null;

    private List<String> tableNames;

    private static String configName = "/hbase_solr.properties";
    private String tableName;
    private String rowKeyStr;
    private String qualifierStr;
    private String valueStr;

    public HBaseQueryUtils2() {
        PropertiesLoader propertiesLoader = new PropertiesLoader(configName);

        conf = HBaseConfiguration.create();
        // 使用kafka连接hbase的配置
        try {
            conf.set("hbase.zookeeper.quorum",
                    propertiesLoader.getProperty("HBASE_ZOOKEEPER_QUORUM"));
            conf.set("hbase.table.sanity.checks", "true");
            // 提高RPC通信时长
            conf.setLong("hbase.rpc.timeout",
                    Integer.parseInt(propertiesLoader.getProperty("HBASE_RPC_TIMEOUT")));
            // 设置Scan缓存 默认10000条记录
            conf.setLong("hbase.client.scanner.caching", Integer
                    .parseInt(propertiesLoader.getProperty("HBASE_CLIENT_SCANER_CACHING")));
            conf.setLong("zookeeper.session.timeout", Integer
                    .parseInt(propertiesLoader.getProperty("ZOOKEEPER_SESSION_TIMEOUT")));
            tableNames = new ArrayList<String>();

            this.getAdmin();

            //获取表名
            String getTableName = propertiesLoader.getProperty("HBASE_TABLE_NAME");
            this.tableName = getTableName;
            this.rowKeyStr = propertiesLoader.getProperty("hbase.table.rowKeyStr");
            this.qualifierStr = propertiesLoader.getProperty("hbase.table.qualifierStr");
            this.valueStr = propertiesLoader.getProperty("hbase.table.param.valueStr");
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }


    /**   
     * @Title: getTableName   
     * @Description: TODO(获取表名)   
     * @param: @return      
     * @return: String      
     * @throws   
     */
    public String getTableName() {
        return this.tableName;
    }

    public void setTableName() throws FileNotFoundException, IOException {
        PropertiesLoader propertiesLoader = new PropertiesLoader(configName);
        String getTableName = propertiesLoader.getProperty("HBASE_TABLE_NAME");
        this.tableName = getTableName;
    }



    public String getRowKeyStr() {
        return this.rowKeyStr;
    }




    public void setRowKeyStr(String rowKeyStr) {
        this.rowKeyStr = rowKeyStr;
    }




    public String getQualifierStr() {
        return this.qualifierStr;
    }




    public void setQualifierStr(String qualifierStr) {
        this.qualifierStr = qualifierStr;
    }




    public String getValueStr() {
        return this.valueStr;
    }




    public void setValueStr(String valueStr) {
        this.valueStr = valueStr;
    }



    public HTableInterface getTable(String tableName) {
        try {
            if (StringUtil.isNullOrEmpty(tableName))
                throw new Exception("add data to the Hadoop, tableName does not allow for Null Or Empty");
            if (!this.HTableInterfaces.containsKey(tableName)) {
                synchronized (this.lockObj) {
                    if (!this.HTableInterfaces.containsKey(tableName)) {
                        HConnection connection = this.getConnection();
                        HTableInterface table = connection.getTable(Bytes.toBytes(tableName));
                        this.HTableInterfaces.put(tableName, table);
                    }
                }
            }
            return this.HTableInterfaces.get(tableName);
        } catch (Exception ex) {
            log.error(ex.getMessage());
            ex.printStackTrace();
        }
        return null;
    }

    private synchronized HConnection getConnection() throws IOException {
        if (this.connection == null) {
            this.connection = HConnectionManager.createConnection(conf);
        }
        return connection;
    }

    public synchronized HBaseAdmin getAdmin()
            throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        if (admin == null) {
            admin = new HBaseAdmin(this.getConnection().getConfiguration());
        }
        return admin;
    }


    /**
     * @author Administrator
     * @detail 简单的get查询
     * @date 2018-01-08
     * @param requestBean
     *            请求Bean实体
     * 
     */

    public Map<String, Map<String, List<HBaseRecord>>> singleGetQuery(String rowRey) {
        String mytableName = getTableName();
        Map<String, Map<String, List<HBaseRecord>>> tableRecordsMap = new HashMap<String, Map<String, List<HBaseRecord>>>();
        if (StringUtil.isNullOrEmpty(mytableName)) {
            log.error("tableName cann't be empty please check");
            System.out.println("tableName cann't be empty please check");
            return tableRecordsMap;
        }
        Get get = singleGetCondition(rowRey);
        List<Get> gets = new ArrayList<Get>();
        if (get != null) {
            gets.add(get);
        } else {
            log.error("there not any condition match request params");
        }
        return this.batchGetRecords(mytableName, gets);
    }


    /**
     * @author Administrator
     * @detail 简单的get查询
     * @date 2018-01-08
     * @param requestBean
     *            请求Bean实体
     * 
     */

    public Map<String, Map<String, List<HBaseRecord>>> batchRowKeyGetQuery(List<String> rowKeyList) {
        String mytableName = getTableName();
        Map<String, Map<String, List<HBaseRecord>>> tableRecordsMap = new HashMap<String, Map<String, List<HBaseRecord>>>();
        if (StringUtil.isNullOrEmpty(mytableName)) {
            log.error("tableName cann't be empty please check");
            System.out.println("tableName cann't be empty please check");
            return tableRecordsMap;
        }
        List<Get> gets = new ArrayList<Get>();

        for(String rowKey:rowKeyList) {
            Get get = singleGetCondition(rowKey);
            if (get != null) {
                gets.add(get);
            } else {
                log.error("there not any condition match request params");
            }
        }
        return this.batchGetRecords(mytableName, gets);
    }


    private Get singleGetCondition(String rowKey) {
        Get resultGet = null;
        if (!StringUtil.isNullOrEmpty(rowKey)) {
            resultGet = new Get(rowKey.getBytes());
        }
        return resultGet;
    }



    /**
     * TODO 写简单的get查询方法 批量的从表中get
     * 
     * @author libangqin
     * @date 2017-09-13
     * @param tableName
     *            gets 批量请求的get
     * @return Map<String, Map<String, List<HBaseRecord>>> 代表该table的表结构
     * @Detail 该方法主要提供给精确查询使用 知道、rowkey的情况下查询速度更快
     */
    // 查询的数据结构 Map<String, Map<String, List<HBaseRecord>>> dataMap
    public Map<String, Map<String, List<HBaseRecord>>> batchGetRecords(String tableName, List<Get> gets) {
        Map<String, Map<String, List<HBaseRecord>>> tableRecordsMap = new HashMap<String, Map<String, List<HBaseRecord>>>();
        try {
            if (!this.tableExists(tableName)) {
                log.info("hbase tableName:" + tableName + " is not exist");
                return tableRecordsMap;
            }
            HTableInterface table = this.getConnection().getTable(Bytes.toBytes(tableName));
            Result[] rss = table.get(gets);
            Map<String, List<HBaseRecord>> recordsMap = new HashMap<String, List<HBaseRecord>>();;
            for (Result rs : rss) {
                List<HBaseRecord> recordList = this.getResult(rs);
                String rowKey = Bytes.toString(rs.getRow());
                recordsMap.put(rowKey, recordList);
            }
            tableRecordsMap.put(tableName, recordsMap);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return tableRecordsMap;
    }



    /**
     * @param rs
     * @return
     */
    @SuppressWarnings("deprecation")
    private List<HBaseRecord> getResult(Result rs) {
        List<HBaseRecord> recordList = new ArrayList<HBaseRecord>();
        for (KeyValue kv : rs.raw()) {
            try {
                String rowKey = Bytes.toString(kv.getRow());
                String family = Bytes.toString(kv.getFamily());
                String qualifier = Bytes.toString(kv.getQualifier());
                long timestamp = kv.getTimestamp();
                String value = "";
                value = Bytes.toString(kv.getValue());
                HBaseRecord record = new HBaseRecord(rowKey, family, qualifier, value, timestamp);
                recordList.add(record);
            } catch (Exception e) {
            }
        }
        return recordList;
    }

    public boolean tableExists(String tableName) {
        try {
            if (StringUtil.isNullOrEmpty(tableName))
                throw new Exception(
                        "Judge whether the table exists from Hadoop,tableName does not allow for Null Or Empty");
            List<String> tables = this.getTableNames();
            if (tables != null && tables.size() > 0 && tables.contains(tableName))
                return true;
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return false;
    }

    /**
     * @author libangqin
     * @param void
     * @return List<String> tableNames
     * @detail 该方法主要是查询hbase服务器中所有的表名
     */
    public List<String> getTableNames() {
        try {
            if (this.tableNames == null || this.tableNames.size() <= 0) {
                synchronized (lockObj) {
                    if (this.tableNames == null || this.tableNames.size() <= 0) {
                        @SuppressWarnings("deprecation")
                        String[] list = getAdmin().getTableNames();
                        if (list != null && list.length > 0) {
                            this.tableNames.addAll(Arrays.asList(list));
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return this.tableNames;
    }

    /**
     * 添加作为查询结果的字段到get 也就是结果过滤条件
     * 
     * @param get
     * @param recordList
     * @return
     */
    public Get addColumnToGet(Get get, List<HBaseRecord> recordList) {
        for (HBaseRecord r : recordList) {
            HBaseRecord record = (HBaseRecord) r;
            if (StringUtil.isNullOrEmpty(record.getValue())) { // 作为查询结果
                if (!StringUtil.isNullOrEmpty(record.getFamily()) && !StringUtil.isNullOrEmpty(record.getQualifier()))
                    get.addColumn(Bytes.toBytes(record.getFamily()), Bytes.toBytes(record.getQualifier()));
                else if (!StringUtil.isNullOrEmpty(record.getFamily()))
                    get.addFamily(Bytes.toBytes(record.getFamily()));
            }
        }
        return get;
    }


    /**
     * 根据rowKeyList批量获取
     * */
    public JSONArray getDataListByRowKeyList(List<Map<String, Object>> dataList) {
        JSONArray dataArr = new JSONArray();
        List<String> rowKeyList = new ArrayList<String>(); 
        for(Map<String, Object> map : dataList){
            rowKeyList.add(String.valueOf(map.get("id")));
        }
        Map<String, Map<String, List<HBaseRecord>>> batchResultRecords = batchRowKeyGetQuery(rowKeyList);

        //放每个字段的数据
        try {
            for (Entry<String, Map<String, List<HBaseRecord>>> recordsMap : batchResultRecords.entrySet()) {
                String recordsMapKey = recordsMap.getKey();
                Map<String, List<HBaseRecord>> recordsMapValue = recordsMap.getValue();
                for (Entry<String, List<HBaseRecord>> recordEach : recordsMapValue.entrySet()) {
                    String rowkey1 = recordEach.getKey();
                    List<HBaseRecord> val1List = recordEach.getValue();                 

                    JSONObject jSONObject = new JSONObject();
                    //单条记录
                    jSONObject = parseHbaseRecord(val1List);
                    if(jSONObject.isEmpty())
                        log.error("---------getDataListByRowKeyList方法:hbase 获取单条记录异常 。异常rowkey:"+rowkey1);
                    dataArr.add(jSONObject);
                }
                System.out.println("recordsMapKey="+recordsMapKey);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("---------getDataListByRowKeyList方法:hbase 批量获取异常");
        }


        return dataArr;
    }


    public JSONObject parseHbaseRecord(List<HBaseRecord> val1List){
        JSONObject colsObj = new JSONObject();

        for(int i=0;i<val1List.size();i++){ //遍历List
            HBaseRecord hBaseRecord = val1List.get(i);
            //System.out.println(hBaseRecord.getString());
            if(hBaseRecord!=null){
                String[] colsArr = hBaseRecord.getString().split(",");

                String keyName = "";
                String keyValue = "";
                for(int j=0;j<colsArr.length;j++){
                    //System.out.println(colsArr[j]);
                    String[] str_jArr = colsArr[j].split("\\:");
                    if(rowKeyStr.equals(str_jArr[0]))
                        colsObj.put(rowKeyStr, str_jArr[1]); //比如:("rowKey","123456789")
                    if(qualifierStr.equals(str_jArr[0])){
                        keyName = str_jArr[1];

                    }
                    if(valueStr.equals(str_jArr[0])){
                        if(str_jArr.length>1)
                            keyValue = String.valueOf(str_jArr[1]);     
                        //如果数据的值含有冒号,比如08:00:00
                        if(str_jArr.length>2){
                            for(int k=2;k<str_jArr.length;k++){
                                keyValue = keyValue + ":" + str_jArr[k];
                            }
                        }
                    }                               
                    else{
                        continue;
                    }
                }
                colsObj.put(keyName, keyValue);//比如:("count","-10")

            }
        }

        return colsObj;
    }

}

查询结果工具类:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.Logger;
import com.utils.date.DateUtils;
import com.utils.hbaseUtils.HBaseQueryUtils2;

public class DataQueryUtils {
    private static Logger logger = Logger.getLogger(DataQueryUtils.class);
    /**   
     * @Title: getQueryParams   
     * @Description: TODO(整理查询条件格式)   
     * @param: @param request
     * @param: @return      
     * @return: String      
     * @throws   
     */
    public static String getQueryParams(String col1,String col2){

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("col1:" + "*" + col1 + "*");
        stringBuffer.append(" AND " + "col2:" + "*" + col2 + "*");
        if(stringBuffer.length()==0)
            stringBuffer.append("*:*");
        return stringBuffer.toString();
    }


    public static String getFilterParams(String dataSourceID,String versionID){
        return "";
    }


    /**   
     * @Title: getModelRunDataFromHbaseNoPage   
     * @Description: TODO(通过solr索引(rowkey),去hbase查数据,默认取消分页)   
     * @param: @param requestParams
     * @param: @return
     * @param: @throws Exception      
     * @return: Map<String,Object>      
     * @throws   
     */
    public static Map<String, Object> getResultDataFromHbaseNoPage(Map<String, String> requestParams)throws Exception{  
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("resultStatus", "1001");
        Map<String, Object> solrReturnMap = SolrQueryUtils.getDocumentNoPage(requestParams);
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        dataList = (List<Map<String, Object>>) solrReturnMap.get("queryDataContent");
        Integer queryDataCount = Integer.parseInt(""+solrReturnMap.get("queryDataCount"));
        //获取到了rowkey,从hbase中查出数据
        JSONArray jsonArray = new JSONArray();
        HBaseQueryUtils2 hBaseQueryUtils2 = new HBaseQueryUtils2();         
        try {
            jsonArray = hBaseQueryUtils2.getDataListByRowKeyList(dataList);
            resultMap.put("resultData", jsonArray);
            resultMap.put("resultDesc", "");
            resultMap.put("pageDataCount", jsonArray.size());
            resultMap.put("resultDataCount", queryDataCount);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            logger.error("getResultDataFromHbaseNoPage方法:通过solr索引(rowkey),去hbase查数据,默认取消分页 异常");
        }


        return resultMap;
    }


    /**   
     * @Title: getDataFromHbase   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param col1
     * @param: @param col2
     * @param: @return      
     * @return: JSONArray     
     * @throws   
     */
    public static JSONArray getDataFromHbase(String col1,String col2){
        Map<String, String> requestParams = new HashMap<String, String>();  
        String queryParams = getQueryParams(dataSourceID,versionID);
        String filterParams = getFilterParams(dataSourceID,versionID);

        Integer startRow = 0;
        //检索条件===
        requestParams.put("queryParams", queryParams);
        requestParams.put("filterParams", filterParams);
        requestParams.put("startRow", ""+startRow);
        requestParams.put("rowCount", "");
        requestParams.put("highlightField", "id");

        Map<String, Object> resultMap;      
        try {       
            resultMap = getResultDataFromHbaseNoPage(requestParams);
            JSONArray jsonarr = new JSONArray();
            jsonarr = (JSONArray) resultMap.get("resultData");

            return jsonarr;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            logger.error("getDataFromHbase方法:查询数据异常");
        }
        return jsonarr;


    }


    public static void main(String[] args){
        String col1 = "128";
        String col2 = "119";

        System.out.println("开始时间:"+DateUtils.getDateFormatter());
        JSONArray jsonarr = getDataFromHbase(col1, col2);

        System.out.println("结束时间:"+DateUtils.getDateFormatter());
    }

}

HBaseQueryUtils2 中相关工具类:
……

这样,就能通过solr的java api查询出hbase 中数据了.

参考资料:
…..
非常感谢以上作者提供的细致解答.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值