zoukankan      html  css  js  c++  java
  • 【ElasticSearch篇】--ElasticSearch从初识到安装和应用

    一、前述

    ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口,在企业中全文搜索时,特别常用。

    二、常用概念

    cluster

    代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通信和与整个es集群通信是等价的。只需要在同一个网段之内启动多个es节点,就可以自动组成一个集群。默认情况下es会自动发现同一网段内的节点,自动组成集群。


    shards
    代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。


    replicas
    代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。


    recovery
    代表数据恢复或叫数据重新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。


    river
    代表es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。


    gateway
    代表es索引快照的存储方式,es默认是先把索引存放到内存中,当内存满了时再持久化到本地硬盘。gateway对索引快照进行存储,当这个es集群关闭再重新启动时就会从gateway中读取索引备份数据。es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。

    如果需要将数据落地到hadoop的hdfs需要先安装插件elasticsearch/elasticsearch-hadoop,然后再elasticsearch.yml配置
    gateway:     type: hdfs gateway:             hdfs:         uri: hdfs://localhost:9000

    discovery.zen
     代表es的自动发现节点机制,es是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。


    Transport
    代表es内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。

    索引(index)
            一个索引就是一个拥有几分相似特征的文档的集合。比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。在一个集群中,如果你想,可以定义任意多的索引。


    类型(type)
    在一个索引中,你可以定义一种或多种类型。一个类型是你的索引的一个逻辑上的分类/分区,其语义完全由你来定。通常,会为具有一组共同字段的文档定义一个类型。比如说,我们假设你运营一个博客平台并且将你所有的数据存储到一个索引中。在这个索引中,你可以为用户数据定义一个类型,为博客数据定义另一个类型,当然,也可以为评论数据定义另一个类型。
            
    文档(document)
     一个文档是一个可被索引的基础信息单元。比如,你可以拥有某一个客户的文档,某一个产品的一个文档,当然,也可以拥有某个订单的一个文档。文档以JSON(Javascript Object Notation)格式来表示,而JSON是一个到处存在的互联网数据交互格式。  在一个index/type里面,只要你想,你可以存储任意多的文档。注意,尽管一个文档,物理上存在于一个索引之中,文档必须被索引/赋予一个索引的type。

    三、安装教程

    1、前提

    只允许普通用户操作,不允许root用户

    注意:因为elasticsearch有远程执行脚本的功能所以容易中木马病毒,所以不允许用root用户启动,root用户是起不来的,赋权限,用一般的用户启动

    要配置network.host才能别的机器或者网卡访问,否则只能是127.0.0.1或者localhost访问,这里配置成自己的局域网ip

    注意配置yml结尾的配置文件都需要冒号后面加空格才行

    2、先要安装一个解压工具:

    yum install unzip

    3、不同节点间创建相同目录和用户

    下载elasticsearch-2.0.1.tar.zip不要用root解压,需要切换用户

     

    root共同创建 es 目录(共享模式):

     mkdir /opt/soft/es

    3个节点同时添加(共享命令模式)用户并提供密码:

    useradd sxt

    echo sxt | passwd --stdin sxt

    改变目录权限(共享命令模式):

     chown  sxt:sxt es

    4、解压并配置

    切换用户为sxt
    注意配置yml结尾的配置文件都需要冒号后面加空格才行
    使用sxt这个用户解压并进入es 目录的config配置目录修改配置文件config/elasticsearch.yml:注意:这个文件的所有行的:的后面,一定要有一个空格!

    如果要配置集群需要两个节点上的elasticsearch配置的cluster.name相同,都启动可以自动组成集群,这里如果不改cluster.name则默认是cluster.name=elasticsearch,

    nodename随意取但是集群内的各节点不能相同。

     主机和端口

    添加防脑裂配置: 如果不配不知道具体数量,不好控制脑裂

    discovery.zen.ping.multicast.enabled: false

    discovery.zen.ping.unicast.hosts: ["192.168.133.101","192.168.133.102", "192.168.133.103"]

    discovery.zen.ping_timeout: 120s

    client.transport.ping_timeout: 60s

    完整配置如下:

    # ======================== Elasticsearch Configuration =========================
    #
    # NOTE: Elasticsearch comes with reasonable defaults for most settings.
    #       Before you set out to tweak and tune the configuration, make sure you
    #       understand what are you trying to accomplish and the consequences.
    #
    # The primary way of configuring a node is via this file. This template lists
    # the most important settings you may want to configure for a production cluster.
    #
    # Please see the documentation for further information on configuration options:
    # <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
    #
    # ---------------------------------- Cluster -----------------------------------
    #
    # Use a descriptive name for your cluster:
    #
    cluster.name: CKL_elasticsearch
    #
    # ------------------------------------ Node ------------------------------------
    #
    # Use a descriptive name for the node:
    #
    node.name: node01
    #
    # Add custom attributes to the node:
    #
    # node.rack: r1
    #
    # ----------------------------------- Paths ------------------------------------
    #
    # Path to directory where to store the data (separate multiple locations by comma):
    #
    # path.data: /path/to/data
    #
    # Path to log files:
    #
    # path.logs: /path/to/logs
    #
    # ----------------------------------- Memory -----------------------------------
    #
    # Lock the memory on startup:
    #
    # bootstrap.mlockall: true
    #
    # Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
    # available on the system and that the owner of the process is allowed to use this limit.
    #
    # Elasticsearch performs poorly when the system is swapping the memory.
    #
    # ---------------------------------- Network -----------------------------------
    #
    # Set the bind address to a specific IP (IPv4 or IPv6):
    #
    # network.host: 192.168.0.1
    #
    # Set a custom port for HTTP:
    #
    http.port: 9200
    #
    # For more information, see the documentation at:
    # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
    #
    # --------------------------------- Discovery ----------------------------------
    #
    # Pass an initial list of hosts to perform discovery when new node is started:
    # The default list of hosts is ["127.0.0.1", "[::1]"]
    #
    # discovery.zen.ping.unicast.hosts: ["host1", "host2"]
    #
    # Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
    #
    # discovery.zen.minimum_master_nodes: 3
    #
    # For more information, see the documentation at:
    # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
    #
    # ---------------------------------- Gateway -----------------------------------
    #
    # Block initial recovery after a full cluster restart until N nodes are started:
    #
    # gateway.recover_after_nodes: 3
    #
    # For more information, see the documentation at:
    # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
    #
    # ---------------------------------- Various -----------------------------------
    #
    # Disable starting multiple nodes on a single system:
    #
    # node.max_local_storage_nodes: 1
    #
    # Require explicit names when deleting indices:
    #
    # action.destructive_requires_name: true
    discovery.zen.ping.multicast.enabled: false 
    discovery.zen.ping.unicast.hosts: ["192.168.31.101","192.168.133.102", "192.168.133.103"]
    discovery.zen.ping_timeout: 120s
    client.transport.ping_timeout: 60s

     5、分发节点并更改不同节点的es配置中namenode名称

     把配置和文件都分发给别的节点,注意也必须以普通用户身份:
     如果你是用root分发,那边会以root身份写入磁盘系统。

     以sxt用户身份登录

    如果出现如下问题,是因为对方节点目录不是sxt用户 用chown sxt:sxt es更改即可

     

    6、安装插件

    1、es 是rest方式,采用Jason格式,可读性不强,添加head插件,以图表方式显示:
    es目录原来没有plugins,从互联网下载:
    bin/plugin install mobz/elasticsearch-head

    注意:从解压es到操作这个包都必须是普通用户,因为这个过程会创建plugins目录,如果是你root创建,这个就成了root用户权限控制了,会有问题
    下载后:

    2、以上没有成功则使用第二种方式安装

    wget  https://github.com/mobz/elasticsearch-head/archive/master.zip

    unzip master.zip

    mv elasticsearch-head-master /opt/soft/es/elasticsearch-2.2.1/plugins下面

    然后将整个目录移动到Es的plugins文件下

    测试插件效果
    http://ip:9200/_plugin/head
    http://192.168.31.101:9200/_plugin/head?pretty

  • 相关阅读:
    Codeforces Round #401 (Div. 2)【A,B,C,D】
    HDU2874【LCA(模板)】
    ZOJ2898【折半搜索】
    zoj2901【DP·二进制优化】
    萌新笔记之鸽巢原理及其应用
    codeforces 547B【单调栈】
    Codeforces631C【栈维护+瞎搞】
    每天一节组合数学
    HDU 1506【单调栈】
    HDU 3410【单调栈】
  • 原文地址:https://www.cnblogs.com/LHWorldBlog/p/8527015.html
Copyright © 2011-2022 走看看