java.time.LocalDate 的 Apache spark.sql.types.DataTypes 是什么
What is the Apache spark.sql.types.DataTypes of java.time.LocalDate
我开发了 java pojo class 其中包含 java.time.LocalDate 成员变量 date
.
import java.io.Serializable;
import java.time.LocalDate;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EntityMySQL implements Serializable {
@JsonFormat(pattern="yyyy-MM-dd")
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate date;
private float value;
private String id;
private String title;
private static StructType structType = DataTypes.createStructType(new StructField[] {
DataTypes.createStructField("date", DataTypes.DateType, false), // this line throws Exception
DataTypes.createStructField("value", DataTypes.FloatType, false),
DataTypes.createStructField("id", DataTypes.StringType, false),
DataTypes.createStructField("title", DataTypes.StringType, false)
});
如您所见,“date”成员变量类型为java.time.LocalDate
。但是在静态 structType 变量中,
我将 date
的类型设置为 DateTypes.DateType
。当我将 pojo class 与 spark 数据框绑定时。它抛出如下错误,
Caused by: java.lang.RuntimeException: java.time.LocalDate is not a valid external type for schema of date
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.StaticInvoke_0$(Unknown Source)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.writeFields_0_0$(Unknown Source)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.apply(Unknown Source)
at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$Serializer.apply(ExpressionEncoder.scala:210)
当我设置日期成员变量为java.util.Date
时,spark DataTypes.DateType
是正确的配置,没有错误。但是在使用 java.time.LocalDate
的情况下,代码无法正常工作并抛出异常。如果我必须生成自定义日期类型,请告诉我如何生成。有什么想法吗?
java.time.LocalDate 不支持 Spark,即使您尝试为 java 日期类型编写编码器,它也不会工作。
我建议您将 java.time.LocalDate 转换为其他支持的类型,例如 java.sql.Timestamp 或 java.sql.Date 或字符串中的纪元或日期时间。
我开发了 java pojo class 其中包含 java.time.LocalDate 成员变量 date
.
import java.io.Serializable;
import java.time.LocalDate;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EntityMySQL implements Serializable {
@JsonFormat(pattern="yyyy-MM-dd")
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate date;
private float value;
private String id;
private String title;
private static StructType structType = DataTypes.createStructType(new StructField[] {
DataTypes.createStructField("date", DataTypes.DateType, false), // this line throws Exception
DataTypes.createStructField("value", DataTypes.FloatType, false),
DataTypes.createStructField("id", DataTypes.StringType, false),
DataTypes.createStructField("title", DataTypes.StringType, false)
});
如您所见,“date”成员变量类型为java.time.LocalDate
。但是在静态 structType 变量中,
我将 date
的类型设置为 DateTypes.DateType
。当我将 pojo class 与 spark 数据框绑定时。它抛出如下错误,
Caused by: java.lang.RuntimeException: java.time.LocalDate is not a valid external type for schema of date
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.StaticInvoke_0$(Unknown Source)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.writeFields_0_0$(Unknown Source)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.apply(Unknown Source)
at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$Serializer.apply(ExpressionEncoder.scala:210)
当我设置日期成员变量为java.util.Date
时,spark DataTypes.DateType
是正确的配置,没有错误。但是在使用 java.time.LocalDate
的情况下,代码无法正常工作并抛出异常。如果我必须生成自定义日期类型,请告诉我如何生成。有什么想法吗?
java.time.LocalDate 不支持 Spark,即使您尝试为 java 日期类型编写编码器,它也不会工作。
我建议您将 java.time.LocalDate 转换为其他支持的类型,例如 java.sql.Timestamp 或 java.sql.Date 或字符串中的纪元或日期时间。