unk*_*own 1 postgresql jdbc apache-spark parquet pyspark
我想将镶木地板文件写入PostgreSQL。我正在使用Spark并使用Spark Dataframe的write.jdbc函数写入文件。对于镶木地板列类型,如长,十进制或文本,一切正常。问题出在诸如Map之类的复杂类型上。我想将Map作为json存储在我的PostgreSQL中。由于我知道PostgreSQL可以将文本数据类型自动转换为json(使用强制转换操作),因此我将map转储为json字符串。
但是spark程序抱怨我们试图将“字符变化”数据类型插入“ json”类型的列中。这清楚表明PostgreSQL不会自动将“字符变化”转换为JSON。
我继续并登录到数据库,然后手动尝试将JSON字符串插入到表的JSON数据类型列中,并且它可以正常工作。
我的问题是为什么我的spark程序抱怨强制转换操作?
我正在使用Spark版本1.6.1,PostgreSQL 4.3和JDBC 42.1.1
这是代码片段
url = "jdbc:postgresql://host_name:host_port/db_name"
data_frame.write.jdbc(url, table_name, properties={"user": user, "password": password})
Run Code Online (Sandbox Code Playgroud)
错误堆栈跟踪:
Hint: You will need to rewrite or cast the expression.
Position: 66 Call getNextException to see other errors in the batch.
at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:148)
at org.postgresql.core.ResultHandlerDelegate.handleError(ResultHandlerDelegate.java:50)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2190)
at org.postgresql.core.v3.QueryExecutorImpl.flushIfDeadlockRisk(QueryExecutorImpl.java:1325)
at org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:1350)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:458)
at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:791)
at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1547)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.savePartition(JdbcUtils.scala:215)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$saveTable$1.apply(JdbcUtils.scala:277)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$saveTable$1.apply(JdbcUtils.scala:276)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$33.apply(RDD.scala:920)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$33.apply(RDD.scala:920)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
... 1 more
Caused by: org.postgresql.util.PSQLException: ERROR: column "value" is of type json but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 66
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2476)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2189)
... 18 more
Run Code Online (Sandbox Code Playgroud)
已经很晚了,但是这是所有迷失灵魂的答案。
您需要将“ stringtype”参数传递给JDBC。它指定在绑定通过setString()设置的PreparedStatement参数时要使用的类型。默认情况下,它是varchar,它强制将参数设置为varchar并阻止任何强制转换操作(在我的情况下是JSON字符串到JSON)。如果指定,则stringtype ==“ unspecified”,然后将其留给数据库来确定参数是哪种类型。就我而言,这有助于Postgres将字符串轻松转换为JSON的方式。
说明文件:https : //jdbc.postgresql.org/documentation/head/connect.html