Synapse Notebook: Passing variables from %%pyspark cell to %%sparkr cell

2023-12-29T12:38:18.9766667+00:00

I would like to pass variables values from one cell executing Python (%%pyspark) in one cell to another cell executing R (%%sparkr). In the same notebook.

Is it possible?

My code looks like this :

#cell 1: executes pyspark (python code)

%%pyspark

var = 'A'

#cel2: executes sparkr (R code)

%%sparkr

print(paste0("value of var=",var))

Thanks in advance for the help.

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,240 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amira Bedhiafi 29,946 Reputation points
    2023-12-29T17:37:14.03+00:00

    One of the easiest ways to pass data between %%pyspark and %%sparkr cells is by creating a Spark DataFrame in Python and then accessing it in R.

    
       %%pyspark
    
       var = 'A'
    
       # Create a Spark DataFrame
    
       df = spark.createDataFrame([(var,)], ["var"])
    
       df.createOrReplaceTempView("var_df")
    
    

    In your R (%%sparkr) cell:

    
       %%sparkr
    
       # Access the DataFrame
    
       df <- sql("SELECT * FROM var_df")
    
       var_value <- collect(df)$var[1]
    
       print(paste0("value of var=", var_value))
    
    
    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.