Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from SparkR to sparklyr?

How do I convert a SparkDataFrame from SparkR into a tbl_spark from sparklyr?

A similar question was asked here: Convert spark dataframe to sparklyR table "tbl_spark".

The suggestion was to use the sdf_copy_to function, however, the input of this function must be an R object in instead of a SparkDataFrame.

Any suggestions to solve this problem?

Thanks!

like image 693
Ruth van Dongen Avatar asked Feb 04 '26 10:02

Ruth van Dongen


1 Answers

Use a temp Spark table to convert from SparkR::SparkDataFrame to sparklyr::tbl_spark.

Starting with a SparkDataFrame in SparkR

df_sparkr <- SparkR::createDataFrame(data.frame(
  x = 1:10
))

Create a temp table in Spark

SparkR::registerTempTable(df_sparkr, "temp_df")

Read the table using sparklyr

sc <- sparklyr::spark_connect(master = "local")
df_sparklyr <- dplyr::tbl(sc, "temp_df")

Here is a second method if your data is small. You can convert to a normal R data frame then copy into sparklyr. This is not recommended if the data frame is large.

df_normal <- SparkR::collect(df_sparkr)
df_sparklyr <- dplyr::copy_to(sc, df_normal)
like image 145
Paul Avatar answered Feb 06 '26 01:02

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!