Last active
October 25, 2021 16:24
-
-
Save stiekel/b183b5473ac7e9031c5428a0be138e69 to your computer and use it in GitHub Desktop.
Import array in JSON file to Elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import json array file to elasticsearch | |
const fs = require('fs') | |
const shelljs = require('shelljs') | |
let endpoint = process.argv[2] | |
let jsonFile = process.argv[3] | |
if (!endpoint || !jsonFile || endpoint.search('_bulk') === -1) { | |
console.log('sample: node jsonArrayToES.js localhost:9200/bank/customer/_bulk customer.json') | |
process.exit() | |
} | |
console.log('json', jsonFile) | |
let data | |
try { | |
data = JSON.parse(fs.readFileSync(jsonFile)) | |
} catch (e) { | |
console.log('read json file failed') | |
process.exit() | |
} | |
console.log('read', data.length, 'line(s)') | |
let dataBinary = [] | |
data.forEach((d, index) => { | |
let idx = { index: { _id: index + 1 } } | |
dataBinary.push(JSON.stringify(idx)) | |
dataBinary.push(JSON.stringify(d)) | |
}) | |
dataBinary = dataBinary.join('\n') | |
fs.writeFileSync('result.json', dataBinary) | |
let cmd = ['curl -XPOST ', endpoint, ' --data-binary \'@result.json\''].join('') | |
console.log(cmd) | |
let rlt = shelljs.exec(cmd) | |
console.log(rlt) | |
shelljs.exec('rm result.json') | |
if (rlt.code === 0) { | |
console.log('data imported') | |
} else { | |
console.log('error occured') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you convert this js script into python code