為了輸入我的 JSON 檔案(陣列形式),我嘗試了很多東西,到目前為止我沒有任何結果
input{
file{
path=> "/usr/share/logs/Docs.json"
#codec=> "json" tried
#codec=> json {charset => "ISO-8859-1"} tried
#codec => json{ } tried
start_position=> "beginning"
sincedb_path=> "/dev/null"
}
}
filter{
json{
source=> "message"
}
}
output{
stdout{
codec=> "json"
}
elasticsearch{
hosts=> ["http://es1:9200"]
index=> "index_abc"
}
}
JSON 檔案格式(都在同一行):
[{"id":1,"something":"text1"},{"id":2,"something":"text2"},{"id":3,"something":"text3"}]
如果你可以的話,我會非常感激。
uj5u.com熱心網友回復:
問題解決了,這是因為編碼,我使用jq實用程式將我的 JSON 檔案轉換為正確的格式(用于 Logstash),即:
{"id":1,"something":"text1"}
{"id":2,"something":"text2"}
{"id":3,"something":"text3"}
我一開始并沒有注意到,但是 jq 在轉換程序中改變了我檔案的編碼,它最終變成了 UTF-16 LE。因此,我使用 VS Code 將編碼更改(并保存)為 UTF-8,之后它運行良好。
我現在可以使用的更新代碼:
input{
file{
path=> "/usr/share/logs/Docs_formatted.json"
codec=> "json"
start_position=> "beginning"
sincedb_path=> "/dev/null"
}
}
filter{}
output{
stdout{}
elasticsearch{
hosts=> ["http://es1:9200"]
index=> "index_abc"
}
}
對于那些感興趣的人,我使用這個命令列來將我的 JSON 檔案轉換為正確的格式(Windows 命令列):
type Docs.json | jq -c '.[]' > Docs_formatted.json
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464134.html
