CNode

nodejs 如何加载大数据json文件比如2g ,3g,10g

问答
Aansendu发布于11 年前最后回复9 年前24 回复13360 浏览0 收藏

Fatal error in ../deps/v8/src/handles.h, line 48

CHECK(location_ != NULL) failed

==== C stack trace ===============================

1: V8_Fatal 2: v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int) 3: node::StringBytes::Encode(v8::Isolate*, char const*, unsigned long, node::encoding) 4: void node::Buffer::StringSlice<(node::encoding)1>(v8::FunctionCallbackInfov8::Value const&) 5: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfov8::Value const&)) 6: ?? 7: ??

使用 edu_school_dict = fs.readFileSync(file_path, "utf8");或者edu_school_dict = require(file_path);加载直接报上面的错

我这边需求需要分析大数据文件 非日志文件 想用nodejs处理 问大家是否有好的方法

查看回复

回复 (24)

L
leizongmin#1·11 年前
C
captainblue2013#2·11 年前

你说到10g是一个json??我反而好奇怎么写进去的,找写进去那个人,解铃还需系铃人

A
ansendu#3·11 年前

我只是举个例子,我的意思是想用nodejs来分析大数据文件 比如如何加载一个大的数据文件 格式可以是json或者可以是一行一行的数据有比较好的模块吗 ?node-lei-stream我关注下,谢谢!

A
ansendu#4·11 年前

653M Jul 8 10:46 school_schools_majors.data 我是用php生成的 临时json文件 比较大 呵呵

C
captainblue2013#6·11 年前

本身生产者的角度就做错了啊,你要实现pipe处理,数据就要一条一条小的,或者是一个个小文件,有利于多开消费者

A
ansendu#8·11 年前
引用 leizongmin你需要这个模块 https://github.com/leizongmin/node lei stream

@leizongmin

[icsongzhi@appBI42 appnode]$ node server.js module.js:338 throw err; ^ Error: Cannot find module 'debug' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at Object. (/opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/utils.js:7:19) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) 好像util里面的debug模块没有 可以给个可运行的吗

L
leizongmin#10·11 年前
引用 ansendu@leizongmin npm安装ok!

@ansendu 一般自己不是从NPM安装的模块,需要执行npm install安装其依赖模块

A
ansendu#11·11 年前
引用 leizongmin你需要这个模块 https://github.com/leizongmin/node lei stream

@leizongmin 用你的模块报错了-_-

opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/readline.js:0

RangeError: Invalid string length at ReadLineStream._onData (/opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/readline.js:62:17) at ReadStream. (/opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/readline.js:48:10) at ReadStream.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at ReadStream.Readable.push (_stream_readable.js:126:10) at onread (fs.js:1679:12) at FSReqWrap.wrapper [as oncomplete] (fs.js:527:17)

我就一个600多m的一行大json数据
A
ansendu#12·11 年前
引用 leizongmin@ansendu 一般自己不是从NPM安装的模块,需要执行npm install安装其依赖模块

@leizongmin

可以使用你的模块 只是 我的json文件就一行数据但内容比较多 600多m 报的错我贴出来了!

A
ansendu#13·11 年前
引用 leizongmin@ansendu 一般自己不是从NPM安装的模块,需要执行npm install安装其依赖模块

@leizongmin 好像node在处理大文件的一些操作很费时间然后没有简单的方式处理是吧?

A
ansendu#14·11 年前
引用 leizongmin你需要这个模块 https://github.com/leizongmin/node lei stream

@leizongmin

果然 在使用这种方式 var file_path = writeDir + generate_file_map['edu_school_dict']; // var file_path = writeDir + "test.json";

// readLineStream第一个参数为ReadStream实例,也可以为文件名 var s = readLine(fs.createReadStream(file_path), { // 换行符,默认\n newline: '\n', // 是否自动读取下一行,默认false autoNext: false, // 编码器,可以为函数或字符串(内置编码器:json,base64),默认null encoding: function (data) { return JSON.parse(data); } });

// 读取到一行数据时触发data事件 s.on('data', function (data) { console.log(data["major"][13884256]); s.next(); });

// 流结束时触发end事件 s.on('end', function () { // console.log('end'); cb(null, null); });

也是报错

RangeError: Invalid string length
at ReadLineStream._onData (/opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/readline.js:62:17)
at ReadStream.<anonymous> (/opt/userhome/icsongzhi/server/ifchange_mobile_server/appnode/node_modules/lei-stream/lib/readline.js:48:10)
at ReadStream.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at ReadStream.Readable.push (_stream_readable.js:126:10)
at onread (fs.js:1679:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:527:17)
A
ansendu#16·11 年前
引用 leizongmin@ansendu 我错了……

@leizongmin 这个可以修复下吗 大神? 呵呵

L
leizongmin#17·11 年前
H
hezedu#20·11 年前

解析好了怎么用? 10G JSON 生成 对象 得多少G?

L
leizongmin#21·11 年前
引用 hezedu解析好了怎么用? 10G JSON 生成 对象 得多少G?

@hezedu 这种存储方法从一开始就错了……

A
ansendu#22·11 年前
引用 hezedu解析好了怎么用? 10G JSON 生成 对象 得多少G?

@hezedu 是临时的 放在内存里面,然后 别的数据 进行key value map查找

A
alsotang#23·11 年前

可以参考一下雷总 @leizongmin 的这篇文章:https://cnodejs.org/topic/55a73038f73c01466cf931f2

参与回复
登录后即可参与回复。登录