SparkSQL 简单测试

其中注册表的方法都在另一篇记配置文件的文章中所示的代码里

/**
  * Created by Draven on 2017/11/1.
  *  [email protected]
  */
object CassandraSqlExample extends App {
  val ss = getSparkSession

  import ss.implicits._

  ss.read.format(CASSANDRA_FORMAT).options(cassandraOptions("qiandw_test", "loan_program"))
    .load().filter($"funding_target" > 20000000).createOrReplaceTempView("loan_program")
  ss.sql("select * from loan_program").show()
}

/**
  * 从sql语句构建
  */
object CassandraSqlTextExample extends App {
  val ss = getSparkSession

  import ss.implicits._

  ss.read.format(CASSANDRA_FORMAT)
    .options(cassandraOptions("qiandw_test", "cash_account"))
    .load()
    .filter($"balance" > 0).createOrReplaceTempView("cash_account")
  ss.sql("select * from cash_account").orderBy("balance").show()
}

/**
  * 连表查询
  */
object JoinQueryExample extends App {
  val ss = getSparkSession

  import ss.implicits._

  ss.read.format(CASSANDRA_FORMAT)
    .options(cassandraOptions("qiandw_test", "cash_account"))
    .load()
    .createOrReplaceTempView("cash_account")

  ss.read.format(CASSANDRA_FORMAT)
    .options(cassandraOptions("qiandw_test", "cash_account_detail"))
    .load()
    .createOrReplaceTempView("cash_account_detail")

  ss.sql("select * from cash_account a left join cash_account_detail b on a.acct_id = b.acct ").show(5)
}

object DifferentDataBaseJoinQueryExample extends App {
  registerCassandraTables
  registerMySqlTables
  //  ss.read.format("jdbc")
  //    .options(Map("url" -> "jdbc:mysql://localhost:3306/huangjinxin",
  //      "driver" -> "com.mysql.jdbc.Driver",
  //      "dbtable" -> "t_user",
  //      "user" -> "root",
  //      "password" -> "root")).load().createOrReplaceTempView("t_user")
  //
  //  ss.read.format(CASSANDRA_FORMAT)
  //    .options(cassandraOptions("qiandw_test", "cash_account"))
  //    .load()
  //    .createOrReplaceTempView("cash_account")

  getSparkSession.sql("select * from cash_account a right join t_user b on a.state = b.age ").show()
}

你可能感兴趣的:(Spark)