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...