使用Java从CSV读取ApacheSpark数组浮点

使用Java从CSV读取ApacheSpark数组浮点,java,arrays,apache-spark,apache-spark-sql,apache-spark-mllib,Java,Arrays,Apache Spark,Apache Spark Sql,Apache Spark Mllib,我正在使用Java处理一个新的Spark项目。我必须从CSV文件中读取一些数据,这些CSV有一个浮动数组,我不知道如何在我的数据集中获得这个数组 我从CSV上读到: [CSV data image][1] https://imgur.com/a/PdrMhev 我试图通过这种方式获取数据: Dataset<Row> typedTrainingData = sparkSession.sql("SELECT CAST(IDp as String) IDp, CAST(Instt as

我正在使用Java处理一个新的Spark项目。我必须从CSV文件中读取一些数据,这些CSV有一个浮动数组,我不知道如何在我的数据集中获得这个数组

我从CSV上读到:

[CSV data image][1] https://imgur.com/a/PdrMhev
我试图通过这种方式获取数据:

Dataset<Row> typedTrainingData = sparkSession.sql("SELECT CAST(IDp as String) IDp, CAST(Instt as String) Instt, CAST(dataVector as String) dataVector FROM TRAINING_DATA");
正如您在模式中所看到的,我将数组读取为字符串,但希望获取为数组。推荐

我想在这个加载的数据中使用一些MLlib的机器学习算法,因此我想将数据作为数组


谢谢你们

首先定义您的模式

StructType customStructType = new StructType();
        customStructType = customStructType.add("_c0", DataTypes.StringType, false);
        customStructType = customStructType.add("_c1", DataTypes.StringType, false);
        customStructType = customStructType.add("_c2", DataTypes.createArrayType(DataTypes.LongType), false);
然后您可以将df映射到新模式

    Dataset<Row> newDF = oldDF.map((MapFunction<Row, Row>) row -> {

        String strings[] = row.getString(3).split(","); 
        long[] result = new long[strings.length];
        for (int i = 0; i < strings.length; i++)
        result[i] = Long.parseLong(strings[i]);

        return RowFactory.create(row.getString(0),row.getString(1),result);
    }, RowEncoder.apply(customStructType));
Dataset newDF=oldDF.map((MapFunction)行->{
String strings[]=row.getString(3).split(“,”);
long[]结果=新的long[strings.length];
for(int i=0;i
首先定义您的模式

StructType customStructType = new StructType();
        customStructType = customStructType.add("_c0", DataTypes.StringType, false);
        customStructType = customStructType.add("_c1", DataTypes.StringType, false);
        customStructType = customStructType.add("_c2", DataTypes.createArrayType(DataTypes.LongType), false);
然后您可以将df映射到新模式

    Dataset<Row> newDF = oldDF.map((MapFunction<Row, Row>) row -> {

        String strings[] = row.getString(3).split(","); 
        long[] result = new long[strings.length];
        for (int i = 0; i < strings.length; i++)
        result[i] = Long.parseLong(strings[i]);

        return RowFactory.create(row.getString(0),row.getString(1),result);
    }, RowEncoder.apply(customStructType));
Dataset newDF=oldDF.map((MapFunction)行->{
String strings[]=row.getString(3).split(“,”);
long[]结果=新的long[strings.length];
for(int i=0;i
能否显示您的CSV文件示例。CSV格式不支持数组。所以,您只需要使用数据向量中的with列构造一个数组string@BSeitkazin当然可以。在主帖子中编辑。@BSeitkazin StackOverFlow不允许我放置照片,因此我放置了一个链接,向您展示我的CSV是如何实现的。您能展示您的CSV文件吗?示例。CSV格式不支持数组。所以,您只需要使用数据向量中的with列构造一个数组string@BSeitkazin当然可以。在主帖子中编辑。@BSeitkazin StackOverFlow不允许我放照片,所以我放了一个链接,向您展示我的CSV是如何的。谢谢您的回复!我正在尝试使用它,但当我尝试使用它时,我遇到了下一个错误:
java.base/java.lang.String不能转换为java.base/java.lang.Long
可以是什么?谢谢@Mahmoud!!再次感谢你!但对我来说不起作用,它给了我这个错误:
,原因是:java.lang.NumberFormatException:for输入字符串:“-0.41”位于java.base/java.lang.NumberFormatException.forInputString
不知道发生了什么!你试过这段代码吗?你只需要把你的字符串转换为Long。使用您自己的代码,我写的答案是在自定义模式中读取df,谢谢您的回复!我正在尝试使用它,但当我尝试使用它时,我遇到了下一个错误:
java.base/java.lang.String不能转换为java.base/java.lang.Long
可以是什么?谢谢@Mahmoud!!再次感谢你!但对我来说不起作用,它给了我这个错误:
,原因是:java.lang.NumberFormatException:for输入字符串:“-0.41”位于java.base/java.lang.NumberFormatException.forInputString
不知道发生了什么!你试过这段代码吗?你只需要把你的字符串转换为Long。使用您自己的代码,我写的答案是在自定义模式中读取df