小编unk*_*own的帖子

将镶木地板文件存储到PostgreSQL数据库中

我想将镶木地板文件写入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 …
Run Code Online (Sandbox Code Playgroud)

postgresql jdbc apache-spark parquet pyspark

1
推荐指数
1
解决办法
2621
查看次数

标签 统计

apache-spark ×1

jdbc ×1

parquet ×1

postgresql ×1

pyspark ×1