Apache spark 使用结构和数组类型定义架构

Apache spark 使用结构和数组类型定义架构,apache-spark,pyspark,Apache Spark,Pyspark,我有一个Spark数据框,它有一个模式(我读过并推断出): 我不知道如何定义数组和结构。下面是一个示例: from pyspark.sql.types import StructType, StructField, IntegerType, StringType, ArrayType, FloatType zipsSchema3 = StructType([ \ StructField("city", StringType(), True), \ StructFie

我有一个Spark数据框,它有一个模式(我读过并推断出):

我不知道如何定义数组和结构。

下面是一个示例:

from pyspark.sql.types import StructType, StructField, IntegerType, StringType, ArrayType, FloatType

zipsSchema3 = StructType([ \
  StructField("city", StringType(), True), \
  StructField("loc", \
    ArrayType(FloatType(), True), True), \
  StructField("pop", IntegerType(), True) \
])
将模式应用于JSON意味着使用.schema方法。这只会导致返回模式中指定的列,并且可能会更改列类型

loc
表示基本类型的数组: e、 g.
“loc”:[-72.576142,42.176443]

下面是一个具有多个字段的数组的更复杂示例:

fullTweetSchema = StructType([
  StructField("id", LongType(), True),
  StructField("user", StructType([
    StructField("id", LongType(), True),
    StructField("screen_name", StringType(), True),
    StructField("location", StringType(), True),
    StructField("friends_count", IntegerType(), True),
    StructField("followers_count", IntegerType(), True),
    StructField("description", StringType(), True)
  ]), True),
  StructField("entities", StructType([
    StructField("hashtags", ArrayType(
      StructType([
        StructField("text", StringType(), True)
      ]),
    ), True),
    StructField("urls", ArrayType(
      StructType([
        StructField("url", StringType(), True),
        StructField("expanded_url", StringType(), True),
        StructField("display_url", StringType(), True)
      ]),
    ), True)
  ]), True),
  StructField("lang", StringType(), True),
  StructField("text", StringType(), True),
  StructField("created_at", StringType(), True)
])

你能接受这个答案吗?
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, ArrayType, FloatType

zipsSchema3 = StructType([ \
  StructField("city", StringType(), True), \
  StructField("loc", \
    ArrayType(FloatType(), True), True), \
  StructField("pop", IntegerType(), True) \
])
fullTweetSchema = StructType([
  StructField("id", LongType(), True),
  StructField("user", StructType([
    StructField("id", LongType(), True),
    StructField("screen_name", StringType(), True),
    StructField("location", StringType(), True),
    StructField("friends_count", IntegerType(), True),
    StructField("followers_count", IntegerType(), True),
    StructField("description", StringType(), True)
  ]), True),
  StructField("entities", StructType([
    StructField("hashtags", ArrayType(
      StructType([
        StructField("text", StringType(), True)
      ]),
    ), True),
    StructField("urls", ArrayType(
      StructType([
        StructField("url", StringType(), True),
        StructField("expanded_url", StringType(), True),
        StructField("display_url", StringType(), True)
      ]),
    ), True)
  ]), True),
  StructField("lang", StringType(), True),
  StructField("text", StringType(), True),
  StructField("created_at", StringType(), True)
])