背景
來了個需求,需要從某個線上es庫查詢一些數據出來並進行大屏展示。問需求方有沒有開發環境的es庫,答:沒有,說要不直連他們的線上庫。
後面想想也行吧,業務方都這麼說了,結果開網絡的流程被打回了,理由是網絡隔離。
於是,只能採用從線上es庫導出文件,然後在開發環境原樣搭建這麼一個es庫並導入的辦法。
瞭解到線上es庫,版本是5.4.3,準備在開發環境恢復的那個索引的數據量大概是有20來個g。
我們是使用elasticdump來進行數據導入導出的,數據量小的時候用這個還是可以,但20 來個g這種,導入的過程還是有一些坑的,當時一開始沒加一些參數,搞了一晚上都沒弄完,後面研究了下,速度才快了,所以簡單記錄下。
開發環境es搭建
簡單搭建
先找到了官方的5.4.3版本的文檔。
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/getting-started.html
首先是搭建,參考官方:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/zip-targz.html
我是用tar包這種方式:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.3.tar.gz
sha1sum elasticsearch-5.4.3.tar.gz
tar -xzf elasticsearch-5.4.3.tar.gz
cd elasticsearch-5.4.3/
./bin/elasticsearch
結果啓動報錯:
[root@VM-0-6-centos elasticsearch-5.4.3]# ./bin/elasticsearch
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
網上查了下,把內存改了下,我的雲主機內存小,大家看着改吧:
[root@VM-0-6-centos elasticsearch-5.4.3]# vim config/jvm.options
-Xms256m
-Xmx256m
再啓動,再報錯:
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
創建個用戶、用戶組吧:
// --先看看有沒有es的相關用戶存在
cat /etc/passwd
groupadd elasticsearch
useradd elasticsearch -g elasticsearch
chown -R elasticsearch:elasticsearch /opt/upload/elasticsearch-5.4.3
然後可以單開個shell
su elasticsearch
cd /opt/upload/elasticsearch-5.4.3
bin/elasticsearch
這樣就前臺啓動起來了。默認的日誌就在es安裝目錄下:
/opt/upload/elasticsearch-5.4.3/logs/elasticsearch.log
curl -X GET "localhost:9200/?pretty"
後臺運行:
後臺運行並記錄pid到pid file:
./bin/elasticsearch -d -p pid
停止:
kill `cat pid`
關於配置
網上很多安裝教程會涉及把這兩個配置相關的目錄,改成es用戶,如這種:
chown elasticsearch:elasticsearch -R /var/log/elasticsearch
但這個路徑還是要根據實際來,這個path.logs/path.data在config/elasticsearch.yml 中配置,我們這裏沒配置,所以就在安裝目錄下,所以不需要單獨去chown修改權限。
除了這個之外,還有很多配置項,開發環境可以無所謂,線上還是得每個參數好好斟酌。
這些參數配置的文檔:
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/important-settings.html#path-settings
還有很多重要的配置:
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/setting-system-settings.html
樣例數據導入
在看官網時,發現還有樣例數據輔助學習,試了下,還是不錯的。
原地址已經404了,在網上找了下:
https://blog.csdn.net/qq_20667511/article/details/109614359
https://github.com/elastic/elasticsearch/blob/7.5/docs/src/test/resources/accounts.json
https://github.com/elastic/elasticsearch/issues/88146
數據導入:
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/gs-exploring-data.html
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'
esdump導入數據
elasticsearch-dump安裝
https://github.com/elasticsearch-dump/elasticsearch-dump
這個是用js寫的,我這邊是先在本地虛擬機用npm安裝這個module(有網絡),然後把這個模塊拷貝到內網es服務器上去跑導入本地文件的;當然它也支持從一個es/文件導出,直接導入到另一個es/文件。
反正就是目標和源都可以是文件和es服務。
npm install elasticdump -g
or 安裝指定版本的module
npm i [email protected]
https://www.npmjs.com/package/elasticdump/v/6.104.1?activeTab=readme
找到elasticdump這個node,打tar包,拷貝到無網絡的服務器上
ll /root/upload/node-v12.3.0-linux-x64/lib/node_modules
tar -cvf elasticdump.tar elasticdump
目標服務器上解壓:
/root/upload/node-v16.20.2-linux-x64/lib/node_modules
此時,執行elasticdump不生效,找不到,所以要在path下建立軟連接:
cd /root/upload/node-v16.20.2-linux-x64/bin
ln -s ../lib/node_modules/elasticdump/bin/elasticdump elasticdump
ln -s ../lib/node_modules/elasticdump/bin/multielasticdump multielasticdump
導入(慢)
我是從文件導入新搭建的es服務。根據導出語句寫導入語句即可:
注意,數據量大的時候,下面語句比較慢,看完全文再操作。
elasticdump --input=/root/upload/root/esbackup/20231204/base20231204/common_mapping.json --output=http://localhost:9200/base20231204 --type=mapping
elasticdump --input=/root/upload/root/esbackup/20231204/base20231204/common_data.json --output=http://localhost:9200/base20231204 --type=data
elasticdump --input=/root/upload/root/esbackup/20231204/base20231204/data_mapping.json --output=http://localhost:9200/data --type=mapping
後臺導入:
nohup elasticdump --input=/root/upload/root/esbackup/20231204/base20231204/data_data.json --output=http://localhost:9200/data --type=data 2>&1 &
導入(快)
後面的語句:
nohup elasticdump --input=/root/upload/root/esbackup/20231204/history20231204/common_data.json --output=http://localhost:9200/data20231204 --type=data --noRefresh --limit 10000 --support-big-int --fileSize 1gb 2>&1 &
主要是增加了--noRefresh,這個纔是主要的。
參數的解釋:
開了這個選項後,導入快多了,之前是一晚上都搞不完。
kibana
順便記錄下kibana的安裝。
https://www.elastic.co/guide/en/kibana/5.4/targz.html
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.3-linux-x86_64.tar.gz
sha1sum kibana-5.4.3-linux-x86_64.tar.gz
tar -xzf kibana-5.4.3-linux-x86_64.tar.gz
cd kibana/
啓動前改下配置:
cd config/
vim kibana.yml
elasticsearch.url: "http://localhost:9200"
server.host: 0.0.0.0
其他
本來一開始規劃是後端對接es,給前端提供接口;後來計劃是前端直接對接es(前端爲了避免跨域,還是通過後端nginx轉發es請求到es服務器)。
當時本來還研究了下java client的版本兼容,後面就沒弄了。
https://www.elastic.co/guide/en/elasticsearch/client/index.html
客戶端這塊,Java Client只支持7.0後版本的服務端;
Java Rest Client這塊,5.6版本的高級客戶端,不支持es服務端5.4.3版本,所以,如果要用的話,都只能使用5.4或5.5或5.6的低級客戶端。
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/java-rest-high-compatibility.html
參考鏈接
https://www.elastic.co/guide/en/elastic-stack/5.4/index.html
https://www.elastic.co/guide/en/elastic-stack/5.4/elastic-stack.html
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/getting-started.html
https://www.elastic.co/guide/en/kibana/5.4/introduction.html