Spark教程
作者: 时海 风自在
Spark链接Mysql

1、


package com.k6k4

import java.util.Properties

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

/**
 * Created by k6k4 on 16/12/13.
 */
object MysqlTest {
  def main(args: Array[String]) {

    val sparkConf = new SparkConf().setAppName("MysqlTest").
      setMaster("local[2]")

    val spark = SparkSession
      .builder()
      .config(sparkConf)
      .getOrCreate();


    val prop = new Properties;
    prop.setProperty("user", "root");
    prop.setProperty("password", "root")

    val table = spark.read.jdbc("jdbc:mysql://localhost:3306/dbName", "tableName", prop)



    val custinfo = table.
      select("name", "age")
      .where("id=1");

    val rows = custinfo.collect();

    rows.foreach(row => {
      println(row.getString(0) + "\t" + row.getLong(1))
    }
    )

    spark.stop();

  }

}


2、

标签: val、prop、sparkconf、spark、mysqltest
一个创业中的苦逼程序员
  • 回复
隐藏