使用 -POST 将文件的每一行导入到 Elasticsearch 中



当我运行这个命令时,它实际上是将$line发布到Elasticsearch而不是文件行中.txt

cat file.txt | while IFS= read -r line; do curl -d '{ "field" : "$line" }' -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;

文件.txt包括:

This is line one.
This is line two.
This is line three.

这是 Centos 7 上的 Elasticsearch 7.2。

我试图像$line($line)"$line"那样逃避$line
但它发布了$line的实际价值,($line)"$line"

预期要放入 Elasticsearch 的数据:

{ "field" : "This is line one." }
{ "field" : "This is line two." }
{ "field" : "This is line three." }

实际数据放入 Elasticsearch
{ "field" : "$line" }

我发现它类似于这个问题:如何在 bash 脚本和答案中使用 curl 调用中的变量。 谢谢你@that另一个人和@chepner

cat file.txt | while IFS= read -r line; do jq -n --arg var "$line"  '.field = $var'| curl --data @- -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;

最新更新