import org.apache.spark.sql.{SaveMode, SparkSession}
import org.apache.spark.sql.types.{FloatType, IntegerType, StringType, StructField, StructType}
object MysqlWrite {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()
.master("local[6]")
.appName("mysql write")
.getOrCreate()
//定义表约束信息
val schema = StructType(
List(
StructField("name", StringType),
StructField("age", IntegerType),
StructField("gpa", FloatType)
)
)
//读取数据集 注意:使用csv读取 如果不是,分割 需要指定分隔符即可
val readDF = spark.read.schema(schema).option("delimiter", "\t").csv("data/studenttab10k")
import spark.implicits._
//etl处理
val resultDF = readDF.where('age < 30)
//落地
resultDF.write
.format("jdbc")
.mode(SaveMode.Overwrite)
.option("url", "jdbc:mysql://hadoop100:3306/spark_test")
.option("dbtable", "student10k")
.option("user", "root")
.option("password", "root")
.save()
spark.stop()
}
}