Confronto tra SparkR e sparklyr

Gli utenti R possono scegliere tra due API per Apache Spark: SparkR e sparklyr. Questo articolo confronta queste API. Databricks consiglia di scegliere una di queste API per sviluppare un'applicazione Spark in R. La combinazione di codice da entrambe queste API in un singolo script o in un singolo notebook o processo di Azure Databricks può rendere il codice più difficile da leggere e gestire.

Origini API

SparkR viene creato dalla community spark e dagli sviluppatori di Databricks. Per questo motivo SparkR segue attentamente le classi Spark Scala e l'API DataFrame.

Sparklyr ha iniziato con RStudio e da allora è stato donato alla Linux Foundation. sparklyr è strettamente integrato nel tidyverse sia nello stile di programmazione che tramite l'interoperabilità api con dplyr.

SparkR e sparklyr sono altamente in grado di lavorare con Big Data in R. Negli ultimi anni, i loro set di funzionalità si avvicinano alla parità.

Differenze tra le API

L'esempio di codice seguente illustra come usare SparkR e sparklyr da un notebook di Azure Databricks per leggere un file CSV dai set di dati di esempio in Spark.

# #############################################################################
# SparkR usage

# Note: To load SparkR into a Databricks notebook, run the following:

# library(SparkR)

# You can then remove "SparkR::" from the following function call.
# #############################################################################

# Use SparkR to read the airlines dataset from 2008.
airlinesDF <- SparkR::read.df(path        = "/databricks-datasets/asa/airlines/2008.csv",
                              source      = "csv",
                              inferSchema = "true",
                              header      = "true")

# Print the loaded dataset's class name.
cat("Class of SparkR object: ", class(airlinesDF), "\n")

# Output:
#
# Class of SparkR object: SparkDataFrame

# #############################################################################
# sparklyr usage

# Note: To install, load, and connect with sparklyr in a Databricks notebook,
# run the following:

# install.packages("sparklyr")
# library(sparklyr)
# sc <- sparklyr::spark_connect(method = "databricks")

# If you run "library(sparklyr)", you can then remove "sparklyr::" from the
# preceding "spark_connect" and from the following function call.
# #############################################################################

# Use sparklyr to read the airlines dataset from 2007.
airlines_sdf <- sparklyr::spark_read_csv(sc   = sc,
                                         name = "airlines",
                                         path = "/databricks-datasets/asa/airlines/2007.csv")

# Print the loaded dataset's class name.
cat("Class of sparklyr object: ", class(airlines_sdf))

# Output:
#
# Class of sparklyr object: tbl_spark tbl_sql tbl_lazy tbl

Tuttavia, se si tenta di eseguire una funzione sparklyr in un SparkDataFrame oggetto da SparkR o se si tenta di eseguire una funzione SparkR in un tbl_spark oggetto da sparklyr, non funzionerà, come illustrato nell'esempio di codice seguente.

# Try to call a sparklyr function on a SparkR SparkDataFrame object. It will not work.
sparklyr::sdf_pivot(airlinesDF, DepDelay ~ UniqueCarrier)

# Output:
#
# Error : Unable to retrieve a Spark DataFrame from object of class SparkDataFrame

## Now try to call s Spark R function on a sparklyr tbl_spark object. It also will not work.
SparkR::arrange(airlines_sdf, "DepDelay")

# Output:
#
# Error in (function (classes, fdef, mtable) :
#   unable to find an inherited method for function ‘arrange’ for signature ‘"tbl_spark", "character"’

Questo perché sparklyr converte le funzioni dplyr, arrange ad esempio in un piano di query SQL usato da SparkSQL. Questo non è il caso di SparkR, che include funzioni per le tabelle SparkSQL e i dataframe Spark. Questi comportamenti sono i motivi per cui Databricks non consiglia la combinazione di API SparkR e sparklyr nello stesso script, notebook o processo.

Interoperabilità API

In rari casi in cui non è possibile evitare di combinare le API SparkR e sparklyr, è possibile usare SparkSQL come tipo di bridge. Ad esempio, nel primo esempio di questo articolo sparklyr ha caricato il set di dati delle compagnie aeree dal 2007 in una tabella denominata airlines. È possibile usare la funzione SparkR sql per eseguire query su questa tabella, ad esempio:

top10delaysDF <- SparkR::sql("SELECT
                               UniqueCarrier,
                               DepDelay,
                               Origin
                             FROM
                               airlines
                             WHERE
                               DepDelay NOT LIKE 'NA'
                             ORDER BY DepDelay
                             DESC LIMIT 10")

# Print the class name of the query result.
cat("Class of top10delaysDF: ", class(top10delaysDF), "\n\n")

# Show the query result.
cat("Top 10 airline delays for 2007:\n\n")
head(top10delaysDF, 10)

# Output:
#
# Class of top10delaysDF: SparkDataFrame
#
# Top 10 airline delays for 2007:
#
#   UniqueCarrier DepDelay Origin
# 1            AA      999    RNO
# 2            NW      999    EWR
# 3            AA      999    PHL
# 4            MQ      998    RST
# 5            9E      997    SWF
# 6            AA      996    DFW
# 7            NW      996    DEN
# 8            MQ      995    IND
# 9            MQ      994    SJT
# 10           AA      993    MSY

Per altri esempi, vedere Usare dataframe e tabelle in R.