|
| 1 | +package com.redislabs.provider.redis.rdd |
| 2 | + |
| 3 | +import com.redislabs.provider.redis._ |
| 4 | +import org.apache.spark.sql.SQLContext |
| 5 | +import org.apache.spark.{SparkConf, SparkContext} |
| 6 | +import org.scalatest.{BeforeAndAfterAll, FunSuite, ShouldMatchers} |
| 7 | + |
| 8 | +class RedisSparkSQLStandaloneSuite extends FunSuite with ENV with BeforeAndAfterAll with ShouldMatchers { |
| 9 | + |
| 10 | + var sqlContext: SQLContext = null |
| 11 | + override def beforeAll() { |
| 12 | + super.beforeAll() |
| 13 | + |
| 14 | + sc = new SparkContext(new SparkConf() |
| 15 | + .setMaster("local").setAppName(getClass.getName) |
| 16 | + .set("redis.host", "127.0.0.1") |
| 17 | + .set("redis.port", "6379") |
| 18 | + .set("redis.auth", "passwd") |
| 19 | + ) |
| 20 | + redisConfig = new RedisConfig(new RedisEndpoint("127.0.0.1", 6379, "passwd")) |
| 21 | + |
| 22 | + // Flush all the hosts |
| 23 | + redisConfig.hosts.foreach( node => { |
| 24 | + val conn = node.connect |
| 25 | + conn.flushAll |
| 26 | + conn.close |
| 27 | + }) |
| 28 | + |
| 29 | + sqlContext = new SQLContext(sc) |
| 30 | + sqlContext.sql( s""" |
| 31 | + |CREATE TEMPORARY TABLE rl |
| 32 | + |(name STRING, score INT) |
| 33 | + |USING com.redislabs.provider.redis.sql |
| 34 | + |OPTIONS (table 'rl') |
| 35 | + """.stripMargin) |
| 36 | + |
| 37 | + (1 to 64).foreach{ |
| 38 | + index => { |
| 39 | + sqlContext.sql(s"insert overwrite table rl select t.* from (select 'rl${index}', ${index}) t") |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + test("RedisKVRDD - default(cluster)") { |
| 45 | + val df = sqlContext.sql( |
| 46 | + s""" |
| 47 | + |SELECT * |
| 48 | + |FROM rl |
| 49 | + """.stripMargin) |
| 50 | + df.filter(df("score") > 10).count should be (54) |
| 51 | + df.filter(df("score") > 10 and df("score") < 20).count should be (9) |
| 52 | + } |
| 53 | + |
| 54 | + test("RedisKVRDD - cluster") { |
| 55 | + implicit val c: RedisConfig = redisConfig |
| 56 | + val df = sqlContext.sql( |
| 57 | + s""" |
| 58 | + |SELECT * |
| 59 | + |FROM rl |
| 60 | + """.stripMargin) |
| 61 | + df.filter(df("score") > 10).count should be (54) |
| 62 | + df.filter(df("score") > 10 and df("score") < 20).count should be (9) |
| 63 | + } |
| 64 | + |
| 65 | + override def afterAll(): Unit = { |
| 66 | + sc.stop |
| 67 | + System.clearProperty("spark.driver.port") |
| 68 | + } |
| 69 | +} |
| 70 | + |
0 commit comments