Created
September 15, 2017 07:32
-
-
Save ilovesoup/5cbdad449d2a67c6880352c8e9f8cf48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.spark.sql.TiContext | |
// 这里写PD的IP 和 README中一样 | |
val ti = new TiContext(spark, List("127.0.0.1:" + 2379)) | |
ti.tidbMapDatabase("TPCH") // 你要用的TiDB数据库 | |
// 对TiSpark使用的查询语句 | |
val tablToDump = spark.sql("select count(*) from part") | |
val jdbcUsername = "root" | |
val jdbcHostname = "localhost" | |
val jdbcPort = 4000 | |
val jdbcDatabase ="testcases" // 输出用的数据库名字 | |
val jdbcUrl = s"jdbc:mysql://${jdbcHostname}:${jdbcPort}/${jdbcDatabase}" | |
import java.util.Properties | |
val connectionProperties = new Properties() | |
connectionProperties.put("user", "root") | |
// 创建表并写入 | |
tablToDump.write.jdbc(jdbcUrl, "result", connectionProperties) | |
// 对现有表追加 | |
import org.apache.spark.sql.SaveMode | |
tablToDump.write.mode(SaveMode.Append).jdbc(jdbcUrl, "result", connectionProperties) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment