csv文件内容如下
Value
/emr.s3.zlh.com/zlh/UserInfoBak/2023/20230418/zbook_20230418_094807.gz
一开始代码如下
Dataset<Row> jsonDs = sparkSession.read().option("inferSchema", true)
.format("csv")
.load(flagPath).filter((FilterFunction<Row>) Objects::nonNull);
运行后报错
23/04/18 10:40:36 ERROR yarn.ApplicationMaster: User class threw exception: java.lang.IllegalArgumentException: Field "Value" does not exist.
Available fields: _c0
java.lang.IllegalArgumentException: Field "Value" does not exist.
Available fields: _c0
at org.apache.spark.sql.types.StructType$$anonfun$fieldIndex$1.apply(StructType.scala:303)
at org.apache.spark.sql.types.StructType$$anonfun$fieldIndex$1.apply(StructType.scala:303)
at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
at scala.collection.AbstractMap.getOrElse(Map.scala:59)
at org.apache.spark.sql.types.StructType.fieldIndex(StructType.scala:302)
at org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema.fieldIndex(rows.scala:187)
at org.apache.spark.sql.Row$class.getAs(Row.scala:333)
at org.apache.spark.sql.catalyst.expressions.GenericRow.getAs(rows.scala:166)
at com.cqvip.jobstream.common.spark.userlog.UniqueUserInfo.main(UniqueUserInfo.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$2.run(ApplicationMaster.scala:685)
23/04/18 10:40:36 INFO yarn.ApplicationMaster: Final app status: FAILED, exitCode: 15, (reason: User class threw exception: java.lang.IllegalArgumentException: Field "Value" does not exist.
Available fields: _c0
at org.apache.spark.sql.types.StructType$$anonfun$fieldIndex$1.apply(StructType.scala:303)
at org.apache.spark.sql.types.StructType$$anonfun$fieldIndex$1.apply(StructType.scala:303)
at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
at scala.collection.AbstractMap.getOrElse(Map.scala:59)
at org.apache.spark.sql.types.StructType.fieldIndex(StructType.scala:302)
at org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema.fieldIndex(rows.scala:187)
at org.apache.spark.sql.Row$class.getAs(Row.scala:333)
at org.apache.spark.sql.catalyst.expressions.GenericRow.getAs(rows.scala:166)
at com.cqvip.jobstream.common.spark.userlog.UniqueUserInfo.main(UniqueUserInfo.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$2.run(ApplicationMaster.scala:685)
)
怀疑是分隔符问题,因为python指定了"\t",而spark默认是逗号,但是指定分隔符"\t"读取以及修改了python指定的分隔符为逗号都尝试了,作为只有一列的csv文件,显然不是分隔符的问题。
想到spark写出csv默认情况下,使用逗号作为列分隔符,并且没有标题行。所以应该是我读取的时候没有设置第一行为标题行导致,修改测试后果然是这样
修改后代码:
Dataset<Row> jsonDs = sparkSession.read()
.option("header", "true")
.option("inferSchema", "true")
.csv(flagPath).filter((FilterFunction<Row>) Objects::nonNull);