【问题标题】:Importing every line of a file into Elasticsearch with -POST使用 -POST 将文件的每一行导入 Elasticsearch
【发布时间】:2019-11-19 07:49:45
【问题描述】:

当我运行这个命令时,它会将$line 直接发布到 Elasticsearch 中,而不是 file.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;

file.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 elasticsearch curl while-loop cat


【解决方案1】:

我发现它类似于这个问题:How can I use a variable in curl call within bash script 并回答。谢谢@that other guy 和@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;

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2013-04-02
    相关资源
    最近更新 更多