chunk.apply

การประมวลผลข้อมูลขนาดใหญ่ใน R

Simon Urbanek

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

chunk.apply()

  • ซ่อนขั้นตอนการวนลูป
  • รองรับการประมวลผลแบบขนาน
  • iotools เป็นพื้นฐานของ hmr ซึ่งช่วยให้ประมวลผลข้อมูลบน Apache Hadoop ได้
การประมวลผลข้อมูลขนาดใหญ่ใน R

mstrsplit() อ่าน chunk เป็น matrix

# 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() อ่าน chunk เป็น data frame

# 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

ข้อควรทราบเกี่ยวกับการประมวลผลแบบขนาน

  • การเพิ่มจำนวน processor ไม่ได้ทำให้โค้ดเร็วขึ้นเสมอไป
  • โดยทั่วไป ผลที่ได้จะลดลงเรื่อย ๆ เมื่อเพิ่ม processor บนเครื่องเดียว
การประมวลผลข้อมูลขนาดใหญ่ใน R

มาฝึกกันเถอะ!

การประมวลผลข้อมูลขนาดใหญ่ใน R

Preparing Video For Download...