Monday, June 29, 2015

Explorando SparkR

Un compañero de trabajo, me pidió que investigara sobre Spark y R. Así que lo más obvio era investigar sobre SparkR -;)

Instalé Scala, Hadoop, Spark y SparkR...no estoy seguro si Hadoop es necesario en este caso...pero quería tener la imagen completa -:)

En fín...me encontré con un código que lee líneas de un archivo y cuenta cuantas líneas tienen una "a" y cuantas lineas tienen una "b"...

Para este código utilicé la letra de Girls Not Grey por AFI...

SparkR.R
library(SparkR)

start.time <- Sys.time()
sc <- sparkR.init(master="local")
logFile <- "/home/blag/R_Codes/Girls_Not_Grey"
logData <- SparkR:::textFile(sc, logFile)
numAs <- count(SparkR:::filterRDD(logData, function(s) { grepl("a", s) }))
numBs <- count(SparkR:::filterRDD(logData, function(s) { grepl("b", s) }))
paste("Lines with a: ", numAs, ", Lines with b: ", numBs, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken



0.3167355 segundos...bastante rápido...me pregunto como le iría al R regular?

PlainR.R
library("stringr")

start.time <- Sys.time()
logFile <- "/home/blag/R_Codes/Girls_Not_Grey"
logfile<-read.table(logFile,header = F, fill = T)
logfile<-apply(logfile[,], 1, function(x) paste(x, collapse=" "))
df<-data.frame(lines=logfile)
a<-sum(apply(df,1,function(x) grepl("a",x)))
b<-sum(apply(df,1,function(x) grepl("b",x)))
paste("Lines with a: ", a, ", Lines with b: ", b, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken


Que bueno...0.01522398 segundos...espera...qué? No se supone que Spark sea mucho más veloz? Bueno...recuerdo haber leído en algun lado que Spark brilla con archivos grandes...

Bueno...preparé un archivo con 5 columnas y 1 millón de registros...veamos como vá...

SparkR.R
library(SparkR)

start.time <- Sys.time()
sc <- sparkR.init(master="local")
logFile <- "/home/blag/R_Codes/Doc_Header.csv"
logData <- SparkR:::textFile(sc, logFile)
numAs <- count(SparkR:::filterRDD(logData, function(s) { grepl("a", s) }))
numBs <- count(SparkR:::filterRDD(logData, function(s) { grepl("b", s) }))
paste("Lines with a: ", numAs, ", Lines with b: ", numBs, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken



26.45734 segundos para 1 millón de registros? Buen trabajo -:) Veamos si el R regular gana nuevamente...

PlainR.R
library("stringr")

start.time <- Sys.time()
logFile <- "/home/blag/R_Codes/Doc_Header.csv"
logfile<-read.csv(logFile,header = F)
logfile<-apply(logfile[,], 1, function(x) paste(x, collapse=" "))
df<-data.frame(lines=logfile)
a<-sum(apply(df,1,function(x) grepl("a",x)))
b<-sum(apply(df,1,function(x) grepl("b",x)))
paste("Lines with a: ", a, ", Lines with b: ", b, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken


48.31641 segundos? Parece que Spark fué prácticamente el doble de rápido esta vez...y este es un ejemplo de lo más sencillo...estoy seguro de que cuando la complejidad aumenta...la brecha es aún más grande...

Y seguro...Yo sé que mucha gente puede tomar mi código en  R y hacerlo aún más veloz que Spark...pero...este es mi blog...no el de ellos -;)

Regresaré apenas sepa más sobre SparkR -:D

Saludos,

Blag.
Development Culture.

No comments: