chunk.apply

Rで学ぶスケーラブルなデータ処理

Simon Urbanek

Member of R-Core, Lead Inventive Scientist, AT&T Labs Research

chunk.apply()

  • ループ処理を抽象化する
  • 並列実行が可能
  • iotoolshmr の基盤であり、Apache Hadoop インフラ上でデータ処理ができる
Rで学ぶスケーラブルなデータ処理

mstrsplit() でチャンクを行列として読み込む

# Use chunk.apply to get chunks of rows from foo.csv
chunk_col_sums <- chunk.apply("foo.csv",

# A function to process each of the chunk function(chunk) { # Turn the chunk into a matrix m <- mstrsplit(chunk, type = "numeric", sep = ",") # Return the column sums colSums(m) }, # Maximum chunk size in bytes CH.MAX.SIZE = 1e5)
# Get the total sum colSums(chunk_col_sums)
Rで学ぶスケーラブルなデータ処理

dstrsplit() でチャンクをデータフレームとして読み込む

# Use chunk.apply to get chunks of rows from foo.csv
chunk_col_sums <- chunk.apply("foo.csv",

 # A function to process each of the chunk
 function(chunk) {
   # Turn the chunk into a data frame
   d <- dstrsplit(chunk, col_types = rep("numeric", 3), sep = ",")
   # Return the column sums
   colSums(d)
 }, 
 # Maximum chunk size in bytes
 CH.MAX.SIZE = 1e5)

# Get the total sum
colSums(chunk_col_sums)
Rで学ぶスケーラブルなデータ処理

chunk.apply() の並列化

# Use chunk.apply to get chunks of rows from foo.csv
chunk_col_sums <- chunk.apply("foo.csv",

 # A function to process each of the chunk
 function(chunk) {

   # Turn the chunk into a data frame
   d <- dstrsplit(chunk, col_types = rep("numeric", 3), sep = ",")
   colSums(d)
 }, 
 # 2 processors read and process data
 CH.PARALLEL = 2)

# Get the total sum
colSums(chunk_col_sums)
Rで学ぶスケーラブルなデータ処理

並列化に関する注意点

  • プロセッサ数を増やしても、必ずしも処理が速くなるわけではない
  • 単一マシン上でプロセッサを追加しても、収穫逓減が生じることが多い
Rで学ぶスケーラブルなデータ処理

練習しましょう!

Rで学ぶスケーラブルなデータ処理

Preparing Video For Download...