将 Spark 数据帧写入 postgres db 时出错

Posted

技术标签:

【中文标题】将 Spark 数据帧写入 postgres db 时出错【英文标题】:Error writing Spark dataframe to postgres db 【发布时间】:2018-03-13 22:31:45 【问题描述】:

我正在尝试将一些元数据从我的 Spark 作业写入 postgres 数据库。不断出现以下错误:

2018-03-13 15:23:40,599 root ERROR An error occurred while calling o1315.jdbc.
: scala.MatchError: null
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:62)
    at org.apache.spark.sql.execution.datasources.DataSource.write(DataSource.scala:472)
    at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:48)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:58)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:56)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:74)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:92)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:92)
    at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:610)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:233)
    at org.apache.spark.sql.DataFrameWriter.jdbc(DataFrameWriter.scala:461)
    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 py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)

我在 spark-conf.default 中提供以下驱动程序信息:

# Example:
# spark.master                     spark://master:7077
# spark.eventLog.enabled           true
# spark.eventLog.dir               hdfs://namenode:8021/directory
# spark.serializer                 org.apache.spark.serializer.KryoSerializer
# spark.driver.memory              5g
# spark.executor.extraJavaOptions  -XX:+PrintGCDetails -Dkey=value -Dnumbers="one two three"
spark.driver.extraClassPath /path/to/drivers/postgresql-9.4.1210.jre7.jar
spark.executor.extraClassPath /path/to/drivers/postgresql-9.4.1210.jre7.jar
spark.jars /path/to/drivers/postgresql-9.4.1210.jre7.jar

当前使用 Spark 2.2.0。知道要检查什么吗?

【问题讨论】:

【参考方案1】:

根据scala.matcherror和Null values in field generates MatchError,postgres jdbc库没有问题。它可能只需要检查列名即可找到空值。

【讨论】:

感谢您的回答。它只有一行,写入了 3 列,并且没有一个具有空值。所以我不知道这将如何解释。除非我错过了什么。

以上是关于将 Spark 数据帧写入 postgres db 时出错的主要内容,如果未能解决你的问题,请参考以下文章

将 spark 数据帧写入 postgres 数据库

如何将流式数据帧写入 PostgreSQL?

将大型 Spark 数据帧从数据块写入 csv 失败

将 Spark 数据帧写入带分区的 CSV

将 Spark 数据帧写入带分区的 CSV

如何将 Spark 数据帧写入 impala 数据库