ElasticSearch和ElasticSearch Head环境搭建和数据模拟

首先elasticsearch-6.0.0in目录下运行elasticsearch服务

修改elasticsearch-6.0.0elasticsearch.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
  • binelasticsearch-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亿级数据的实践

原文地址:https://www.cnblogs.com/leeolevis/p/ElasticSearch01.html