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、普通插入:

1
2
3
4
5
6
7
8
9
10
11
12
#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、批量插入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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亿级数据的实践

posted @   leeolevis  阅读(1782)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示