记录解决java spark读取python保存的csv文件报错

文章描述了一次在使用Spark处理CSV文件时遇到的问题,即尝试访问不存在的字段Value引发的IllegalArgumentException。原因是没有指定CSV文件有标题行。解决方案是添加header选项,设定为true,以使Spark解析第一行作为列名。
摘要由CSDN通过智能技术生成

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值