小编nag*_*nag的帖子

Spark java使用java.util.Map类型创建行的问题

使用火花2.1

我在里面创建了一个带有MapDataType的DataSet

StructType schema = new StructType(new StructField[]{
                new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
                new StructField("words", DataTypes.StringType, false, Metadata.empty()),
                new StructField("label", DataTypes.IntegerType, false, Metadata.empty()),
                new StructField("features", DataTypes.createMapType(DataTypes.StringType, DataTypes.IntegerType), false, Metadata.empty())
        });

        Map<String,Integer> abc = new HashMap<String,Integer>();
        abc.put("abc", 1);
        Row r = RowFactory.create(0, "Hi these are words ", 1, abc);    
        List<Row> data = Arrays.asList(r);
        Dataset<Row> wordDataFrame = spark.createDataFrame(data, schema);
        wordDataFrame.show();
Run Code Online (Sandbox Code Playgroud)

以上代码工作正常.

但是当我尝试在这个DataSet上调用map函数(用新的HashMap替换Map DataType条目)时,我收到以下错误.

StructType schema = new StructType(new StructField[]{
                new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
                new StructField("words", DataTypes.StringType, false, Metadata.empty()),
                new StructField("label", DataTypes.IntegerType, …
Run Code Online (Sandbox Code Playgroud)

java apache-spark spark-dataframe

9
推荐指数
1
解决办法
3105
查看次数

标签 统计

apache-spark ×1

java ×1

spark-dataframe ×1