-
-
Save ConAlgorithm/7546a1765e91dd5b197c8b22fd081726 to your computer and use it in GitHub Desktop.
Import array in JSON file to Elasticsearch
This file contains 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