Fork me on GitHub

Elasticsearch测试数据快速导入

目录

  • 背景
  • 第一部分 环境
  • 第二部分 导入数据案例
  • 参考文献及资料

背景

测试elasticsearch需要一些案例数据导入,然后进行相关工程测试和验证。elasticsearch官方提供了一个公开数据集:莎士比亚作品对白文本数据。

使用这个数据进行测试。

第一部分 环境

本次实战的环境信息如下:

  • 操作系统:Ubuntu 16.04 LTS

  • JDK:1.8.0

  • elasticsearch:7.6.0

第二部分 导入数据案例

测试环境地址:192.168.52.142elasticsearch服务的IP地址,9200elasticsearch服务的端口号。导入的shell脚本如下:

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
28
29
30
31
32
#!/bin/bash

ES_HOST=$1
ES_PORT=$2

echo "elasticsearch server is $ES_HOST:$ES_PORT"
echo "downloading data from github"

wget --no-check-certificate https://download.elastic.co/demos/kibana/gettingstarted/7.x/shakespeare.json
echo "downloading data success"
echo "creating index ..."
curl -X PUT \
http://${ES_HOST}:${ES_PORT}/shakespeare \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"mappings": {
"properties": {
"speaker": {"type": "keyword"},
"play_name": {"type": "keyword"},
"line_id": {"type": "integer"},
"speech_number": {"type": "integer"}
}
}'

echo ""
echo "loading data ..."

curl -u elastic -H 'Content-Type: application/x-ndjson' -XPOST '$ES_HOST:$ES_PORT/shakespeare/_bulk?pretty' --data-binary @shakespeare.json > result.log

echo "finish"

保存为执行脚本:create_shakespeare_index

1
2
chmod a+x create_shakespeare_index.sh \
&& ./create_shakespeare_index.sh 192.168.52.142 9200

执行完毕后,会创建名为shakespeare的索引,并带有111,396个文档,案例数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"_index": "shakespeare",
"_type": "_doc",
"_id": "111395",
"_version": 1,
"_score": 0,
"_source": {
"type": "line",
"line_id": 111396,
"play_name": "A Winters Tale",
"speech_number": 38,
"line_number": "",
"speaker": "LEONTES",
"text_entry": "Exeunt"
}
}

注意:由于特殊网络限制,大陆下载较慢可以先下载文件,然后再导入:

https://download.elastic.co/demos/kibana/gettingstarted/7.x/shakespeare.json

参考文献及资料

1、官网,链接:https://www.elastic.co/cn/

本文标题:Elasticsearch测试数据快速导入

文章作者:rong xiang

发布时间:2022年05月19日 - 13:05

最后更新:2022年10月25日 - 23:10

原始链接:https://zjrongxiang.github.io/posts/ed4f4003/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%