Skip to content

Instantly share code, notes, and snippets.

@williamho
Last active November 17, 2015 18:27
Show Gist options
  • Select an option

  • Save williamho/4cbd27f31c8d99129389 to your computer and use it in GitHub Desktop.

Select an option

Save williamho/4cbd27f31c8d99129389 to your computer and use it in GitHub Desktop.
db-ip to mongoimport
// From a db-ip csv file, generate a mongoimport-compatible file, converting IPs to Longs.
// Ignores IPv6 because byte arrays are annoying to work with. Sorry.
// Usage: node index.js <input.csv >output.txt
var ipModule = require('ip')
, parse = require('csv-parse')
, transform = require('stream-transform')
;
var parser = parse({ columns: ['startIp', 'endIp', 'countryCode'] });
var transformer = transform(function(record, callback){
if (record.startIp.indexOf(':') > -1) { return; } // Ignore IPv6
// If we want to a byte array instead, call this instead of ipModule.toLong
function toBinaryBSON(ip) {
return {
$binary: ipModule.toBuffer(ip).toString('base64'),
$type: '05'
}
}
var newRecord = {
startIp: ipModule.toLong(record.startIp),
endIp: ipModule.toLong(record.endIp),
countryCode: record.countryCode
};
callback(null, JSON.stringify(newRecord) + '\n');
}, {parallel: 10});
process.stdin
.pipe(parser)
.pipe(transformer)
.pipe(process.stdout);
{
"name": "db-ip to mongoimport",
"version": "1.0.0",
"description": "convert db-ip csv to mongoimport compatible input",
"main": "index.js",
"author": "",
"license": "MIT",
"dependencies": {
"csv-parse": "^1.0.0",
"ip": "^1.0.2",
"stream-transform": "^0.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment