【问题标题】:Write Spark dataframe to Redshift:save StructField(user_agent,ArrayType(StringType,true),true)将 Spark 数据帧写入 Redshift:保存 StructField(user_agent,ArrayType(StringType,true),true)
【发布时间】:2016-06-03 19:57:13
【问题描述】:

我有一个数据框,架构包含一个 Array[String] 字段:

 StructField("user_agent", ArrayType apply (StringType, true))

   ...
   myDataframe.printSchema
(an excerpt)
 |-- user_agent: array (nullable = true)
 |    |-- element: string (containsNull = true)

我正在使用 com.databricks.spark.redshift 包写入 Redshift。我收到一个错误:

java.lang.IllegalArgumentException: Don't know how to save StructField(user_agent,ArrayType(StringType,true),true) to JDBC
        at com.databricks.spark.redshift.JDBCWrapper$$anonfun$schemaString$1.apply(RedshiftJDBCWrapper.scala:253)
        at com.databricks.spark.redshift.JDBCWrapper$$anonfun$schemaString$1.apply(RedshiftJDBCWrapper.scala:233)

是否可以使用这个包将这样的数据类型写入 Redshift?

【问题讨论】:

  • 我遇到了同样的问题,最终将数组转换为字符串。

标签: apache-spark dataframe amazon-redshift


【解决方案1】:

spark-redshift 支持以下数据类型:

field.dataType match {
          case IntegerType => "INTEGER"
          case LongType => "BIGINT"
          case DoubleType => "DOUBLE PRECISION"
          case FloatType => "REAL"
          case ShortType => "INTEGER"
          case ByteType => "SMALLINT" // Redshift does not support the BYTE type.
          case BooleanType => "BOOLEAN"
          case StringType =>
            if (field.metadata.contains("maxlength")) {
              s"VARCHAR(${field.metadata.getLong("maxlength")})"
            } else {
              "TEXT"
            }
          case TimestampType => "TIMESTAMP"
          case DateType => "DATE"
          case t: DecimalType => s"DECIMAL(${t.precision},${t.scale})"
          case _ => throw new IllegalArgumentException(s"Don't know how to save $field to JDBC")
}

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2017-04-17
    • 2021-11-10
    • 2020-07-20
    • 2021-12-11
    • 2018-08-13
    • 2018-09-06
    相关资源
    最近更新 更多