ElasticSearch和ElasticSearch Head環境搭建和數據模擬


首先elasticsearch-6.0.0\bin目錄下運行elasticsearch服務

修改elasticsearch-6.0.0\elasticsearch.yml文件

在文件最后加入下面代碼,設置跨域

http.cors.enabled: true 
http.cors.allow-origin: "*"

如果安裝X-Pack,需要添加 配置

http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE

http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length

 

然后用淘寶NPM鏡像CNPM Start安裝和啟動elasticsearch head

過程很簡單,解壓后執行Npm命令就可以正常啟動了

安裝成功后訪問 http://localhost:9100/ 即可自動連接elasticsearch

 

其他插件也可以用elasticsearch-plugin安裝的方式

1.離線安裝

  • Unix
  • sudo bin/elasticsearch-plugin install file:///path/to/plugin.zip
  • Windows
  • bin\elasticsearch-plugin install file:///C:/path/to/plugin.zip

2.在線安裝

  • elasticsearch-plugin install x-pack
  • elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip

一、使用python快速像ES服務添加測試索引數據,分普通插入和批量插入兩種,數據分析時使用python來處理的場景還是很多的

運行前先要搭建好基本的Python環境和pip,然后使用pip install 命令安裝所需import的包

1、普通插入:

#coding:utf-8
from datetime import datetime
from elasticsearch import Elasticsearch

es = Elasticsearch( "localhost:9200" ) 
data = {
    "@timestamp" :datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000+0800"),
    "http_code" :"404",
    "count" :"10"
}

es.index(index="http_code", doc_type="error_code", body=data)

 2、批量插入:

#coding:utf-8
from datetime import datetime
from elasticsearch import Elasticsearch
import elasticsearch.helpers
import random

es = Elasticsearch( "localhost:9200" ) 
package = []
for i in range( 10 ):
    row = {
		"@timestamp" :datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000+0800"),
		"http_code" :"404",
        "count" : random.randint(1, 100)
    }
    package.append( row )

actions = [
    {
        '_op_type': 'index',
        '_index': "http_code",  
        '_type': "error_code",  
        '_source': d
    }
    for d in package
]    

elasticsearch.helpers.bulk(es, actions)

插入后效果:

 

通過ElasticSearch Head做最基本的查詢:

二、使用Postman來模擬請求插入數據

新建一個Post請求,http://127.0.0.1:9200/tdb/ttable 

tdb相當於新增的數據庫,ttable相當於具體要新增數據的表

 

 

批量插入的話可以使用JQuery編寫Post請求或者使用前端工具設置批量新增

預告:

使用Logstash來實時同步MySQL數據到ES

使用docker快速搭建ELK環境

使用NetCore向ES快速寫數據的設計

NetCore結合ES億級數據的實踐


免責聲明!

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



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