elasticsearch批量插入数据

官方文档地址:https://www.elastic.co/guide/en/kibana/6.1/tutorial-load-dataset.html


本教程需要三个数据集:

    1.威廉莎士比亚的全部作品,适当地解析成字段。压缩文件需要解压。下载地址:

https://download.elastic.co/demos/kibana/gettingstarted/shakespeare_6.0.json

    2.一组随机生成数据的虚构账户,下载地址

https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip
    3, 一组随机生成的日志文件
https://download.elastic.co/demos/kibana/gettingstarted/logs.jsonl.gz

莎士比亚的作品集有这样的结构
{
    "line_id": INT,
    "play_name": "String",
    "speech_number": INT,
    "line_number": "String",
    "speaker": "String",
    "text_entry": "String",
}
虚拟账户结构是
{
    "account_number": INT,
    "balance": INT,
    "firstname": "String",
    "lastname": "String",
    "age": INT,
    "gender": "M or F",
    "address": "String",
    "employer": "String",
    "email": "String",
    "city": "String",
    "state": "String"
}

日志数据集有几十个不同的字段。以下是本教程的重要字段:

{
    "memory": INT,
    "geo.coordinates": "geo_point"
    "@timestamp": "date"
}

在加载莎士比亚和日志数据集之前,您必须设置字段的映射。映射将索引中的文档分成逻辑组并指定字段的特征。这些特征包括字段的可搜索性以及它是否被标记,或者分解成单独的单词。

在Kibana Dev Tools> Console中,设置莎士比亚数据集的映射:

PUT /shakespeare
{
 "mappings": {
  "doc": {
   "properties": {
    "speaker": {"type": "keyword"},
    "play_name": {"type": "keyword"},
    "line_id": {"type": "integer"},
    "speech_number": {"type": "integer"}
   }
  }
 }
}
该映射为数据集指定字段特征:speaker play_name字段是关键字字段。这些字段未被分析。即使它们包含多个单词,字符串也会被视为一个单元。 line_id speech_number 字段是整数。

日志数据集需要映射才能通过应用geo_point 类型将纬度和经度对标记为地理位置。

PUT /logstash-2015.05.18
{
  "mappings": {
    "log": {
      "properties": {
        "geo": {
          "properties": {
            "coordinates": {
              "type": "geo_point"
            }
          }
        }
      }
    }
  }
}
PUT /logstash-2015.05.19
{
  "mappings": {
    "log": {
      "properties": {
        "geo": {
          "properties": {
            "coordinates": {
              "type": "geo_point"
            }
          }
        }
      }
    }
  }
}

PUT /logstash-2015.05.20
{
  "mappings": {
    "log": {
      "properties": {
        "geo": {
          "properties": {
            "coordinates": {
              "type": "geo_point"
            }
          }
        }
      }
    }
  }
}
账户数据集不需要任何映射。

此时,就已准备好使用Elasticsearch批量API加载数据集,这里要注意的是这些文件的位置应该是你所在的当前目录,
如果你当前位置是D盘~那么这些文件位置就要放在D盘下,否则读不到

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/doc/_bulk?pretty' --data-binary @shakespeare_6.0.json
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/_bulk?pretty' --data-binary @logs.jsonl

或者对于Windows用户,在Powershell中:

Invoke-RestMethod "http://localhost:9200/bank/account/_bulk?pretty" -Method Post -ContentType 'application/x-ndjson' -InFile "accounts.json"
Invoke-RestMethod "http://localhost:9200/shakespeare/doc/_bulk?pretty" -Method Post -ContentType 'application/x-ndjson' -InFile "shakespeare_6.0.json"
Invoke-RestMethod "http://localhost:9200/_bulk?pretty" -Method Post -ContentType 'application/x-ndjson' -InFile "logs.jsonl"

这些命令可能需要一些时间才能执行,具体取决于计算机的性能

验证是否成功加载:

GET /_cat/indices?v

你的输出应该看起来类似于这个:

health status index               pri rep docs.count docs.deleted store.size pri.store.size
yellow open   bank                  5   1       1000            0    418.2kb        418.2kb
yellow open   shakespeare           5   1     111396            0     17.6mb         17.6mb
yellow open   logstash-2015.05.18   5   1       4631            0     15.6mb         15.6mb
yellow open   logstash-2015.05.19   5   1       4624            0     15.7mb         15.7mb
yellow open   logstash-2015.05.20   5   1       4750            0     16.4mb   










  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值