開發環境之ElasticSearch


查看更多開發環境配置,請點擊《開發環境配置大全》

拾章:ElasticSearch安裝教程

1)去官網下載ElasticSearch安裝包

本教程所有軟件使用的版本:5.6.8

http://www.elasticsearch.org/

選擇無中文目錄,解壓縮ElasticSearch安裝包即安裝完成;

 

2)配置環境變量

在原有的PATH下追加:D:\tensquare_APP\elasticsearch-5.6.8\elasticsearch-5.6.8\bin;

cmd執行elasticsearch,顯示兩個端口,java開發用9300,其他用9200;

瀏覽器輸入http://localhost:9200會返回數據,即啟動成功。

 

3)安裝圖形化界面前提環境

3.1 這個圖形化界面是由node.js做的,所以先安裝node.js

node.js是一個javascript的運行環境;

可以使前后端使用同一份js文件,但是后端一般會用java實現;

下載路徑:https://nodejs.org/zh-cn/download/

下載安裝包之后直接下一步安裝;dos下輸入node -v查看版本。

 

3.2 安裝npm

node包管理和分發的工具;

node.js使用npm安裝我們所依賴的js包;

vue中需要通過npm安裝webpack;

npm隨Node.js一起安裝;

但是要配置一些路徑,以便從遠程下載js包時下載到指定目錄,在Node文件夾下創建npm_modules和npm_cache文件夾,然后win+r,cmd,輸入npm config set prefix "D:\tensquare_APP\nodejs\npm_modules";繼續輸入執行npm config set cache "D:\tensquare_APP\nodejs\npm_cache"。

 

3.3 安裝cnpm

代替了npm,從國內淘寶鏡像下載js包;

dos命令下執行:npm install -g cnpm --registry=https://registry.npm.taobao.org

查看鏡像指向位置nrm ls,輸出結果前面有個*,表示指向的鏡像地址;

指定位置命令nrm use taobao(地址),更換地址。

 

3.4 將grunt安裝為全局命令

win+r+cmd,執行命令 npm install -g grunt-cli;

 

4)安裝圖形化界面(head插件)

網上搜索elasticsearch head下載即可,解壓縮到任意目錄下安裝完成;

安裝依賴:win+r+cmd,進入圖形化安裝包的路徑D:\tensquare_APP\elasticsearch-head-master\elasticsearch-head-master,執行命令 cnpm install;

啟動圖形化界面:win+r+cmd,進入圖形化安裝包的路徑D:\tensquare_APP\elasticsearch-head-master\elasticsearch-head-master,執行命令 grunt server;默認端口9100;瀏覽器輸入http://localhost:9100/即可訪問。

 

5)ElasticSearch配置允許跨域訪問

找到D:\tensquare_APP\elasticsearch-5.6.8\elasticsearch-5.6.8\config\elasticsearch.yml文件,在最下面添加下面2行代碼即可:

http.cors.enabled: true

http.cors.allow-origin: "*"

 

6)配置IK分詞器

下載IK分詞器安裝包:

https://github.com/medcl/elasticsearch-analysis-ik/releases

將IK分詞器的解壓后的插件包放到D:\tensquare_APP\elasticsearch-5.6.8\elasticsearch-5.6.8\plugins下即可

 

7)添加IK詞匯(比如一些網上流行詞)

在D:\tensquare_APP\elasticsearch-5.6.8\elasticsearch-5.6.8\plugins\ik\elasticsearch\config下創建一個文件,命名為myword.dic,在里面添加詞匯(如高富帥),然后在同級的IKAnalyzer.cfg.xml文件下啟用該文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <comment>IK Analyzer 擴展配置</comment>
  <!--用戶可以在這里配置自己的擴展字典 -->
  <entry key="ext_dict">myword.dic</entry>
  <!--用戶可以在這里配置自己的擴展停止詞字典-->
  <entry key="ext_stopwords"></entry>
  <!--用戶可以在這里配置遠程擴展字典 -->
  <!-- <entry key="remote_ext_dict">words_location</entry> -->
  <!--用戶可以在這里配置遠程擴展停止詞字典-->
  <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

測試是否生效:http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=高富帥

 

8)logstash(日志收集處理框架,同步索引)

ElasticSearch與Mysql數據同步(即將Mysql數據導入索引庫)

8.1 logstash的安裝

下載安裝包,解壓即可

 

8.2 logstash的命令

測試:進入安裝包bin目錄下(可配置環境變量),win+r+cmd,執行 logstash -e 'input { stdin { } } output { stdout {} }'

-e:表示直接執行

-f:表示執行本地的文件(因為文檔太多,一般通過這種方式導入)

 

8.3 同步索引

在logstash的安裝包下創建一個文件夾mysqlcode,在mysqlcode下創建mysql.conf配置文件,讓它配

置好要導入的數據庫相關信息;

input {
  jdbc {
      # mysql jdbc connection string to our backup databse
      # 連接哪個數據庫
      jdbc_connection_string => "jdbc:mysql://localhost:3306/tensquare_article?characterEncoding=UTF8"
      
      # the user we wish to excute our statement as
      # 賬號、密碼
      jdbc_user => "root"
      jdbc_password => "admins"
      
      # the path to our downloaded jdbc driver  
      # 數據庫驅動所在位置
      jdbc_driver_library => "D:\tensquare_APP\logstash-5.6.8\mysqlcode\mysql-connector-java-5.1.46.jar"
      # the name of the driver class for mysql
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      
      # 是否分頁
      jdbc_paging_enabled => "true"
      # 顯示多少條數據
      jdbc_page_size => "50"
      #以下對應着要執行的sql的絕對路徑。
      #statement_filepath => ""
      statement => "select id,title,content,state from tb_article"
      #定時字段 各字段含義(由左至右)分、時、天、月、年,全部為*默認含義為每分鍾都更新(測試結果,不同的話請留言指出)
      schedule => "* * * * *"
  }
}

output {
  elasticsearch {
      #ESIP地址與端口
      hosts => "localhost:9200" 
      #ES索引名稱(自己定義的)
      index => "tensquare_article"
      #自增ID編號
      document_id => "%{id}"
      document_type => "article"
  }
  stdout {
      #以JSON格式輸出
      codec => json_lines
  }
}

win+r+cmd,執行 logstash -f D:\tensquare_APP\logstash-5.6.8\mysqlcode\mysql.conf(其實就是

找到那個配置文件去執行導入,可能會卡住,耐心等待)

 

注意:刪除操作是不能同步的,所以一般要設置狀態,修改狀態,根據狀態查詢相應的信息即可

 

9)logstash 6.2.1版本

9.1 安裝logstash-input-jdbc插件

6.X版本開始不會自帶這個插件,這個插件由ruby開發,所以先下載ruby,然后cmd進入到logstash的bin目錄執

行命令:

.\logstash-plugin.bat install logstash-input-jdbc

 

9.2 配置文件

在D:\tensquare_APP\logstash-6.2.1\config下創建一個模板文件xc_course_template.json,設置ES的映射關系:

{
   "mappings" : {
      "doc" : {
         "properties" : {
            "charge" : {
               "type" : "keyword"
            },
            "description" : {
               "analyzer" : "ik_max_word",
               "search_analyzer" : "ik_smart",
               "type" : "text"
            },
            "end_time" : {
               "format" : "yyyy-MM-dd HH:mm:ss",
               "type" : "date"
            },
            "expires" : {
               "format" : "yyyy-MM-dd HH:mm:ss",
               "type" : "date"
            },
            "grade" : {
               "type" : "keyword"
            },
            "id" : {
               "type" : "keyword"
            },
            "mt" : {
               "type" : "keyword"
            },
            "name" : {
               "analyzer" : "ik_max_word",
               "search_analyzer" : "ik_smart",
               "type" : "text"
            },
            "pic" : {
               "index" : false,
               "type" : "keyword"
            },
            "price" : {
               "type" : "float"
            },
            "price_old" : {
               "type" : "float"
            },
            "pub_time" : {
               "format" : "yyyy-MM-dd HH:mm:ss",
               "type" : "date"
            },
            "qq" : {
               "index" : false,
               "type" : "keyword"
            },
            "st" : {
               "type" : "keyword"
            },
            "start_time" : {
               "format" : "yyyy-MM-dd HH:mm:ss",
               "type" : "date"
            },
            "status" : {
               "type" : "keyword"
            },
            "studymodel" : {
               "type" : "keyword"
            },
            "teachmode" : {
               "type" : "keyword"
            },
            "teachplan" : {
               "analyzer" : "ik_max_word",
               "search_analyzer" : "ik_smart",
               "type" : "text"
            },
            "users" : {
               "index" : false,
               "type" : "text"
            },
            "valid" : {
               "type" : "keyword"
            }
         }
      }
   },
   "template" : "xc_course"
}

 

在D:\tensquare_APP\logstash-6.2.1\config下創建一個數據庫配置文件mysql.conf:

input {
  stdin {
  }
  jdbc {
      # 連接哪個數據庫
      jdbc_connection_string => "jdbc:mysql://localhost:3307/xc_course?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"
      
      # the user we wish to excute our statement as
      # 賬號、密碼
      jdbc_user => "root"
      jdbc_password => "admins"
      
      # the path to our downloaded jdbc driver
      # 數據庫驅動所在位置
      jdbc_driver_library => "D:/tensquare_APP/logstash-6.2.1/config/mysql-connector-java-5.1.46.jar"     
      # the name of the driver class for mysql
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      
      # 是否分頁
      jdbc_paging_enabled => "true"
      # 顯示多少條數據
      jdbc_page_size => "50000"
      # 要執行的sql文件
      #statement_filepath => "/conf/course.sql"
      # 將數據庫timestamp大於logstash_metadata設置的時間的數據都同步到索引庫
      statement => "select * from course_pub where timestamp > date_add(:sql_last_value,INTERVAL 8 HOUR)"
      # 定時配置,默認1分鍾一次
      schedule => "* * * * *"
      record_last_run => true
      # timestamp設置位置
      last_run_metadata_path => "D:/tensquare_APP/logstash-6.2.1/config/logstash_metadata"
  }
}
​
output {
  elasticsearch {
      # ES的ip地址和端口
      hosts => "localhost:9200"
      # 集群設置
      #hosts => ["localhost:9200","localhost:9202","localhost:9203"]
      # ES索引庫名稱
      index => "xc_course"
      # 自增ID編號
      document_id => "%{id}"
      # 對應type(表名)
      document_type => "doc"
​
      # 模板位置
      template =>"D:/tensquare_APP/logstash-6.2.1/config/xc_course_template.json"
      template_name =>"xc_course"
      template_overwrite =>"true"
  }
  stdout {
      #日志輸出,以JSON格式輸出
      codec => json_lines
  }
}

 

9.3 啟動

cmd進入D:\tensquare_APP\logstash-6.2.1\bin,執行:

logstash.bat -f ../config/mysql.conf

 

如果啟動過程報錯:找不到或無法加載主類 Files\Java\jdk1.8.0_73\lib;C:\Program;

則編輯logstash.bat文件,將里面的%CLASSPATH%加上雙引號:

%JAVA% %JAVA_OPTS% -cp "%CLASSPATH%" org.logstash.Logstash %*

 

同步如下:

 

ES數據:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM