【问题标题】:Problems bulk insert elasticsearch curl powershell问题批量插入 elasticsearch curl powershell
【发布时间】:2017-11-03 15:48:06
【问题描述】:

我是 Elasticsearch 新手,我正在按照教程加载示例数据:https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html 当我尝试在 powershell 中批量插入数据时 'curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary "@shakespeare.json"'

我收到以下错误

PS C:\Development\elasticsearch-5.4.1\importdata> curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary " @shakespeare.json" Invoke-WebRequest:无法绑定参数“标头”。无法转换“System.St”类型的“Content-Type:application/x-ndjson”值 ring”来输入“System.Collections.IDictionary”。 在行:1 字符:9 + curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/s ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我尝试了几件事,阅读了批量 api 文档但没有结果

以下是json文件内容的一些示例:

{"index":{"_index":"shakespeare","_type":"act","_id":0}} {"line_id":1,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"} {"index":{"_index":"shakespeare","_type":"scene","_id":1}} {"line_id":2,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"场景 I. 伦敦。宫殿。 "} {"index":{"_index":"shakespeare","_type":"line","_id":2}} {"line_id":3,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"亨利王上,兰开斯特勋爵约翰、威斯特摩兰伯爵、沃尔特·布朗特爵士等人”}

【问题讨论】:

  • 使用批量注入时json数据的结构不同,你基本上需要将index/type/id作为json的一部分添加:这里有更多信息elastic.co/guide/en/elasticsearch/reference/5.4/docs-bulk.html
  • 你也在 powershell/Invoke-WebRequest 中使用 unix/curl,你应该使用 Invoke-RestMethod 代替 imo。
  • 最后 URI 应该是 localhost:9200/_bulk..这些 cmets 无法解决所有问题,因为我没有看到 sharepeare.json 文件,但它应该让你更接近。
  • 我添加了 Shakespeare.json 文件的一些示例行。一切似乎都是正确的。我尝试了 Invoike-RestMethod 但没有结果
  • 有谁知道为什么我得到这个异常'Invoke-WebRequest:无法绑定参数'Headers'。无法将类型“System.String”的“Content-Type: application/x-ndjson”值转换为类型“System.Collections.IDictionary”'

标签: powershell curl elasticsearch


【解决方案1】:

我找到了解决方案。因为我在windows和powershell上使用curl,所以不得不翻译

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json

PS C:\Development\elasticsearch-5.4.1\importdata> $postParams = @"
>> {"index":{"_index":"shakespeare","_type":"act","_id":0}}
>> {"line_id":1,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"}
>>
>> "@

PS C:\Development\elasticsearch-5.4.1\importdata> curl -H @{"Content-Type" = "application/x-ndjson"} -Method POST 'http://localhost:92
00/shakespeare/_bulk?pretty' -body $postParams

当我使用演示文件时,批量插入没有成功,但这是另一个问题

PS C:\Development\elasticsearch-5.4.1\importdata> $postParams = Get-Content "shakespeare.json"

【讨论】:

    猜你喜欢
    • 2011-11-15
    • 2011-01-19
    • 1970-01-01
    • 2015-08-18
    • 2021-05-12
    • 2016-08-11
    • 2022-08-10
    • 1970-01-01
    相关资源
    最近更新 更多