【问题标题】:Is there a way to write pyspark dataframe to azure cache for redis?有没有办法将 pyspark 数据帧写入 redis 的 azure 缓存?
【发布时间】:2021-07-02 10:07:33
【问题描述】:

我有一个包含 2 列的 pyspark 数据框。我为 redis 实例创建了一个 azure 缓存。我想将pyspark数据框写入redis,数据框的第一列作为键,第二列作为值。我如何在 azure 中做到这一点?

【问题讨论】:

    标签: python azure pyspark redis azure-redis-cache


    【解决方案1】:

    你需要利用这个库:https://github.com/RedisLabs/spark-redis 以及所需的相关 jar(取决于您使用的 spark+scala 版本)。

    在我的例子中,我在 spark cluster(Scala=2.12) 最新 spark 上安装了 3 个 jars:

    1. spark_redis_2_12_2_6_0.jar
    2. commons_pool2_2_10_0.jar
    3. jedis_3_6_0.jar

    沿着连接redis的配置:

    集群配置设置

    spark.redis.auth PASSWORD
    spark.redis.port 6379
    spark.redis.host xxxx.xxx.cache.windows.net
    

    确保您拥有 azure redis 4.0,该库可能与 6.0 有问题。 要推送的示例代码:

        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()
    )
    
     
    

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2018-06-24
      • 1970-01-01
      • 2020-11-26
      • 2019-03-18
      相关资源
      最近更新 更多