【发布时间】:2018-01-24 12:45:39
【问题描述】:
我需要读取一个非常大的位置历史记录文件并提取一些数据并将其作为 JSON 数据写入文件。我怎样才能做到这一点。 以下代码不会生成任何输出。 编辑: 我希望在文件中输出字符串,因为它通过管道传输到 fileOutputStream
const fs = require('fs')
var JSONStream = require('JSONStream');
var es = require('event-stream');
const filePath = './location-history.json'
const fileOutputPath = './transform-location-history.json'
fileStream = fs.createReadStream(filePath);
fileOutputStream = fs.createWriteStream(fileOutputPath)
const transformer = (data) => {
const location = {
latitude: data.latitudeE7 / 10000000,
longitude: data.longitudeE7 / 10000000
}
return JSON.stringify(location);
}
fileStream
.pipe(JSONStream.parse('locations.*'))
.pipe(es.through(transformer))
.pipe(fileOutputStream)
【问题讨论】:
-
为什么不写入输出流?
-
一切看起来都很好。你会提到你从哪里获取数据变量的值
-
gist.github.com/tuncatunc/d4670b73032d4473a7236ac95514c3d0 示例输入。原始文件有超过200K点。
标签: javascript json node.js jsonstream