保存/导出数据框转换回JDBC / MySQL的 [英] Saving / exporting transformed DataFrame back to JDBC / MySQL

查看:2828
本文介绍了保存/导出数据框转换回JDBC / MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用新的 DataFrameWriter 来将数据写回JDBC数据库。我似乎无法找到这个任何文件,但查看源$ C ​​$ C现在看来似乎应该是可能的。

I'm trying to figure out how to use the new DataFrameWriter to write data back to a JDBC database. I can't seem to find any documentation for this, although looking at the source code it seems like it should be possible.

的一个简单的例子是我想要这样的容貌:

A trivial example of what I'm trying looks like this:

sqlContext.read.format("jdbc").options(Map(
  "url" -> "jdbc:mysql://localhost/foo", "dbtable" -> "foo.bar")
).select("some_column", "another_column")
.write.format("jdbc").options(Map(
  "url" -> "jdbc:mysql://localhost/foo", "dbtable" -> "foo.bar2")
).save("foo.bar2")

这不工作 - 我结束了这个错误:

This doesn't work — I end up with this error:

java.lang.RuntimeException: org.apache.spark.sql.execution.datasources.jdbc.DefaultSource does not allow create table as select.
    at scala.sys.package$.error(package.scala:27)
    at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.apply(ResolvedDataSource.scala:200)

我不知道如果我做错了什么(为什么它解析为DefaultSource而不是JDBCRDD例如?),或者如果写入现有的MySQL数据库仅仅是不可能的使用星火的DataFrames API。

I'm not sure if I'm doing something wrong (why is it resolving to DefaultSource instead of JDBCRDD for example?) or if writing to an existing MySQL database just isn't possible using Spark's DataFrames API.

推荐答案

有可能写入现有表,但它看起来像在这一刻(星火1.5.0)使用JDBC数据源不支持尚未创建表* 。您可以检查 SPARK-7646 以供参考。

It is possible to write to an existing table but it looks like at this moment (Spark 1.5.0) creating table using JDBC data source is not supported yet*. You can check SPARK-7646 for reference.

如果表中已经存在,你可以简单地使用 DataFrameWriter.jdbc 方法:

If table already exists you can simply use DataFrameWriter.jdbc method:

val prop: java.util.Properties = ???
df.write.jdbc("jdbc:mysql://localhost/foo", "foo.bar2", prop)

*什么是有趣的PySpark似乎使用 JDBC 方法来支持创建表。

这篇关于保存/导出数据框转换回JDBC / MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆