structfield问题

xn1cxnb4  于 2021-07-09  发布在  Spark
关注(0)|答案(1)|浏览(399)

我想为一个表指定一个模式,我使用以下代码在java中执行此操作:

StructType schema =  new StructType(List
                (
                        StructField("id", NumericType,  true),
                        StructField("text", StringType,  true),
                        StructField("user", StringType,  true)
                ));

但是 NumericType , StringType 表示“期望表达式”时出错。如何解决这个问题?
谢谢您。

41ik7eoe

41ik7eoe1#

您的代码包含一些错误。你需要使用 new 要创建的关键字 StructField 你不能使用 List(StructField, ...) . 而且,没有 NumericType 在spark sql类型中,您可能希望使用 IntegerType . 请参阅可用的spark数据类型。
也就是说,你可以试试这个:

StructType schema = new StructType(new StructField[]{
                new StructField("id", IntegerType, true, null),
                new StructField("text", StringType, true, null),
                new StructField("user", StringType, true, null)
        });

或者将静态导入与中提供的方法一起使用 DataTypes 工厂:

import static org.apache.spark.sql.types.DataTypes.*;

StructType schema = createStructType(Arrays.asList(
                createStructField("id", IntegerType, true),
                createStructField("text", StringType, true),
                createStructField("user", StringType, true)
        ));

相关问题