【问题标题】:spark dataframe is loading all nulls from csv file火花数据框正在从 csv 文件加载所有空值
【发布时间】:2020-02-22 20:32:43
【问题描述】:

我有一个包含以下数据的文件

####$ cat products.csv 
1,tv,sony,hd,699
2,tv,sony,uhd,799
3,tv,samsung,hd,599
4,tv,samsung,uhd,799
5,phone,iphone,x,999
6,phone,iphone,11,999
7,phone,samsung,10,899
8,phone,samsung,10note,999
9,phone,pixel,4,799
10,phone,pixel,3,699

我试图将它加载到 spark 数据框中,它没有给我任何错误,但它正在加载所有空值。

scala> val productSchema = StructType((Array(StructField("productId",IntegerType,true),StructField("productType",IntegerType,true),StructField("company",IntegerType,true),StructField("model",IntegerType,true),StructField("price",IntegerType,true))))
productSchema: org.apache.spark.sql.types.StructType = StructType(StructField(productId,IntegerType,true), StructField(productType,IntegerType,true), StructField(company,IntegerType,true), StructField(model,IntegerType,true), StructField(price,IntegerType,true))

scala> val df = spark.read.format("csv").option("header", "false").schema(productSchema).load("/path/products_js/products.csv")
df: org.apache.spark.sql.DataFrame = [productId: int, productType: int ... 3 more fields]

scala> df.show
+---------+-----------+-------+-----+-----+
|productId|productType|company|model|price|
+---------+-----------+-------+-----+-----+
|     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|
|     null|       null|   null| null| null|
|     null|       null|   null| null| null|
+---------+-----------+-------+-----+-----+

现在我尝试了一种不同的方式来加载数据,它成功了

scala> val temp = spark.read.csv("/path/products_js/products.csv")
temp: org.apache.spark.sql.DataFrame = [_c0: string, _c1: string ... 3 more fields]

scala> temp.show
+---+-----+-------+------+---+
|_c0|  _c1|    _c2|   _c3|_c4|
+---+-----+-------+------+---+
|  1|   tv|   sony|    hd|699|
|  2|   tv|   sony|   uhd|799|
|  3|   tv|samsung|    hd|599|
|  4|   tv|samsung|   uhd|799|
|  5|phone| iphone|     x|999|
|  6|phone| iphone|    11|999|
|  7|phone|samsung|    10|899|
|  8|phone|samsung|10note|999|
|  9|phone|  pixel|     4|799|
| 10|phone|  pixel|     3|699|
+---+-----+-------+------+---+

在第二种方法中,它加载了数据,但我无法将方案添加到数据框。两种加载数据的方法有什么区别,为什么第一种方法加载 null ?谁能帮帮我

【问题讨论】:

    标签: scala csv dataframe apache-spark


    【解决方案1】:

    你先把列的字符串类型定义为整数类型是错误的。这是有效的,

    import org.apache.spark.sql.types.{StructType, IntegerType, StringType}
    
    val productSchema = new StructType()
                            .add("productId", "int")
                            .add("productType", "string")
                            .add("company", "string")
                            .add("model", "string")
                            .add("price", "int")
    
    val df = spark.read.format("csv")
                .option("header", "false")
                .schema(productSchema)
                .load("test.csv")
    
    df.show()
    

    结果是

    +---------+-----------+-------+------+-----+
    |productId|productType|company| model|price|
    +---------+-----------+-------+------+-----+
    |        1|         tv|   sony|    hd|  699|
    |        2|         tv|   sony|   uhd|  799|
    |        3|         tv|samsung|    hd|  599|
    |        4|         tv|samsung|   uhd|  799|
    |        5|      phone| iphone|     x|  999|
    |        6|      phone| iphone|    11|  999|
    |        7|      phone|samsung|    10|  899|
    |        8|      phone|samsung|10note|  999|
    |        9|      phone|  pixel|     4|  799|
    |       10|      phone|  pixel|     3|  699|
    +---------+-----------+-------+------+-----+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多