如何将新的 Struct 列添加到 DataFrame [英] How to add a new Struct column to a DataFrame

查看:38
本文介绍了如何将新的 Struct 列添加到 DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试从 MongoDB 中提取一个数据库,并使用 Spark 通过 geo_points 摄取到 ElasticSearch.

I'm currently trying to extract a database from MongoDB and use Spark to ingest into ElasticSearch with geo_points.

Mongo 数据库有纬度和经度值,但 ElasticSearch 要求将它们转换为 geo_point 类型.

The Mongo database has latitude and longitude values, but ElasticSearch requires them to be casted into the geo_point type.

Spark 有没有办法将 latlon 列复制到一个新的 arraystruct 列?

Is there a way in Spark to copy the lat and lon columns to a new column that is an array or struct?

感谢任何帮助!

推荐答案

我假设您从某种平面模式开始:

I assume you start with some kind of flat schema like this:

root
 |-- lat: double (nullable = false)
 |-- long: double (nullable = false)
 |-- key: string (nullable = false)

首先让我们创建示例数据:

First lets create example data:

import org.apache.spark.sql.Row
import org.apache.spark.sql.functions.{col, udf}
import org.apache.spark.sql.types._

val rdd = sc.parallelize(
    Row(52.23, 21.01, "Warsaw") :: Row(42.30, 9.15, "Corte") :: Nil)

val schema = StructType(
    StructField("lat", DoubleType, false) ::
    StructField("long", DoubleType, false) ::
    StructField("key", StringType, false) ::Nil)

val df = sqlContext.createDataFrame(rdd, schema)

一个简单的方法是使用 udf 和 case 类:

An easy way is to use an udf and case class:

case class Location(lat: Double, long: Double)
val makeLocation = udf((lat: Double, long: Double) => Location(lat, long))

val dfRes = df.
   withColumn("location", makeLocation(col("lat"), col("long"))).
   drop("lat").
   drop("long")

dfRes.printSchema

我们得到

root
 |-- key: string (nullable = false)
 |-- location: struct (nullable = true)
 |    |-- lat: double (nullable = false)
 |    |-- long: double (nullable = false)

一种困难的方法是转换您的数据并在之后应用架构:

A hard way is to transform your data and apply schema afterwards:

val rddRes = df.
    map{case Row(lat, long, key) => Row(key, Row(lat, long))}

val schemaRes = StructType(
    StructField("key", StringType, false) ::
    StructField("location", StructType(
        StructField("lat", DoubleType, false) ::
        StructField("long", DoubleType, false) :: Nil
    ), true) :: Nil 
)

sqlContext.createDataFrame(rddRes, schemaRes).show

我们得到了预期的输出

+------+-------------+
|   key|     location|
+------+-------------+
|Warsaw|[52.23,21.01]|
| Corte|  [42.3,9.15]|
+------+-------------+

从头开始创建嵌套模式可能很乏味,所以如果可以的话,我会推荐第一种方法.如果您需要更复杂的结构,可以轻松扩展它:

Creating nested schema from scratch can be tedious so if you can I would recommend the first approach. It can be easily extended if you need more sophisticated structure:

case class Pin(location: Location)
val makePin = udf((lat: Double, long: Double) => Pin(Location(lat, long))

df.
    withColumn("pin", makePin(col("lat"), col("long"))).
    drop("lat").
    drop("long").
    printSchema

我们得到了预期的输出:

and we get expected output:

root
 |-- key: string (nullable = false)
 |-- pin: struct (nullable = true)
 |    |-- location: struct (nullable = true)
 |    |    |-- lat: double (nullable = false)
 |    |    |-- long: double (nullable = false)

不幸的是,您无法控制 nullable 字段,因此如果它对您的项目很重要,则必须指定架构.

Unfortunately you have no control over nullable field so if is important for your project you'll have to specify schema.

终于可以使用1.4中引入的struct函数了:

Finally you can use struct function introduced in 1.4:

import org.apache.spark.sql.functions.struct

df.select($"key", struct($"lat", $"long").alias("location"))

这篇关于如何将新的 Struct 列添加到 DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆