热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ElasticSerach初探第一篇认识ES+环境搭建+简单MySQL数据同步+SpringBoot整合ES

一、认识ElasticSearch是一个基于Lucene的开源搜索引擎,通过简单的RESTfulAPI来隐藏Lucene的复杂性。全文搜索,分析系统&#

一、认识ElasticSearch

是一个基于Lucene的开源搜索引擎,通过简单的RESTful API来隐藏Lucene的复杂性。全文搜索,分析系统,分布式数据库;elasticsearch的革命性就在于将这些单一的有用的技术整合成一个一体化的”实时的应用”。

 

二、ElasticSearch环境搭建

1.安装ElasticSearch

从官网下载,https://www.elastic.co/downloads/elasticsearch,选择自己想要的版本下载到本地,然后解压;

 

2.安装Kibana

从官网下载,https://www.elastic.co/downloads/kibana,这里选择和ElasticSearch一样的版本,然后解压。

Kibana是一个为 ElasticSearch 提供数据分析的 Web 接口。可使用它对日志进行高效的搜索、可视化、分析等各种操作。

 

3.安装X-pack

是ElasticSearch的一个扩展包,包括安全、警告、监视、图形、报告等功能集成在一个软件包,方便我们操作ElasticSearch。

到ElasticSearch目录,执行./bin/elasticsearch-plugin install x-pack;到Kibana目录,执行./bin/kibana-plugin install x-pack。

 

4.启动ElasticSearch和Kibana+访问

启动ElasticSearch:ES目录执行./bin/elasticsearch;

启动Kibana:Kibana目录执行./bin/kibana

 

访问Kibana:http://localhost:5601/



 

 

特别注意:登录的默认用户名和密码分别是:elastic和changeme 。

 

三、MySQL数据同步到ElasticSearch

自己在网上找了很多资料,最后选择采用logstash-input-jdbc的方式进行MySQL数据到ES的同步,已经做了一个初步的尝试,感觉还不错。现在仅仅尝试了一次同步一个表的数据,批量数据同步还需要研究。下面是我们进行数据同步的一些步骤:

 

步骤一:安装Ruby

因为logstash-input-jdbc是logstash的一个插件,使用ruby开发。自己本地是在MacOS上,安装方式:ruby -e "$(curl --insecure -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)";

 

步骤二:安装logstash-input-jdbc

首先从官网下载logstash:https://www.elastic.co/downloads/logstash,解压之后进入本地的logstash目录,到bin目录下面执行命令:./logstash-plugin install logstash-input-jdbc,安装的过程需要花费一点时间,静待一会儿,耐心等待,直到出现Installation successful。

 

步骤三:使用logstash-input-jdbc

进入本地的logstash/bin目录,创建mysql目录,并添加jdbc.confjdbc.sql两个文件,并且加入mysql驱动。下面贴出jdbc.conf和jdbc.sql文件的内容,这里实现了同步MySQL中一个表的数据到ES。

 

jdbc.conf文件内容如下:

input {  

    stdin {

    }  

 

    jdbc {  

      # mysql 数据库链接,test为数据库名  

      jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/test"  

      # 登录mysql数据库用户名和密码  

      jdbc_user => "root"  

      jdbc_password => "root"  

      # 驱动  

      jdbc_driver_library => "/Users/wuhoujian/Documents/myself/learning/elasticsearch/logstash-5.5.1/bin/mysql/mysql-connector-java-5.1.38.jar"  

      # 驱动类名  

      jdbc_driver_class => "com.mysql.jdbc.Driver"  

      jdbc_paging_enabled => "true"  

      jdbc_page_size => "50000"  

      # 执行的sql 文件路径+名称  

      statement_filepath => "/Users/wuhoujian/Documents/myself/learning/elasticsearch/logstash-5.5.1/bin/mysql/jdbc.sql"  

      # 设置监听间隔  各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新  

      #schedule => "* * * * *"  

      # 索引类型  

      type => "pattern"  

    }  

}  

  

filter {  

    json {  

        source => "pattern" 

    }  

}  

  

output {  

    elasticsearch {  

        # ES的IP地址及端口  

        hosts => ["localhost:9200"]  

        # 索引名称  

        index => "test"  

        # 自增ID 需要关联的数据库中有有一个id字段,对应索引的id号  

        document_id => "%{id}"  

    }  

    stdout {  

       # JSON格式输出  

        codec => json_lines  

    }  

 

}

 

jdbc.sql文件内容如下:

select * from 要同步的MySQL数据库中的表名

 

bin目录下执行命令:./logstash -f  ./mysql/jdbc.conf

 

四、Java HTTP REST API访问ElasticSearch

Java访问ES的两种方式:第一种——通过TransportClient访问;第二种——通过Jest访问。我们采用Jest(一个封装了对ES各种操作的轻量的框架)。

 

步骤一:pom.xml中添加相关依赖

<dependency>

    <groupId>io.searchboxgroupId>

    <artifactId>jestartifactId>

    <version>5.3.3version>

dependency>

 

<dependency>

<groupId>org.elasticsearchgroupId>

<artifactId>elasticsearchartifactId>

<version>5.5.1.0version>

dependency>

 

<dependency>

<groupId>org.apache.httpcomponentsgroupId>

<artifactId>httpcoreartifactId>

<version>4.4.7version>

dependency>

 

<dependency>

<groupId>org.apache.httpcomponentsgroupId>

<artifactId>httpclientartifactId>

<version>4.5.3version>

dependency>

 

<dependency>

<groupId>commons-logginggroupId>

<artifactId>commons-loggingartifactId>

<version>1.1.1version>

 

dependency>

 

步骤二&#xff1a;准备工具类&#xff0c;封装各种Jest操作ES的方法

 package com.jhzz.jizhitong.common.util;

 

import java.util.List;

 

import com.google.gson.GsonBuilder;

 

import io.searchbox.client.JestClient;

import io.searchbox.client.JestClientFactory;

import io.searchbox.client.JestResult;

import io.searchbox.client.config.HttpClientConfig;

import io.searchbox.core.Bulk;

import io.searchbox.core.BulkResult;

import io.searchbox.core.Count;

import io.searchbox.core.CountResult;

import io.searchbox.core.Delete;

import io.searchbox.core.DocumentResult;

import io.searchbox.core.Get;

import io.searchbox.core.Index;

import io.searchbox.core.Search;

import io.searchbox.core.SearchResult;

import io.searchbox.indices.CreateIndex;

import io.searchbox.indices.DeleteIndex;

import io.searchbox.indices.mapping.GetMapping;

import io.searchbox.indices.mapping.PutMapping;

 

/**

 * Jest操作ES工具类

 * 

 * &#64;author wuhoujian

 *

 */

public class JestUtil {

/**

* 获取JestClient对象

* &#64;return

*/

public static JestClient getJestClient() {

 

JestClientFactory factory &#61; new JestClientFactory();

factory.setHttpClientConfig(new HttpClientConfig.Builder("http://127.0.0.1:9200")

.gson(new GsonBuilder().setDateFormat("yyyy-MM-dd&#39;T&#39;hh:mm:ss").create()).connTimeout(1500)

.readTimeout(3000).multiThreaded(true).build());

return factory.getObject();

}

 

/**

* 创建索引

* &#64;param jestClient

* &#64;param indexName

* &#64;return

* &#64;throws Exception

*/

public boolean createIndex(JestClient jestClient, String indexName) throws Exception {

 

JestResult jr &#61; jestClient.execute(new CreateIndex.Builder(indexName).build());

return jr.isSucceeded();

}

 

/**

* Put映射

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param source

* &#64;return

* &#64;throws Exception

*/

public boolean createIndexMapping(JestClient jestClient, String indexName, String typeName, String source)

throws Exception {

 

PutMapping putMapping &#61; new PutMapping.Builder(indexName, typeName, source).build();

JestResult jr &#61; jestClient.execute(putMapping);

return jr.isSucceeded();

}

 

/**

* Get映射

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;return

* &#64;throws Exception

*/

public String getIndexMapping(JestClient jestClient, String indexName, String typeName) throws Exception {

 

GetMapping getMapping &#61; new GetMapping.Builder().addIndex(indexName).addType(typeName).build();

JestResult jr &#61; jestClient.execute(getMapping);

return jr.getJsonString();

}

 

/**

* 索引文档

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param objs

* &#64;return

* &#64;throws Exception

*/

public boolean index(JestClient jestClient, String indexName, String typeName, List objs) throws Exception {

 

Bulk.Builder bulk &#61; new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);

for (Object obj : objs) {

Index index &#61; new Index.Builder(obj).build();

bulk.addAction(index);

}

BulkResult br &#61; jestClient.execute(bulk.build());

return br.isSucceeded();

}

 

/**

* 搜索文档

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param query

* &#64;return

* &#64;throws Exception

*/

public SearchResult search(JestClient jestClient, String indexName, String typeName, String query)

throws Exception {

 

Search search &#61; new Search.Builder(query).addIndex(indexName).addType(typeName).build();

return jestClient.execute(search);

}

 

/**

* Count文档

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param query

* &#64;return

* &#64;throws Exception

*/

public Double count(JestClient jestClient, String indexName, String typeName, String query) throws Exception {

 

Count count &#61; new Count.Builder().addIndex(indexName).addType(typeName).query(query).build();

CountResult results &#61; jestClient.execute(count);

return results.getCount();

}

 

/**

* Get文档

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param id

* &#64;return

* &#64;throws Exception

*/

public static JestResult get(JestClient jestClient, String indexName, String typeName, String id) throws Exception {

 

Get get &#61; new Get.Builder(indexName, id).type(typeName).build();

return jestClient.execute(get);

}

 

/**

* Delete索引

* &#64;param jestClient

* &#64;param indexName

* &#64;return

* &#64;throws Exception

*/

public boolean delete(JestClient jestClient, String indexName) throws Exception {

 

JestResult jr &#61; jestClient.execute(new DeleteIndex.Builder(indexName).build());

return jr.isSucceeded();

}

 

/**

* Delete文档

* &#64;param jestClient

* &#64;param indexName

* &#64;param typeName

* &#64;param id

* &#64;return

* &#64;throws Exception

*/

public boolean delete(JestClient jestClient, String indexName, String typeName, String id) throws Exception {

 

DocumentResult dr &#61; jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());

return dr.isSucceeded();

}

}

 

 

步骤三&#xff1a;编码访问ES

JestResult jestResult &#61; JestUtil.get(JestUtil.getJestClient(), "jizhitong", "pattern", "5");

 

System.out.println(jestResult.getSourceAsString());



推荐阅读
  • 58同城的Elasticsearch应用与平台构建实践
    本文由58同城高级架构师于伯伟分享,由陈树昌编辑整理,内容源自DataFunTalk。文章探讨了Elasticsearch作为分布式搜索和分析引擎的应用,特别是在58同城的实施案例,包括集群优化、典型应用实例及自动化平台建设等方面。 ... [详细]
  • Spring Cloud因其强大的功能和灵活性,被誉为开发分布式系统的‘一站式’解决方案。它不仅简化了分布式系统中的常见模式实现,还被广泛应用于企业级生产环境中。本书内容详实,覆盖了从微服务基础到Spring Cloud的高级应用,适合各层次的开发者。 ... [详细]
  • Hadoop入门与核心组件详解
    本文详细介绍了Hadoop的基础知识及其核心组件,包括HDFS、MapReduce和YARN。通过本文,读者可以全面了解Hadoop的生态系统及应用场景。 ... [详细]
  • 本文介绍了Elasticsearch (ES),这是一个基于Java开发的开源全文搜索引擎。ES通过JSON接口提供服务,支持分布式集群管理和索引功能,特别适合大规模数据的快速搜索与分析。 ... [详细]
  • 本文详细介绍了Python编程语言的学习路径,涵盖基础语法、常用组件、开发工具、数据库管理、Web服务开发、大数据分析、人工智能、爬虫开发及办公自动化等多个方向。通过系统化的学习计划,帮助初学者快速掌握Python的核心技能。 ... [详细]
  • 本文作者分享了在阿里巴巴获得实习offer的经历,包括五轮面试的详细内容和经验总结。其中四轮为技术面试,一轮为HR面试,涵盖了大量的Java技术和项目实践经验。 ... [详细]
  • 本文深入探讨了MySQL中常见的面试问题,包括事务隔离级别、存储引擎选择、索引结构及优化等关键知识点。通过详细解析,帮助读者在面对BAT等大厂面试时更加从容。 ... [详细]
  • 创邻科技成功举办Graph+X生态合作伙伴大会,30余家行业领军企业共聚杭州
    9月22日,创邻科技在杭州举办“Graph+X”生态合作伙伴大会,汇聚了超过30家行业头部企业的50多位企业家和技术领袖,共同探讨图技术的前沿应用与发展前景。 ... [详细]
  • 本文介绍了Elasticsearch的基本概念,包括集群、节点、分片和副本的定义,并详细解释了如何执行文档和索引的CRUD操作。同时,文章还探讨了Elasticsearch与传统关系型数据库MySQL之间的对应关系,以及倒排索引的工作原理。 ... [详细]
  • MySQL 数据库迁移指南:从本地到远程及磁盘间迁移
    本文详细介绍了如何在不同场景下进行 MySQL 数据库的迁移,包括从一个硬盘迁移到另一个硬盘、从一台计算机迁移到另一台计算机,以及解决迁移过程中可能遇到的问题。 ... [详细]
  • 全面解析运维监控:白盒与黑盒监控及四大黄金指标
    本文深入探讨了白盒和黑盒监控的概念,以及它们在系统监控中的应用。通过详细分析基础监控和业务监控的不同采集方法,结合四个黄金指标的解读,帮助读者更好地理解和实施有效的监控策略。 ... [详细]
  • Java项目分层架构设计与实践
    本文探讨了Java项目中应用分层的最佳实践,不仅介绍了常见的三层架构(Controller、Service、DAO),还深入分析了各层的职责划分及优化建议。通过合理的分层设计,可以提高代码的可维护性、扩展性和团队协作效率。 ... [详细]
  • ElasticSearch 集群监控与优化
    本文详细介绍了如何有效地监控 ElasticSearch 集群,涵盖了关键性能指标、集群健康状况、统计信息以及内存和垃圾回收的监控方法。 ... [详细]
  • 字节跳动夏季招聘面试经验分享
    本文详细记录了字节跳动夏季招聘的面试经历,涵盖了一、二、三轮面试的技术问题及项目讨论,旨在为准备类似面试的求职者提供参考。 ... [详细]
  • SpringCloud电商平台开发指南:实战案例解析
    本文详细介绍了基于SpringCloud构建的电商平台项目,涵盖了从技术选型到项目部署的全流程,旨在帮助开发者快速掌握电商平台的开发技巧。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有