Creating a simple 1-row Spark DataFrame with Java API

前端 未结 2 1998
无人及你
无人及你 2021-02-04 14:54

In Scala, I can create a single-row DataFrame from an in-memory string like so:

val stringAsList = List(\"buzz\")
val df = sqlContext.sparkContext.parallelize(js         


        
2条回答
  •  隐瞒了意图╮
    2021-02-04 15:04

    You can achieve this by creating List to Rdd and than create Schema which will contain column name.

    There might be other ways as well, it's just one of them.

    List stringAsList = new ArrayList();
            stringAsList.add("buzz");
    
    JavaRDD rowRDD = sparkContext.parallelize(stringAsList).map((String row) -> {
                    return RowFactory.create(row);
                });
    
    StructType schema = DataTypes.createStructType(new StructField[] { DataTypes.createStructField("fizz", DataTypes.StringType, false) });
    
    DataFrame df = sqlContext.createDataFrame(rowRDD, schema).toDF();
    df.show();
    
    //+----+
    |fizz|
    +----+
    |buzz|
    

提交回复
热议问题