[英]Post PowerShell JSON into ElasticSearch
我正在尝试通过PowerShell将一些数据推送到ElasticSearch中。 我将数据转换为JSON,然后尝试使用Invoke-WebRequest
和Invoke-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。 您应该使用以下符号:
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"} }
'
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.