SparkSQL簡易テスト

6650 ワード

レジストリのメソッドは、別のプロファイルの記事に示すコードに記載されています.
/**
  * 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()
}