Reputation: 35
I'm having a pyspark dataframe with 2 columns. I created a azure cache for redis instance. I would like to write the pyspark dataframe to redis with first column of dataframe as key and second column as value. How can I do it in azure?
Upvotes: 0
Views: 1520
Reputation: 780
You need to leverage this library:https://github.com/RedisLabs/spark-redis along with the associated jar needed(depending on which version of spark+scala you are using).
In my case I have installed 3 jars on spark cluster(Scala=2.12) latest spark:
Along the configuration for connecting to redis:
spark.redis.auth PASSWORD
spark.redis.port 6379
spark.redis.host xxxx.xxx.cache.windows.net
Make sure you have azure redis 4.0, the library might have issue with 6.0. Sample code to push:
from pyspark.sql.types import StructType, StructField, StringType
schema = StructType([
StructField("id", StringType(), True),
StructField("colA", StringType(), True),
StructField("colB", StringType(), True)
])
data = [
['1', '8', '2'],
['2', '5', '3'],
['3', '3', '1'],
['4', '7', '2']
]
df = spark.createDataFrame(data, schema=schema)
df.show()
--------------
(
df.
write.
format("org.apache.spark.sql.redis").
option("table", "mytable").
option("key.column", "id").
save()
)
Upvotes: 1