【问题标题】:How to change JSON structure on pyspark?如何更改 pyspark 上的 JSON 结构?
【发布时间】:2020-09-02 11:14:21
【问题描述】:

我有两个被kafka读取的json文件,这是他们的printSchema()

JSON1 打印架构:

root
 |-- _id: string (nullable = true)
 |-- Data: string (nullable = true)
 |-- NomeAzienda: string (nullable = true)
 |-- Valori_Di_Borsa: struct (nullable = false)
 |    |-- PrezzoUltimoContratto: double (nullable = true)
 |    |-- Var%: double (nullable = true)
 |    |-- VarAssoluta: double (nullable = true)
 |    |-- OraUltimoContratto: string (nullable = true)
 |    |-- QuantitaUltimo: double (nullable = true)
 |    |-- QuantitaAcquisto: double (nullable = true)
 |    |-- QuantitaVendita: double (nullable = true)
 |    |-- QuantitaTotale: double (nullable = true)
 |    |-- NumeroContratti: double (nullable = true)
 |    |-- MaxOggi: double (nullable = true)
 |    |-- MinOggi: double (nullable = true)

JSON2 printSchema():

root
 |-- _id: string (nullable = true)
 |-- News: struct (nullable = false)
 |    |-- TitoloNews: string (nullable = true)
 |    |-- TestoNews: string (nullable = true)
 |    |-- DataNews: string (nullable = true)
 |    |-- OraNews: long (nullable = true)
 |    |-- SoggettoNews: string (nullable = true)

加入两个 JSON,我得到了这个 printSchema():

root
 |-- _id: string (nullable = true)
 |-- Data: string (nullable = true)
 |-- NomeAzienda: string (nullable = true)
 |-- Valori_Di_Borsa: struct (nullable = false)
 |    |-- PrezzoUltimoContratto: double (nullable = true)
 |    |-- Var%: double (nullable = true)
 |    |-- VarAssoluta: double (nullable = true)
 |    |-- OraUltimoContratto: string (nullable = true)
 |    |-- QuantitaUltimo: double (nullable = true)
 |    |-- QuantitaAcquisto: double (nullable = true)
 |    |-- QuantitaVendita: double (nullable = true)
 |    |-- QuantitaTotale: double (nullable = true)
 |    |-- NumeroContratti: double (nullable = true)
 |    |-- MaxOggi: double (nullable = true)
 |    |-- MinOggi: double (nullable = true)
 |-- _id: string (nullable = true)
 |-- News: struct (nullable = false)
 |    |-- TitoloNews: string (nullable = true)
 |    |-- TestoNews: string (nullable = true)
 |    |-- DataNews: string (nullable = true)
 |    |-- OraNews: long (nullable = true)
 |    |-- SoggettoNews: string (nullable = true)

但我想要的结果是这样的:

更新根目录:

 -- _id: string (nullable = true)
 -- Data: string (nullable = true)
 -- NomeAzienda: string (nullable = true)
 -- Valori_Di_Borsa: struct (nullable = false)
     |-- PrezzoUltimoContratto: double (nullable = true)
     |-- Var%: double (nullable = true)
     |-- VarAssoluta: double (nullable = true)
     |-- OraUltimoContratto: string (nullable = true)
     |-- QuantitaUltimo: double (nullable = true)
     |-- QuantitaAcquisto: double (nullable = true)
     |-- QuantitaVendita: double (nullable = true)
     |-- QuantitaTotale: double (nullable = true)
     |-- NumeroContratti: double (nullable = true)
     |-- MaxOggi: double (nullable = true)
     |-- MinOggi: double (nullable = true)
     |-- News: struct (nullable = false)
                |-- id: string (nullable = true)
                |-- TitoloNews: string (nullable = true)
                |-- TestoNews: string (nullable = true)
                |-- DataNews: string (nullable = true)
                |-- OraNews: long (nullable = true)
                |-- SoggettoNews: string (nullable = true)

我如何使用 pyspark 来做到这一点?

这是我的代码:

   df_borsa = spark.readStream.format("kafka") \
                  .option("kafka.bootstrap.servers", kafka_broker) \
                  .option("startingOffsets", "latest") \
                  .option("subscribe","Be_borsa") \
                  .load() \
                  .selectExpr("CAST(value AS STRING)") 

   df_news = spark.readStream.format("kafka") \
                  .option("kafka.bootstrap.servers", kafka_broker) \
                  .option("startingOffsets", "latest") \
                  .option("subscribe","Ita_news") \
                  .load() \
                  .selectExpr("CAST(value AS STRING)") 

    df_borsa =df_borsa.withColumn("Valori_Di_Borsa",F.struct(F.col("PrezzoUltimoContratto"),F.col("Var%"),F.col("VarAssoluta"),F.col("OraUltimoContratto"),F.col("QuantitaUltimo"),F.col("QuantitaAcquisto"),F.col("QuantitaVendita"),F.col("QuantitaTotale"),F.col("NumeroContratti"),F.col("MaxOggi"),F.col("MinOggi")))

    df_borsa.printSchema()

    df_news = df_news.withColumn("News",F.struct(F.col("TitoloNews"),F.col("TestoNews"),F.col("DataNews"),F.col("OraNews"),F.col("SoggettoNews")))

    df_news.printSchema()

    df_join = df_borsa.join(df_news)

    df_join.printSchema()

【问题讨论】:

    标签: python json apache-spark pyspark apache-kafka


    【解决方案1】:

    检查下面的代码。

    提取结构Valori_Di_Borsa 列,添加News 列并重新构造结构。

    df_join = df_borsa.join(df_news)
    .withColumn("Valori_Di_Borsa",F.struct(F.col("Valori_Di_Borsa.*"),F.col("News"))))
    
    

    【讨论】:

    • 感谢您的回复!但它给了我这个错误:TypeError: _ () takes 1 positional argument but 2 were given
    • 非常感谢!我在 F.col ("Valori_Di_Borsa.*")、F.col ("News") 中插入了一个 ")",现在它可以工作了!
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2019-11-17
    • 2022-01-23
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多