代碼以下:java
val conf = new SparkConf().setAppName("testMysqlToHiveJdbc") .setMaster("local") val spark = SparkSession.builder() .config(conf) .enableHiveSupport() .getOrCreate() ////定義Propertites,肯定連接MySQL的參數 val mysqlProperties = new java.util.Properties() //MySQL的jdbc連接 val mysqlConnectionUrl = "jdbc:mysql://localhost:3306/rest" //定義檢索語句,用於MySQL連接 val mysqlTableName = """(select t.*, case when id<4000000 and id >=0 then 1 when id<8000000 and id >=4000000 then 2 when id<12000000 and id >=8000000 then 3 when id<16000000 and id >=12000000 then 4 when id<20000000 and id >=16000000 then 5 else 6 end par from usppa_twitter_data t) tt""" // val mysqlTableName = "usppa_twitter_data" mysqlProperties.put("driver","com.mysql.jdbc.Driver") //肯定driver mysqlProperties.put("user","root") //用戶名 mysqlProperties.put("password","1234") //密碼 mysqlProperties.put("fetchsize","10000") //批次取數數量 mysqlProperties.put("lowerBound","1") //肯定分區 mysqlProperties.put("upperBound","7") //肯定分區 mysqlProperties.put("numPartitions","6") //分區數量 mysqlProperties.put("partitionColumn","par") //分區字段 //讀取數據 val re = spark.read.jdbc(mysqlConnectionUrl, mysqlTableName,mysqlProperties) //寫入Hive表中 re.toDF().write.mode("overwrite").saveAsTable("testwarehouse.testtt")
代碼中,lowerbound和upperbound有兩種狀況須要考慮。mysql
1) 分區字段值能夠窮舉出來,如年份。sql
引用外網:https://www.percona.com/blog/2016/08/17/apache-spark-makes-slow-mysql-queries-10x-faster/apache
以下,lowerbound和upperbound會按照年份進行數據分區,這裏的分區指的是並行的executors。ide
val jdbcDF = spark.read.format("jdbc").options( | Map("url" -> "jdbc:mysql://localhost:3306/ontime?user=root&password=mysql", | "dbtable" -> "ontime.ontime_sm", | "fetchSize" -> "10000", | "partitionColumn" -> "yeard", "lowerBound" -> "1988", "upperBound" -> "2015", "numPartitions" -> "48" | )).load()
CREATE OR REPLACE TEMPORARY VIEW ontime USING org.apache.spark.sql.jdbc OPTIONS ( url "jdbc:mysql://127.0.0.1:3306/ontime?user=root&password=", dbtable "ontime.ontime", fetchSize "1000", partitionColumn "id", lowerBound "1", upperBound "162668934", numPartitions "128" );