Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmlucjav/81b4f74c5e2c3d95666496253067c103 to your computer and use it in GitHub Desktop.
Save jmlucjav/81b4f74c5e2c3d95666496253067c103 to your computer and use it in GitHub Desktop.
Import array in JSON file to Elasticsearch
// 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