简体   繁体   中英

Insert header for each document before uploading to elastic search

I have a ndjson file with the below format

{"field1": "data1" , "field2": "data2"}
{"field1": "data1" , "field2": "data2"}
....

I want to add a header like

{"index": {}}

before each document before using the bulk operation I found a similar question: Elasticsearch Bulk JSON Data

The solution is this jq command:

jq -cr ".[]" input.json | while read line; do echo '{"index":{}}'; echo $line; done > bulk.json

But I get this error:

'while' is not recognized as a internal or external command

What am I doing wrong? Im running Windows Or is there a better solution? Thanks

The while in your sample is a construct that is usually built-in a developer-friendly shell like eg sh, bash or zsh but windows doesn't provide out of the box. See the bash docs for example .

So if this is a one-time thing, probably the fastest solution is to just use some text editor and add the required action lines by using some multi-cursor functionality.

On the other hand, if you are restricted to Windows but want some kind of better shell to use this more often, you should have a look at the cmder project that brings you a bash environment to your windows desktop when using the full version that is packaged with git-for-windows. This should allow you to use such scripting features even on a non linux or mac environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM