【问题标题】:How to explode a subset of data in Spark?如何在 Spark 中分解数据子集?
【发布时间】:2016-05-06 04:05:46
【问题描述】:

我一直在尝试重复 Zeppelin notebook Magellan 的示例,但使用具有地理位置信息的资产(资产 DF)并尝试将它们映射到邮政编码(邮政编码 DF)。我从 USGS 获得了邮政编码 shapefile,并将其定位到 Spark 中。

这是资产 DF 的样子。它由资产 ID 和地图上的一个点组成。

+---------+--------------------+
|    asset|               point|
+---------+--------------------+
|       10|Point(-96.7595319...|
|       11|Point(4.7115951, ...|

邮政编码 DF 是根据美国邮政编码的 USGS shapefile 构建的。这是我用麦哲伦

val zipcodes = magellanContext.read.format("magellan").
load("magellan_us_states").
select($"polygon", $"metadata").
cache()

结果是邮政编码DF是

+--------------------+--------------------+
|             polygon|            metadata|
+--------------------+--------------------+
|Polygon(5, Wrappe...|Map(YEAR_ADM ->  ...|
|Polygon(5, Wrappe...|Map(YEAR_ADM ->  ...|

然后我将两个 DF 连接在一起并进行查询

val joined = zipcodes.
join(assets).
where($"point" within $"polygon").
select($"asset", explode($"metadata").as(Seq("k", "v"))).
withColumnRenamed("v", "state").
drop("k").
cache()

结果如下:

+--------+--------------------+
|  asset#|               state|
+--------+--------------------+
|10      |Arizona             |
|10      |                  48|
|10      |                1903|
|10      |                  04|
|10      |              23.753|
|10      |  February          |
|10      |                1912|
|10      |              28.931|
|10      |                  14|
|11      |North Carolina      |
...

问题是,当我分解元数据时,我只想要状态。我如何分解这些数据,以便我最终得到看起来像这样的表格 -

+--------+--------------------+
|  asset#|               state|
+--------+--------------------+
|10      |Arizona             |
|11      |North Carolina      |
|12      |Arizona             |
...

【问题讨论】:

    标签: apache-spark apache-spark-sql apache-zeppelin


    【解决方案1】:

    我如何分解这些数据,以便我最终得到看起来像这样的表格

    不要使用explode。相反,您只需 select 感兴趣的领域:

    df.select($"asset",  $"metadata".getItem("state").alias("state"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-01
      • 2017-08-11
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多