示例
从Mysql同步数据到Hive,再从Hive同步数据到Vertica
一、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>SparkSyncTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<hadoop.version>2.6.0-cdh5.4.0</hadoop.version>
<guava.version>19.0</guava.version>
<scala.version.long>2.11.8</scala.version.long>
<scala.version>2.11</scala.version>
<spark.version>2.2.0</spark.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version.long}</version>
</dependency>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>compile-scala</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile-scala</id>
<phase>test-compile</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.11.4</scalaVersion>
</configuration>
</plugin>
</plugins>
<defaultGoal>compile</defaultGoal>
</build>
</project>
二、Mysql2Hive
首先将hive-site.xml加入resources目录中。
package com
import org.apache.spark.sql.SparkSession
object SyncMysqlToHive {
def main(args: Array[String]): Unit = {
val session = SparkSession.builder()
.appName(this.getClass.getSimpleName)
.master("local")
.enableHiveSupport()
.getOrCreate()
val url = "jdbc:mysql://01.mysql.hl.com:3306/?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull"
val sql = "(select id,job_id,hour,name,table_name,job_type,status,job,created_on,started_on,completed_on,data_source from dmf.running_jobs " +
"where hour = '2021-05-29 02:00:00') as running_jobs"
val prop = scala.collection.mutable.Map[String, String]()
prop.put("user", "root")
prop.put("password", "mysql")
prop.put("driver", "com.mysql.jdbc.Driver")
// dbtable如果为表名,则读取表内所有数据
prop.put("dbtable", sql)
prop.put("url", url)
// 创建视图前先cache可以提升效率
val df = session.read.format("jdbc").options(prop).load().cache()
df.createOrReplaceTempView("running_jobs")
println("------------------- 查询mysql中的数据 -------------------")
df.show()
println("------------------- 插入数据到Hive -------------------")
val hsql =
"""
|insert into test.running_jobs select * from running_jobs
|""".stripMargin
session.sql(hsql)
}
}
三、Hive2Vertica
package com
import org.apache.spark.sql.SparkSession
import java.util.Properties
object SyncHiveToVertica {
def main(args: Array[String]): Unit = {
val session = SparkSession.builder()
.appName(this.getClass.getSimpleName)
.master("local")
.enableHiveSupport()
.getOrCreate()
val hsql = "select * from test.running_jobs"
val df = session.sql(hsql).cache()
df.createOrReplaceTempView("running_jobs")
println("------------------- 查询Hive中的数据 -------------------")
df.show()
// vertica配置
val url = "jdbc:vertica://vertica.hl.com:5433/vertica"
val prop = new Properties()
prop.put("user", "mydba")
prop.put("password", "mydba")
prop.put("driver", "com.vertica.jdbc.Driver")
println("------------------- 插入数据到Vertica -------------------")
val vsql =
"""
|select * from running_jobs
|""".stripMargin
session.sql(vsql).write.mode("append").jdbc(url, "report.running_jobs", prop)
}
}