Skip to content

Instantly share code, notes, and snippets.

@mutoo
Last active January 30, 2020 11:50
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 mutoo/28667cfe7e9806ae4cfca9f348997f03 to your computer and use it in GitHub Desktop.
Save mutoo/28667cfe7e9806ae4cfca9f348997f03 to your computer and use it in GitHub Desktop.
a demo of using stream json for big data.
{
"a": {
"b": [
{
"id": 301940,
"slug": "ef4f2422125f",
"nickname": "说",
"avatar_source": "asdfsd",
"total_likes_count": 28553,
"total_wordage": 1373341,
"is_following_user": false,
"ob": {
"a": 1,
"b": 2
}
},
{
"id": 3950651,
"slug": "ca5b9d6f94dc",
"nickname": "三屿",
"avatar_source": "asdf",
"total_likes_count": 3625,
"total_wordage": 169200,
"is_following_user": false
}
]
},
"total_count": 35932
}
const { chain } = require("stream-chain");
const { streamValues } = require("stream-json/streamers/StreamValues");
const Pick = require("stream-json/filters/Pick");
const fs = require("fs");
const pipeline = chain([
fs.createReadStream("./small.json"),
Pick.withParser({ filter: /^a\.b\.\d+/ }),
streamValues()
]);
pipeline.on("data", data => {
console.log(data);
});
pipeline.on("end", () => {
console.log(`the end`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment