作者dppman (*^o^*)
看板R_Language
标题[问题] 透过R使用Rhdfs package读取超大CSV档
时间Mon May 19 12:06:23 2014
[问题类型]:
程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
使用R透过RHadoop的Rhdfs 1.0.8
[程式范例]:
我目前的实验环境,需要读取很大的CSV File(存放在Hadoop的HDFS上,档案大小几乎
都大於20GB),
我使用了RHdoop的rhdfs R Package
Ref.
https://github.com/RevolutionAnalytics/RHadoop/wiki
使用Rstudio Web版开发,原始码如下
*************************************************************************************************
Sys.setenv(HADOOP_CMD="/usr/lib/hadoop/bin/hadoop")
Sys.setenv(HADOOP_STREAMING="/usr/lib/hadoop-mapreduce/hadoop-streaming-2.2.0.2.0.6.0-101.jar")
Sys.setenv(HADOOP_COMMON_LIB_NATIVE_DIR="/usr/lib/hadoop/lib/native/")
library(rmr2);
library(rhdfs);
library(lubridate);
hdfs.init();
f = hdfs.file("/bigdata/rawdata/201312.csv","r",buffersize=104857600);
m = hdfs.read(f);
c = rawToChar(m);
data = read.table(textConnection(c), sep = ",");
*************************************************************************************************
读完後,发现它只读进了前一千五百多笔的资料,正确应该有一亿多笔
*************************************************************************************************
去Google了一下,有查到下列这个解的方向
“rhdfs uses the java api for reading files stored in hdfs.
That api will not necessarily read the entire file in one shot.
It will return some number of bytes for each read.
When it reaches the end of the file it returns -1.
In the case of rhdfs, and end of the file will return NULL.
So, you need to loop on the hdfs.read call until NULL is returned”
不过看了rhdfs的手册,并没有仔细提到上面解法关於hdfs.read()的行为:<
不知道有人有这方面经验吗?
[关键字]:
R, Large Scale Data Set, Big Data, Hadoop, RHadoop, CSV, HDFS, rhdfs
Thanks in advance!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.251.38.1
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/R_Language/M.1400472388.A.FDB.html
※ 编辑: dppman (60.251.38.1), 05/19/2014 12:07:32
※ 编辑: dppman (60.251.38.1), 05/19/2014 12:18:34
1F:推 Wush978:有没有考虑先用hadoop fs -get 把资料拿到local端再读取? 05/19 13:01
2F:→ dppman:hadoop fs -get回local那样效能更差:~ 05/19 15:13
3F:→ dppman:而且就不能利用到RHadoop的rmr2用R写MapReduce程式:~ 05/19 15:14
4F:推 Wush978:文件中是有说这些API都是streaming style 05/19 15:46
5F:→ dppman:在Local的话,连读前面的一千五百多比都有问题:~ 05/19 16:11
6F:→ Wush978:设定一下colClasses, 有差别(用rmr的方式也有差) 05/19 16:22
7F:→ Wush978:另外记得看看你电脑的记忆体够不够 05/19 16:23
8F:→ dppman:其实我现在应该就是卡在我程式的m=hdfs.read(f)那边@@ 05/19 16:25
※ 编辑: dppman (59.120.150.53), 05/19/2014 16:26:22
9F:→ Wush978:文件说明得很清楚,你要不停的呼叫hdfs.read(f)直到它回 05/19 16:33
10F:→ Wush978:NULL 05/19 16:33
11F:→ Wush978:但是记忆体问题是和hdfs.read无关喔,如果不够读到後来也 05/19 16:33
12F:→ Wush978:会出问题 05/19 16:33
13F:→ gsuper:一亿笔相当大...何不分拆? 05/26 10:31
14F:→ dppman:如果是一台32G or 64G记忆体的Linux上Run R,一亿笔怎麽拆 05/29 11:58
15F:→ dppman:比较好呢? 05/29 11:58
16F:→ gsuper:一万笔一万笔的读取吧 05/29 13:50
17F:→ gsuper:#1HLjk2HI 参考我这篇的最後一段 05/29 13:51