一、安装 Apache Solr


图片分割线----------------------------------------------------------------------------------------------------------------------

图片分割线----------------------------------------------------------------------------------------------------------------------

图片分割线----------------------------------------------------------------------------------------------------------------------

二、配置 HBase 二级索引

1、增加HBase复制功能

打开启用编制索引、启用复制

对Hbase的表进行改造:

对于初次建立的表:

# 其中1表示开启replication功能,0表示不开启,默认为0
Create ‘device_safe_center_notice’,{NAME=>’alert_data’,VERSIONS=>3,REPLICATION_SCOPE=>1}

对于已经存在的表:

disable ‘device_safe_center_notice’alter ‘device_safe_center_notice’,{NAME=>’alert_data’,REPLICATION_SCOPE=>1}enable ‘device_safe_center_notice’

2、创建相应的 SolrCloud 集合

[root@node2 bin]# solrctl instancedir --generate /opt/module/hbase-indexer/solr_test[root@node2 conf]# pwd
/opt/module/hbase-indexer/solr_test/conf
[root@node2 conf]# vim managed-schema
添加:
# name格式为”表名_列族名_列名”
<field name="device_alert_notice_alert_data_src_ip" type="string" indexed="true" stored="true" multiValued="true"/><field name="device_alert_notice_alert_data_src_port" type="string" indexed="true" stored="true" multiValued="true"/><field name="device_alert_notice_alert_data_dest_ip" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="device_alert_notice_alert_data_dest_port" type="string" indexed="true" stored="true" multiValued="true"/>

打开硬提交:

[root@node2 conf]# vim solrconfig.xml
<!-- 修改 -->
<autoCommit><maxTime>${solr.autoCommit.maxTime:10000}</maxTime><openSearcher>false</openSearcher>
</autoCommit>
<autoSoftCommit><maxTime>${solr.autoSoftCommit.maxTime:1000}</maxTime>
</autoSoftCommit>

执行:

[root@node2 conf]# solrctl instancedir --create solr_test /opt/module/hbase-indexer/solr_test/
# 以下的collection名称很重要,后续代码中要用到
[root@node2 conf]# solrctl collection --create solr_test

在/opt/module/hbase-indexer/solr_test路径下创建morphline-hbase-mapper.xml文件:

[root@node2 solr_test]# pwd
/opt/module/hbase-indexer/solr_test
[root@node2 solr_test]# vim morphline-hbase-mapper.xml
<?xml version="1.0"?>
<indexer table="device_alert_notice">
​    <field name="device_alert_notice_alert_data_src_ip" value="alert_data:src_ip" type="string"/>
​    <field name="device_alert_notice_alert_data_src_port" value="alert_data:src_port" type="string"/>
​    <field name="device_alert_notice_alert_data_dest_ip" value="alert_data:dest_ip" type="string"/>
​    <field name="device_alert_notice_alert_data_dest_port" value="alert_data:dest_port" type="string"/>
</indexer>

执行:

[root@node2 solr_test]# hbase-indexer add-indexer -n solr_test_indexer -c /opt/module/hbase-indexer/solr_test/morphline-hbase-mapper.xml -cp solr.zk=node1,node2,node3:2181/solr -cp solr.collection=solr_test

至此,hbase新增数据已经同步添加solr索引,但是在添加solr索引之前就已经存在于hbase中的数据需要手动批量添加索引

3、批量添加索引

[root@node2 jars]# hadoop jar /opt/cloudera/parcels/CDH/jars/hbase-indexer-mr-1.5-cdh6.3.2-job.jar --hbase-indexer-zk node1,node2,node3:2181 --hbase-indexer-name solr_test_indexer --reducers 0

三、基于solr二级索引查询hbase

1、SolrQueryUtil.java

添加pom依赖:

     <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --><dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>7.4.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.solr/solr-core --><dependency><groupId>org.apache.solr</groupId><artifactId>solr-core</artifactId><version>7.4.0</version></dependency>

代码:

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;import java.util.Collections;
import java.util.Optional;/*** @Author: fyq* @Date: Create in 11:03 2021/6/30* @Desc: 获取solr中二级索引数据对hbase进行条件匹配查询工具类*/public class SolrQueryUtil {public static void main(String[] args) {try  {long start_time = System.currentTimeMillis();CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Collections.singletonList("192.168.18.211:2181,192.168.18.212:2181,192.168.18.213:2181"), Optional.of("/solr")).build();long middle_time = System.currentTimeMillis();// 查询语句SolrQuery query = new SolrQuery("device_alert_notice_alert_data_dest_ip:163.402.777.809");// 查询结果需要显示的行数query.setRows(20);// 其中“solr_test”为创建的二级索引的collection名QueryResponse response = cloudSolrClient.query("solr_test", query);long stop_time = System.currentTimeMillis();System.out.println("======== connect solr total use time : " + (stop_time - start_time));System.out.println("======== solr query use time : " + (stop_time - middle_time));for (SolrDocument result : response.getResults()) {// SolrQuery()内需要传入的参数格式为:hbase表名_列族名_列名:列值System.out.println(result.get("id"));System.out.println(PhoenixGetDataUtil.get((String) result.get("id")));}System.out.println(response.getResults().getNumFound());System.out.println(response.toString());cloudSolrClient.close();} catch (Exception e) {e.printStackTrace();}}
}

2、PhoenixGetDataUtil.java

添加pom依赖:

     <dependency><groupId>org.apache.phoenix</groupId><artifactId>phoenix-core</artifactId><version>5.0.0-HBase-2.0</version><exclusions><exclusion><groupId>org.glassfish</groupId><artifactId>javax.el</artifactId></exclusion><exclusion><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.glassfish</groupId><artifactId>javax.el</artifactId><version>3.0.1-b06</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.0.0</version></dependency>

代码:

import java.sql.*;
import java.util.Properties;/*** @Author: fyq* @Date: Create in 15:24 2021/6/24* @Desc: 使用phoenix驱动查询hbase数据的工具类*/public class PhoenixGetDataUtil {public static String getData(String id) {try {String driver = "org.apache.phoenix.jdbc.PhoenixDriver";Class.forName(driver);String url = "jdbc:phoenix:192.168.18.211,192.168.18.212,192.168.18.213:2181";//需保证客户端和服务端参数配置一致Properties props = new Properties();props.put("phoenix.schema.isNamespaceMappingEnabled", "true");props.setProperty("phoenix.query.timeoutMs", "1200000");props.setProperty("hbase.rpc.timeout", "1000");props.setProperty("hbase.client.scanner.timeout.period", "1200000");long start_time = System.currentTimeMillis();Connection connection = DriverManager.getConnection(url);long middle_time = System.currentTimeMillis();//查询数据PreparedStatement pstste = connection.prepareStatement("select \"src_ip\",\"dest_ip\" from \"device_alert_notice\" where \"ID\" = ?");pstste.setString(1,id);ResultSet resultSet = pstste.executeQuery();long stop_time = System.currentTimeMillis();System.out.println("============== connect hbase total use time :" + (stop_time - start_time));System.out.println("============== hbase query use time :" + (stop_time - middle_time));String result = null;while (resultSet.next()){result = "src_ip : "+resultSet.getString(1)+" dest_ip : "+resultSet.getString(2);}pstste.close();connection.close();return result;} catch (Exception e){e.printStackTrace();return null;}}
}

Apache Solr 建立 HBase 二级索引相关推荐

  1. 基于Solr的Hbase二级索引

    关于Hbase二级索引 HBase 是一个列存数据库,每行数据只有一个主键RowKey,无法依据指定列的数据进行检索.查询时需要通过RowKey进行检索,然后查看指定列的数据是什么,效率低下.在实际应 ...

  2. 使用solr构建hbase二级索引

    使用solr构建hbase二级索引 @(HBASE)[hbase, solr] 使用solr构建hbase二级索引 一概述 一业务场景描述 二技术方案 1技术方案一 2技术方案二 3关于索引的建议 二 ...

  3. CDH 6 安装 Hbase 二级索引 Solr + Key-Value Store Indexer

    目录 一.集群安装Solr +  Key-Value Store Indexer 二.创建Hbase二级索引 1.更改表结构,允许复制 2.创建相应的SolrCloud集合 3.创建 collecti ...

  4. hbase组合rowkey_「从零单排HBase 11」HBase二级索引解决方案

    HBase一个令人惋惜的地方,就是不支持二级索引.因此,社区有了很多补充方案来填补HBase的二级索引能力的缺陷. 今天,我们就来看看有哪些二级索引方案,通过对比各个方案的优缺点,并结合我们的具体场景 ...

  5. Hbase二级索引入门

    本节的标题也可能是"如果我的表行键看起来像这样,但我也想像这样查询我的表该怎么办". 列表上的一个常见示例是其中rowkey的格式为" user-timestamp&qu ...

  6. 基于ES的HBase二级索引方案

    HBase不支持多条件查询,不提供二级索引,难以满足用户对检索功能多样性和高效率两方面的需求.由索引模块的需求分析可知,本文解决通过,提出数据与索引的分离,利用HBase数据库的存储模式灵活多变,容纳 ...

  7. 华为HBase 二级索引调研

    1.Overall Solution 解决思想: 一个user table对应一个index table index的创建与更新全部在RS端的cp-processor里实现 核心思想:一个actual ...

  8. Hbase 二级索引 Solr int字段排序问题 can not sort on multivalued field

    Hbase Solr 同步二级索引后,进行int字段排序时报错 报错如下 {"responseHeader":{"zkConnected":true," ...

  9. Hbase二级索引 Solr 异常 The most likely cause is another Solr server (or another solr core in this server)

    solr查询数据时候报错,去服务器查看该节点日志 {"responseHeader":{"status":503,"QTime":3,&qu ...

  10. (转)HBase二级索引与Join

    二级索引与索引Join是Online业务系统要求存储引擎提供的基本特性.RDBMS支持得比较好,NOSQL阵营也在摸索着符合自身特点的最佳解决方案. 这篇文章会以HBase做为对象来探讨如何基于Hba ...

最新文章

  1. sift论文_卷积神经网络设计相关论文
  2. [20150309]使用冷备份做恢复的问题.txt
  3. 字符串匹配-BM算法改进SUNDAY--Boyer-Moore-Horspool-Sunday Aglorithm
  4. ActiveMQ入门-ActiveMQ环境搭建
  5. AngularJS学习---REST和自定义服务(REST and Custom Services) ngResource step 11
  6. SAS学习︱逻辑库、数据集创建与查看、数据库链接(SAS与R的code对照)
  7. 在一起计时器_古典计时器简介之一 qqtimer
  8. 紫色精品Bootstrap4 后台UI模板
  9. 深度学习(五十九)mxnet移植至android
  10. python mssqlserver_python for MSSQLserver
  11. 文件隐藏工具Funter for Mac使用方法
  12. linux按数量复制文件,linux下dd命令使用详解---用指定大小的块拷贝一个文件
  13. SATI--文献题录开源程序
  14. Android进阶——性能优化之APP启动速度优化实战总结(三)
  15. Java基础之序列化
  16. MX550性能怎么样 mx550 属于什么档次的显卡
  17. js根据日期计算星期几
  18. L1-030 一帮一 (15分) “一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生
  19. Attention机制学习(二)使用
  20. 金庸小说人物知识图谱构建——以《雪山飞狐》为例

热门文章

  1. Junit 5 实现testsuite
  2. 嵌入式系统中常用的通信接口技术
  3. matlab shapley函数,合作博弈shapley值讲解.ppt
  4. 阿里云短信验证码平台使用demo
  5. autocad2013安装闪退_Win10系统打开AutoCad闪退的两种修复方法
  6. 加载项目的时候提示:需要缺少的web组件才能进行加载
  7. C语言知识点思维导图
  8. 京东店铺数据分析工具推荐
  9. C4D插件X-Particles粒子特效(四)
  10. excel生成随机姓名