【问题标题】:Post PowerShell JSON into ElasticSearch将 PowerShell JSON 发布到 ElasticSearch
【发布时间】:2017-09-12 02:02:41
【问题描述】:

我正在尝试通过 PowerShell 将一些数据推送到 ElasticSearch。我将数据转换为 JSON,然后尝试 Invoke-WebRequestInvoke-RestMethod,但总是在格式错误的数据或不支持的内容类型上出现错误。我还没有创建索引,因为我相信它会为我创建它。

谁能帮助我解决我的遗漏/做错了什么?

示例代码:

$data = @()
$CustomObject = [pscustomobject]@{
        SqlInstance = "myserver1"
        Database = "mydb"
        Schema = "versioning"
        Name = "DataVersionHistory"
        IndexSpaceUsed = 0
        DataSpaceUsed = 0
        RowCount = 0
        };
$data += $CustomObject;
$CustomObject = [pscustomobject]@{
        SqlInstance = "myserver1"
        Database = "mydb"
        Schema = "versioning"
        Name = "VersionHistory"
        IndexSpaceUsed = 10
        DataSpaceUsed = 25
        RowCount = 3000
        };
$data += $CustomObject;
$myJson = ConvertTo-Json -InputObject $data ;
Invoke-RestMethod -Uri http://localhost:9200/myindex/mytype/_bulk?pretty `
-Method POST -Body $myJson -ContentType "application/json"

【问题讨论】:

  • json 在 powershell 中当然是无效的...用`key:value`发布完整的json
  • 嗨 Kiran,更新了示例脚本以使 json 对象更加准确和清晰
  • 看起来不错...如果帖子不起作用,请尝试 put 方法
  • 嘿,Kiran,尝试使用 contenttype 不使用、使用批量和不使用的方式发布/放置,但仍然没有任何乐趣。我得到与stackoverflow.com/questions/35213472 相同的错误,使用: Invoke-WebRequest -Method Post -Uri localhost:9200/myindex/mytype -Body $myJson -ContentType "application/json" 真的不可能只抓取一些 JSON 并将其放入 elasticsearch 吗?
  • 前段时间我在弹性搜索批量上传方面做了一些工作,但一直没有完成它,所以让我检查一下是否可以为你找到。但是,批量上传确实有效。

标签: json powershell elasticsearch


【解决方案1】:

批量请求不是实际的 json。您应该使用以下符号:

curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
'

【讨论】:

  • 嗨 Vitaliy,那么您将如何推送 json 格式的数据(或其他形式的数据)?你必须把它转换成你的语法吗?
  • 这不是我的语法,它是 ElasticSearch 的语法,是的,您必须将您的请求转换为这种格式。如果您想使用批量 API。
  • 感谢 Vitaly,我删除了批量请求。但仍然有问题。有什么想法吗?
  • 那么您应该用您的更改和您仍然存在的问题的描述来更新问题。
【解决方案2】:
    $data = @()
    $CustomObject = [pscustomobject]@{
        SqlInstance    = "myserver1"
        Database       = "mydb"
        Schema         = "versioning"
        Name           = "DataVersionHistory"
        IndexSpaceUsed = 0
        DataSpaceUsed  = 0
        RowCount       = 0
    };
    $data += $CustomObject;
    $CustomObject = [pscustomobject]@{
        SqlInstance    = "myserver1"
        Database       = "mydb"
        Schema         = "versioning"
        Name           = "VersionHistory"
        IndexSpaceUsed = 10
        DataSpaceUsed  = 25
        RowCount       = 3000
    };
    $data += $CustomObject

此时,您拥有一个包含在数组$data 中的对象集合。

注意:对于批量 API,json 数据的最后一行必须以换行符 \n 结尾。所以我使用here-string将换行符\n附加到每个json元素。

还注意到我删除了漂亮的打印。这是为了很好地使用批量 api。

doc_as_upsert 确保如果文档存在则不管它,如果不添加为新的。

$json_col = @()
$data | 
    ForEach-Object {
        #convert object to json
        $json_element = @{doc = $_; doc_as_upsert = $true}    
          | ConvertTo-Json -Depth 1 -Compress

        #construt here string with literal \n
        $json_col += @"

$json_element\n

"@
    }
Invoke-RestMethod -Method Post -Uri "http://localhost:9200/myindex/mytype/_bulk?" -Body $json_col  -ErrorAction Stop 

【讨论】:

  • 嗨 Kiran,感谢您提供的信息(我得出的结论是,它需要分解才能工作,只是没有机会构建它,所以非常合适)可悲的是,当我运行这个时,我仍然得到一个错误: {"error":{"root_cause":[{"type":"illegal_argument_exception","re​​ason":"Action/metadata line [2] contains an unknown parameter [id]"}],"type ":"illegal_argument_exception","re​​ason":"动作/元数据行 [2] 包含未知参数 [id]"},"status":400}
  • 以上应该可以。我认为您只需要创建一个具有类型的索引并在 invoke-restmethod 中指定该类型...但无论如何祝你好运
  • 其他人一起玩。我通过以下方式创建了一个带有映射的索引: PUT dba2 { "mappings": { "tables": { "_all": { "enabled": true }, "properties": { "Sqlinstance": { "type": "text" },“数据库”:{“类型”:“文本”},“架构”:{“类型”:“文本”},“名称”:{“类型”:“文本”},“IndexSpaceUsed”:{“ type": "integer" } , "DataSpaceUsed": { "type": "integer" } , "RowCount": { "type": "integer" } } } } }
  • 更新调用命令以使用它我现在拥有:Invoke-RestMethod -Method POST -Uri "localhost:9200/DBA2/tables/_bulk?" -Body $json_col -ErrorAction Stop and error: {"error":{"root_cause":[{"type":"illegal_argument_exception","re​​ason":"Action/metadata line [2] contains an unknown parameter [SqlInstance]" }],"type":"illegal_argument_exception","re​​ason":"操作/元数据行 [2] 包含未知参数 [SqlInstance]"},"status":400}
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 2014-02-08
  • 2021-05-20
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多