Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在spark sql java中将csv类型字符串转换为数据帧?_Java_Apache Spark_Apache Spark Sql_Spark Structured Streaming - Fatal编程技术网

如何在spark sql java中将csv类型字符串转换为数据帧?

如何在spark sql java中将csv类型字符串转换为数据帧?,java,apache-spark,apache-spark-sql,spark-structured-streaming,Java,Apache Spark,Apache Spark Sql,Spark Structured Streaming,我使用spark结构化流式api制作spark java客户端代码。这些代码从kafka中提取csv类型字符串 SparkSession spark = SparkSession.builder().master("local[*]").appName("KafkaMongoStream").getOrCreate(); Dataset<Row> df = spark.read().format("kafka&quo

我使用spark结构化流式api制作spark java客户端代码。这些代码从kafka中提取csv类型字符串

SparkSession spark = SparkSession.builder().master("local[*]").appName("KafkaMongoStream").getOrCreate();
        
Dataset<Row> df = spark.read().format("kafka").option("kafka.bootstrap.servers", "localhost:9092"))
            .option("subscribe", "topicForMongoDB")
            .option("startingOffsets", "earliest")
            .load()
            .selectExpr("CAST(value AS STRING)");
            
df.show();
然后我尝试将这些字符串转换为spark sql中的spark数据帧。首先,下面的代码是JavaPOJO类

public class EntityMongoDB implements Serializable {

    private Date date;
    private float value;
    private String id;
    private String title;
    private String state;
    private String frequency_short;
    private String units_short;
    private String seasonal_adjustment_short;
    
    private static StructType structType = DataTypes.createStructType(new StructField[] {
              
              DataTypes.createStructField("date", DataTypes.DateType, false),
              DataTypes.createStructField("value", DataTypes.FloatType, false),
              DataTypes.createStructField("id", DataTypes.StringType, false),
              DataTypes.createStructField("title", DataTypes.StringType, false),
              DataTypes.createStructField("state", DataTypes.StringType, false),
              DataTypes.createStructField("frequency_short", DataTypes.StringType, false),
              DataTypes.createStructField("units_short", DataTypes.StringType, false),
              DataTypes.createStructField("seasonal_adjustment_short", DataTypes.StringType, false)
    });
    
    public static StructType getStructType() {
        return structType;
    }
}
我编写代码将这些csv类型的字符串转换为数据帧

Dataset<Row> dfs = df.select(from_json(col("value"), EntityMongoDB.getStructType())
        .as("entityMongoDB"))
        .selectExpr("entityMongoDB.date", "entityMongoDB.value", "entityMongoDB.id", 
                "entityMongoDB.title", "entityMongoDB.state", "entityMongoDB.frequency_short", 
                "entityMongoDB.units_short", "entityMongoDB.seasonal_adjustment_short").toDF();

dfs.show();
dfs.printSchema();
但是生成的列充满了空值

+----+-----+----+-----+-----+---------------+-----------+-------------------------+
|date|value|  id|title|state|frequency_short|units_short|seasonal_adjustment_short|
+----+-----+----+-----+-----+---------------+-----------+-------------------------+
|null| null|null| null| null|           null|       null|                     null|
|null| null|null| null| null|           null|       null|                     null|
|null| null|null| null| null|           null|       null|                     null|
|null| null|null| null| null|           null|       null|                     null|
|null| null|null| null| null|           null|       null|                     null|

我认为dataframe的模式生成正确,但提取数据部分存在一些问题。任何答复都将不胜感激。致意

您在
列中的字符串不是有效的JSON,因此来自\u JSON的
在这里不起作用

对于Spark 3+,您可以使用@mck在评论中指出的:

Dataset<Row> dfs = df.select(from_csv(col("value"), EntityMongoDB.getStructType())
        .as("entityMongoDB"))
        .selectExpr("entityMongoDB.*").toDF(); 

另外,值中似乎有列名,可以过滤掉该行。

值列中的字符串不是有效的JSON,因此来自JSON的
在这里不起作用

对于Spark 3+,您可以使用@mck在评论中指出的:

Dataset<Row> dfs = df.select(from_csv(col("value"), EntityMongoDB.getStructType())
        .as("entityMongoDB"))
        .selectExpr("entityMongoDB.*").toDF(); 

此外,您的值中似乎有列名,您可以过滤掉该行。

使用
来自\u csv
,而不是
来自\u json
使用
来自\u csv
,而不是
来自\u json
Dataset<Row> dfs = df.select(from_csv(col("value"), EntityMongoDB.getStructType())
        .as("entityMongoDB"))
        .selectExpr("entityMongoDB.*").toDF(); 
Dataset<Row> dfs = df.select(split(col("value"), ",").as("values"))
        .select(IntStream.range(0, 7).map(i -> col("values").getItem(i)).toArray())
        .toDF("date", "value", "id", "title", "state", "frequency_short", "units_short", "seasonal_adjustment_short");