【发布时间】:2017-09-12 02:02:41
【问题描述】:
我正在尝试通过 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 在 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