无法使用 spark shell 在 hdfs 中创建镶木地板文件

Not able to create parquet files in hdfs using spark shell

我想在 hdfs 中创建 parquet 文件,然后通过 hive 作为外部读取它 table。在编写镶木地板文件时,我对 spark-shell 中的阶段失败感到震惊。

星火版本:1.5.2 斯卡拉版本:2.10.4 Java: 1.7

输入文件:(employee.txt)

1201,萨蒂什,25
1202,克里希纳,28
1203,阿米斯,39
1204,贾韦德,23
1205,普鲁德维,23

在 Spark-Shell:

val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
val employee = sc.textFile("employee.txt")
employee.first()
val schemaString = "id name age"
import org.apache.spark.sql.Row;
import org.apache.spark.sql.types.{StructType, StructField, StringType};
val schema = StructType(schemaString.split(" ").map(fieldName ⇒ StructField(fieldName, StringType, true)))
val rowRDD = employee.map(_.split(",")).map(e ⇒ Row(e(0).trim.toInt, e(1), e(2).trim.toInt))
val employeeDF = sqlContext.createDataFrame(rowRDD, schema)
val finalDF = employeeDF.toDF();
sqlContext.setConf("spark.sql.parquet.compression.codec", "snappy")
var WriteParquet= finalDF.write.parquet("/user/myname/schemaParquet")

当我输入最后一条命令时,

ERROR

SPARK APPLICATION MANAGER

我什至尝试增加执行程序内存,但仍然失败。 同样重要的是,finalDF.show() 产生了同样的错误。 所以,我认为我在这里犯了一个逻辑错误。

感谢支持

这里的问题是您正在创建所有 fields/columns 类型默认为 StringType 的架构。但是在架构中传递值时,IdAge 的值正在根据 code.Hence 转换为整数,抛出运行ning.

时的匹配错误

架构中列的数据类型应与传递给它的值的数据类型相匹配。试试下面的代码。

val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
val employee = sc.textFile("employee.txt")
employee.first()
//val schemaString = "id name age"
import org.apache.spark.sql.Row;
import org.apache.spark.sql.types._;
val schema = StructType(StructField("id", IntegerType, true) :: StructField("name", StringType, true) :: StructField("age", IntegerType, true) :: Nil)
val rowRDD = employee.map(_.split(" ")).map(e ⇒ Row(e(0).trim.toInt, e(1), e(2).trim.toInt))
val employeeDF = sqlContext.createDataFrame(rowRDD, schema)
val finalDF = employeeDF.toDF();
sqlContext.setConf("spark.sql.parquet.compression.codec", "snappy")
var WriteParquet= finalDF.write.parquet("/user/myname/schemaParquet")

此代码应该 运行 没问题。