【问题标题】:Pyspark: Iterating through multiline nested json to build a dataframe [closed]Pyspark:遍历多行嵌套 json 以构建数据框 [关闭]
【发布时间】:2021-03-17 16:08:50
【问题描述】:

伙计们,我需要一些帮助来遍历 pyspark 中的以下 json...并构建一个数据框:

{
    "success": true,
    "result": {
        "0x00e01a648ff41346cdeb873182383333d2184dd1": {
            "id": 130,
            "name": "xn--mytherwallet-fvb.com",
            "url": "http://xn--mytherwallet-fvb.com",
            "coin": "ETH",
            "category": "Phishing",
            "subcategory": "MyEtherWallet",
            "description": "Homoglyph",
            "addresses": [
                "0x00e01a648ff41346cdeb873182383333d2184dd1",
                "0x11e01a648ff41346cdeb873182383333d2184dd1"
            ],
            "reporter": "MyCrypto",
            "status": "Offline"
        },
        "0x858457daa7e087ad74cdeeceab8419079bc2ca03": {
            "id": 1200,
            "name": "myetherwallet.in",
            "url": "http://myetherwallet.in",
            "coin": "ETH",
            "category": "Phishing",
            "subcategory": "MyEtherWallet",
            "addresses": ["0x858457daa7e087ad74cdeeceab8419079bc2ca03"],
            "reporter": "MyCrypto",
            "ip": "159.8.210.35",
            "nameservers": [
                "ns2.eftydns.com",
                "ns1.eftydns.com"
            ],
            "status": "Active"
        }
    }
}

我需要构建一个代表地址列表的数据框。

【问题讨论】:

    标签: apache-spark pyspark


    【解决方案1】:

    我将您的 JSON 格式化为 SPARK-Readable 格式。

    {"success": true, "result": {"0x00e01a648ff41346cdeb873182383333d2184dd1": {"id": 130, "name": "xn--mytherwallet-fvb.com", "url": "http://xn--mytherwallet-fvb.com", "coin": "ETH", "category": "Phishing", "subcategory": "MyEtherWallet", "description": "Homoglyph", "addresses": ["0x00e01a648ff41346cdeb873182383333d2184dd1", "0x11e01a648ff41346cdeb873182383333d2184dd1"], "reporter": "MyCrypto", "status": "Offline"}, "0x858457daa7e087ad74cdeeceab8419079bc2ca03": {"id": 1200, "name": "myetherwallet.in", "url": "http://myetherwallet.in", "coin": "ETH", "category": "Phishing", "subcategory": "MyEtherWallet", "addresses": ["0x858457daa7e087ad74cdeeceab8419079bc2ca03"], "reporter": "MyCrypto", "ip": "159.8.210.35", "nameservers": ["ns2.eftydns.com", "ns1.eftydns.com"], "status": "Active"}}}
    

    读取 JSON

    val df = spark.read.json("/my_data.json")
    
    df.printSchema()
    df.show(false)
    

    输出

    root
     |-- result: struct (nullable = true)
     |    |-- 0x00e01a648ff41346cdeb873182383333d2184dd1: struct (nullable = true)
     |    |    |-- addresses: array (nullable = true)
     |    |    |    |-- element: string (containsNull = true)
     |    |    |-- category: string (nullable = true)
     |    |    |-- coin: string (nullable = true)
     |    |    |-- description: string (nullable = true)
     |    |    |-- id: long (nullable = true)
     |    |    |-- name: string (nullable = true)
     |    |    |-- reporter: string (nullable = true)
     |    |    |-- status: string (nullable = true)
     |    |    |-- subcategory: string (nullable = true)
     |    |    |-- url: string (nullable = true)
     |    |-- 0x858457daa7e087ad74cdeeceab8419079bc2ca03: struct (nullable = true)
     |    |    |-- addresses: array (nullable = true)
     |    |    |    |-- element: string (containsNull = true)
     |    |    |-- category: string (nullable = true)
     |    |    |-- coin: string (nullable = true)
     |    |    |-- id: long (nullable = true)
     |    |    |-- ip: string (nullable = true)
     |    |    |-- name: string (nullable = true)
     |    |    |-- nameservers: array (nullable = true)
     |    |    |    |-- element: string (containsNull = true)
     |    |    |-- reporter: string (nullable = true)
     |    |    |-- status: string (nullable = true)
     |    |    |-- subcategory: string (nullable = true)
     |    |    |-- url: string (nullable = true)
     |-- success: boolean (nullable = true)
    
    +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
    |result                                                                                                                                                                                                                                                                                                                                                                                                                                     |success|
    +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
    |[[WrappedArray(0x00e01a648ff41346cdeb873182383333d2184dd1, 0x11e01a648ff41346cdeb873182383333d2184dd1),Phishing,ETH,Homoglyph,130,xn--mytherwallet-fvb.com,MyCrypto,Offline,MyEtherWallet,http://xn--mytherwallet-fvb.com],[WrappedArray(0x858457daa7e087ad74cdeeceab8419079bc2ca03),Phishing,ETH,1200,159.8.210.35,myetherwallet.in,WrappedArray(ns2.eftydns.com, ns1.eftydns.com),MyCrypto,Active,MyEtherWallet,http://myetherwallet.in]]|true   |
    +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
    

    【讨论】:

      【解决方案2】:

      在 Pyspark 中,您可以执行以下操作。无需重新格式化您的 json - 它已完美格式化,您只需将 .option('multiline', True) 传递给 json 阅读器。

      df = spark.read.option('multiline', True).json('test.json')
      

      获取地址:

      import pyspark.sql.functions as F
      
      df2 = df.select('result.*')
      df3 = df2.select(
          F.explode(
              F.concat(
                  *[F.col(f'{col}.addresses') for col in df2.columns]
              )
          ).alias('addresses')
      )
      
      df3.show(truncate=False)
      +------------------------------------------+
      |addresses                                 |
      +------------------------------------------+
      |0x00e01a648ff41346cdeb873182383333d2184dd1|
      |0x11e01a648ff41346cdeb873182383333d2184dd1|
      |0x858457daa7e087ad74cdeeceab8419079bc2ca03|
      +------------------------------------------+
      

      【讨论】:

      • 感谢您的评论...我很接近,但仍然无法正常工作。我似乎遇到了语法错误... >>> df3 = df2.select(F.explode(F.concat(*[F.col(f'{col}.addresses') for col in df2.columns] )).alias('addresses')) 文件“”,第 1 行 df3 = df2.select(F.explode(F.concat(*[F.col(f'{col}.addresses') for col在 df2.columns])).alias('addresses')) ^ SyntaxError: invalid syntax
      • @Ropanb 你在使用旧版本的 Python 吗?如果是这样,请尝试将f'{col}.addresses' 替换为'%s.addresses' % (col)
      猜你喜欢
      • 2021-12-31
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2020-05-06
      • 1970-01-01
      • 2021-12-29
      • 2019-04-27
      相关资源
      最近更新 更多