Solr+Hbase多条件查(优劣互补)
為什么要使用solr+hbase組合:
某電信項(xiàng)目中采用HBase來存儲(chǔ)用戶終端明細(xì)數(shù)據(jù),供前臺(tái)頁面即時(shí)查詢。HBase無可置疑擁有其優(yōu)勢,但其本身只對(duì)rowkey支持毫秒級(jí)的快速檢索,對(duì)于多字段的組合查詢卻無能為力。針對(duì)HBase的多條件查詢也有多種方案,但是這些方案要么太復(fù)雜,要么效率太低,本文只對(duì)基于Solr的HBase多條件查詢方案進(jìn)行測試和驗(yàn)證。
solr+habse組合的原理:
基于Solr的HBase多條件查詢?cè)砗芎唵?#xff0c;將HBase表中涉及條件過濾的字段和rowkey在Solr中建立索引,通過Solr的多條件查詢快速獲得符合過濾條件的rowkey值,拿到這些rowkey之后在HBASE中通過指定rowkey進(jìn)行查詢。
環(huán)境
1.????????????? 已搭建好的hadoop集群,3節(jié)點(diǎn)hadoop測試集群(見文檔hadoop2.5完全分布式集群搭建)
2.????????????? 在hadoop集群之上搭建hbase集群(文檔中hadoop2.5分布式中已包含)
3.????????????? 已搭建好的solrcloud集群,3節(jié)點(diǎn)solrcloud集群(見文檔solrcloud分布式集群)
4.????????????? 從oracle中導(dǎo)入數(shù)據(jù)到hbase中(可以通過普通java代碼或mapreduce,也可以直接使用工具sqoop)
5.????????????? 使用sqoop將oracle中的數(shù)據(jù)導(dǎo)入hbase中
sqoop實(shí)現(xiàn)數(shù)據(jù)從oracle導(dǎo)入hdfs(hbase)
?
sqoop import --append --connect jdbc:oracle:thin:@192.168.0.20:1521:orcl --username yqdev --password yq --m 1 --table c_text --columns id,url,title --hbase-create-table --hbase-table c_text --hbase-row-key id --column-family textinfo?
注:需要在hbase中先創(chuàng)建c_text表,創(chuàng)建列族textinfo;我只導(dǎo)入了id,url,title三列,其中id為rowkey.
6.????????????? 創(chuàng)建索引
從hbase中讀取數(shù)據(jù),將需要用作查詢字段添加索引到solr中(例如title)
????
/*** create solrIndex * * @throws IOException* @throws SolrServerException*/public static void addIndex() throws IOException, SolrServerException {// hbaseScan scan = new Scan();scan.addFamily(Bytes.toBytes(FAMILY_NAME));// scan.setCaching(500);scan.setCacheBlocks(false);ResultScanner rs = table.getScanner(scan);System.out.println("start......");Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();Long totalCount = 0l;for (Result r : rs) {SolrInputDocument doc = new SolrInputDocument();doc.addField("id", new String(r.getRow()));for (KeyValue kv : r.raw()) {String fieldName = new String(kv.getQualifier());String fieldValue = new String(kv.getValue());if (fieldName.equalsIgnoreCase("id")|| fieldName.equalsIgnoreCase("title")|| fieldName.equalsIgnoreCase("url")) {doc.addField(fieldName, fieldValue);}docs.add(doc);}if (docs.size() >= 1000) {cloudSolrServer.add(docs);cloudSolrServer.commit();totalCount = totalCount + docs.size();docs = new ArrayList<SolrInputDocument>();System.out.println("already deal with : " + totalCount);}}}
?
7.????????????? 查詢測試
?????
/*** 1.query solrIndex pass some condition 2.query data from hbase pass rowkey* * @throws IOException* @throws SolrServerException*/public static void query() throws IOException, SolrServerException {Get get = null;List<Get> list = new ArrayList<Get>();SolrQuery query = new SolrQuery("title:基金");query.setStart(0);query.setRows(40);QueryResponse response = cloudSolrServer.query(query);SolrDocumentList docs = response.getResults();System.out.println("total:" + docs.getNumFound());System.out.println("query time:" + response.getQTime());//get rowkey from solrfor (SolrDocument doc : docs) {get = new Get(Bytes.toBytes((String) doc.getFieldValue("id")));list.add(get);}//order rowkey query data from hbasefor (Get gt : list) {Result result = table.get(gt);byte[] value = result.getValue("textinfo".getBytes(),"title".getBytes());System.out.println("title------- \t" + new String(value));}}
hbase+solr多條件查詢的設(shè)計(jì)方案:
(利用hbase的大數(shù)據(jù)存儲(chǔ)和solr的強(qiáng)大的索引,達(dá)到互補(bǔ)的效果)
基于Solr的HBase多條件查詢?cè)砗芎唵?#xff0c;將HBase表中涉及條件過濾的字段和rowkey在Solr中建立索引,通過Solr的多條件查詢快速獲得符合過濾條件的rowkey值,拿到這些rowkey之后在HBASE中通過指定rowkey進(jìn)行查詢。
?
參考:http://www.cnblogs.com/chenz/articles/3229997.html
總結(jié)
以上是生活随笔為你收集整理的Solr+Hbase多条件查(优劣互补)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重庆一景区煮麻辣汤圆:下次元宵佳节还得等
- 下一篇: Storm集群部署