作者Wush978 (拒看低质媒体)
看板R_Language
标题Re: [问题] 透过R使用Rhdfs package读取超大CSV档
时间Thu May 22 13:32:20 2014
※ 引述《dppman (*^o^*)》之铭言:
: ※ 引述《dppman (*^o^*)》之铭言:
: : [问题类型]:
: : 程式谘询(我想用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!
: 我试了repeat:
: repeat {
: m = hdfs.read(f)
: c = rawToChar(m)
: print(c)
: if ( is.null(c) ) break
: }
: 可是跑好久还没跑完...
: 我是用Web版的RStudio开发,看了一下m的type是raw...
: Sorry..我只剩C还在脑中有,R实在跟他不熟....
: 不知道是否有人可以指点一下...,这样的写法是否OK?怎麽增加效能呢?
: Thanks in advance!
:
建议你先测量一下throughput
```r
start.time <- Sys.time()
repeat {
m = hdfs.read(f)
duration <- as.numeric(difftime(Sys.time(), start.time, unit = "secs"))
print(length(m) / duration) # 每秒的bytes数
start.time <- Sys.time()
}
```
先看一下hdfs.read的效能,如果throughput是1MB / s的速度的话
20G 需要 20*2^30 / 2^20 秒,大约是5.6小时
---
也建议你另外量测一下hdfs fs -put 的频宽,
拿来和你在R 里面的测到的throughput做比较。
两者的差距是你可以在R 里面优化的程度。
所以如果两者差不多快的话,其实你R 怎麽改也是这样。
FYI
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.56.60
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/R_Language/M.1400736744.A.C95.html
※ 编辑: Wush978 (220.135.56.60), 05/22/2014 13:33:22