在Pyspark中使用JDBC插入Citus散列分布表

问题描述:

我试图在我使用 Citus创建的散列分布表中插入一些数据。在Pyspark中,我使用JDBC postgres驱动器将数据插入到表中。 代码,我使用的数据插入到在pyspark使用JDBC表是:在Pyspark中使用JDBC插入Citus散列分布表

url = "jdbc:postgresql://host:port/db_name?rewriteBatchedStatements=true" 
    properties = {"user":"user_name","password":"password"} 
    df = spark.createDataFrame(rdd_name) 
    df.write.jdbc(url=url, table="table_name", mode="append", properties=properties) 

在此设置下,我得到这个错误:

: org.postgresql.util.PSQLException: ERROR: relation "table_name" already exists 
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) 
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155) 
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288) 
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430) 
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356) 
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:303) 
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289) 
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266) 
at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:246) 
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.createTable(JdbcUtils.scala:692) 
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:89) 
at org.apache.spark.sql.execution.datasources.DataSource.write(DataSource.scala:426) 
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:215) 
at org.apache.spark.sql.DataFrameWriter.jdbc(DataFrameWriter.scala:446) 
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:497) 
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) 

能有人帮我我怎么插入数据在pyspark中使用jdbc进入citus postgres?提前致谢。

+0

您使用哪种spark版本? – Yaron

您的错误消息说“table_name”已经存在。很可能,您的代码或框架试图再次创建相同的表。你能检查一下吗?