redis.clients.jedis.Jedis Scala Examples
The following examples show how to use redis.clients.jedis.Jedis.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ProtobufStorage.scala From gbf-raidfinder with MIT License | 5 votes |
package walfie.gbf.raidfinder.server.persistence import com.trueaccord.scalapb.{GeneratedMessage, GeneratedMessageCompanion, Message} import java.net.URI import redis.clients.jedis.{BinaryJedis, Jedis} trait ProtobufStorage { type CacheItem[T] = GeneratedMessage with Message[T] def set[T <: CacheItem[T]](key: String, value: T): Unit def get[T <: CacheItem[T]]( key: String )(implicit companion: GeneratedMessageCompanion[T]): Option[T] def close(): Unit } object ProtobufStorage { def redis(uri: URI): RedisProtobufStorage = { new RedisProtobufStorage(new BinaryJedis(uri)) } } // TODO: Write integration test class RedisProtobufStorage(redis: BinaryJedis) extends ProtobufStorage { def set[T <: CacheItem[T]](key: String, value: T): Unit = { redis.set(key.getBytes, value.toByteArray) } def get[T <: CacheItem[T]]( key: String )(implicit companion: GeneratedMessageCompanion[T]): Option[T] = { Option(redis.get(key.getBytes)).flatMap { bytes => companion.validate(bytes).toOption } } def close(): Unit = redis.close() } object NoOpProtobufStorage extends ProtobufStorage { def set[T <: CacheItem[T]](key: String, value: T): Unit = () def get[T <: CacheItem[T]]( key: String )(implicit companion: GeneratedMessageCompanion[T]): Option[T] = None def close(): Unit = () }
Example 2
Source File: WithRedis.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.counter.helper import com.typesafe.config.Config import org.apache.s2graph.counter.config.S2CounterConfig import org.apache.s2graph.counter.util.Hashes import org.slf4j.LoggerFactory import redis.clients.jedis.exceptions.JedisException import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig} import scala.util.Try class WithRedis(config: Config) { lazy val s2config = new S2CounterConfig(config) private val log = LoggerFactory.getLogger(getClass) val poolConfig = new JedisPoolConfig() poolConfig.setMaxTotal(150) poolConfig.setMaxIdle(50) poolConfig.setMaxWaitMillis(200) val jedisPools = s2config.REDIS_INSTANCES.map { case (host, port) => new JedisPool(poolConfig, host, port) } def getBucketIdx(key: String): Int = { Hashes.murmur3(key) % jedisPools.size } def doBlockWithIndex[T](idx: Int)(f: Jedis => T): Try[T] = { Try { val pool = jedisPools(idx) var jedis: Jedis = null try { jedis = pool.getResource f(jedis) } catch { case e: JedisException => pool.returnBrokenResource(jedis) jedis = null throw e } finally { if (jedis != null) { pool.returnResource(jedis) } } } } def doBlockWithKey[T](key: String)(f: Jedis => T): Try[T] = { doBlockWithIndex(getBucketIdx(key))(f) } }
Example 3
Source File: ConnectionPool.scala From spark-redis with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.redislabs.provider.redis import redis.clients.jedis.{JedisPoolConfig, Jedis, JedisPool} import redis.clients.jedis.exceptions.JedisConnectionException import java.util.concurrent.ConcurrentHashMap import scala.collection.JavaConversions._ object ConnectionPool { @transient private lazy val pools: ConcurrentHashMap[RedisEndpoint, JedisPool] = new ConcurrentHashMap[RedisEndpoint, JedisPool]() def connect(re: RedisEndpoint): Jedis = { val pool = pools.getOrElseUpdate(re, { val poolConfig: JedisPoolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(250) poolConfig.setMaxIdle(32) poolConfig.setTestOnBorrow(false) poolConfig.setTestOnReturn(false) poolConfig.setTestWhileIdle(false) poolConfig.setMinEvictableIdleTimeMillis(60000) poolConfig.setTimeBetweenEvictionRunsMillis(30000) poolConfig.setNumTestsPerEvictionRun(-1) new JedisPool(poolConfig, re.host, re.port, re.timeout, re.auth, re.dbNum, re.ssl) } ) var sleepTime: Int = 4 var conn: Jedis = null while (conn == null) { try { conn = pool.getResource } catch { case e: JedisConnectionException if e.getCause.toString. contains("ERR max number of clients reached") => { if (sleepTime < 500) sleepTime *= 2 Thread.sleep(sleepTime) } case e: Exception => throw e } } conn } }
Example 4
Source File: StreamUtils.scala From spark-redis with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.redislabs.provider.redis.util import org.apache.commons.lang3.StringUtils import redis.clients.jedis.{StreamEntryID, Jedis} object StreamUtils extends Logging { val EntryIdEarliest = new StreamEntryID(0, 0) def createConsumerGroupIfNotExist(conn: Jedis, streamKey: String, groupName: String, offset: StreamEntryID): Unit = { try { conn.xgroupCreate(streamKey, groupName, offset, true) } catch { case e: Exception if StringUtils.contains(e.getMessage, "already exists") => logInfo(s"Consumer group already exists: $groupName") } } def resetConsumerGroup(conn: Jedis, streamKey: String, groupName: String, offset: StreamEntryID): Unit = { logInfo(s"Setting consumer group $groupName id to $offset") conn.xgroupSetID(streamKey, groupName, offset) } }
Example 5
Source File: ConnectionUtils.scala From spark-redis with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.redislabs.provider.redis.util import java.util.{List => JList} import com.redislabs.provider.redis.RedisConfig import com.redislabs.provider.redis.util.ConnectionUtils.XINFO.{SubCommandGroups, SubCommandStream} import redis.clients.jedis.Jedis import redis.clients.jedis.commands.ProtocolCommand import redis.clients.jedis.util.SafeEncoder import scala.collection.JavaConverters._ object ConnectionUtils { def withConnection[A](conn: Jedis)(body: Jedis => A): A = { try { body(conn) } finally { conn.close() } } def withConnection[A](streamKey: String)(body: Jedis => A)(implicit redisConfig: RedisConfig): A = { withConnection(redisConfig.connectionForKey(streamKey)){ body } } implicit class JedisExt(val jedis: Jedis) extends AnyVal { //TODO: temporary solution to get latest offset while not supported by Jedis def xinfo(command: String, args: String*): Map[String, Any] = { val client = jedis.getClient val combinedArgs = command +: args client.sendCommand(XINFO, combinedArgs: _*) val response = asList(client.getOne).asScala command match { case SubCommandStream => asMap(response) case SubCommandGroups => response.map(m => asList(m)).map(_.asScala).map(asMap) .map(m => String.valueOf(m("name")) -> m).toMap } } private def asMap(seq: Seq[Any]): Map[String, Any] = { seq.grouped(2) .map { group => val key = asString(group.head) val value = group(1) match { case arr: Array[Byte] => asString(arr) case other: Any => other } key -> value }.toMap } private def asList(any: Any): JList[Any] = any.asInstanceOf[JList[Any]] private def asString(any: Any): String = new String(any.asInstanceOf[Array[Byte]]) } object XINFO extends ProtocolCommand { val SubCommandStream = "STREAM" val SubCommandGroups = "GROUPS" val LastGeneratedId = "last-generated-id" val LastDeliveredId = "last-delivered-id" val LastEntry = "last-entry" val EntryId = "_id" override def getRaw: Array[Byte] = SafeEncoder.encode("XINFO") } }
Example 6
Source File: PipelineUtils.scala From spark-redis with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.redislabs.provider.redis.util import java.util.{List => JList} import com.redislabs.provider.redis.ReadWriteConfig import redis.clients.jedis.{Jedis, Pipeline} import scala.collection.JavaConverters._ import scala.collection.{TraversableOnce, mutable} object PipelineUtils { def foreachWithPipelineNoLastSync[A](conn: Jedis, items: TraversableOnce[A])(f: (Pipeline, A) => Unit) (implicit readWriteConfig: ReadWriteConfig): Pipeline = { // iterate over items and create new pipelines periodically var i = 0 var pipeline = conn.pipelined() for (x <- items) { f(pipeline, x) i = i + 1 if (i % readWriteConfig.maxPipelineSize == 0) { pipeline.sync() pipeline = conn.pipelined() } } // return pipeline, the client should sync pipeline pipeline } }
Example 7
Source File: ConnectionPool.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.execution.datasources.redis import java.util.concurrent.ConcurrentHashMap import scala.collection.JavaConverters._ import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig} import redis.clients.jedis.exceptions.JedisConnectionException object ConnectionPool { @transient private lazy val pools: ConcurrentHashMap[RedisEndpoint, JedisPool] = new ConcurrentHashMap[RedisEndpoint, JedisPool]() def connect(re: RedisEndpoint): Jedis = { val pool = pools.asScala.getOrElseUpdate(re, { val poolConfig: JedisPoolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(250) poolConfig.setMaxIdle(32) poolConfig.setTestOnBorrow(false) poolConfig.setTestOnReturn(false) poolConfig.setTestWhileIdle(false) poolConfig.setMinEvictableIdleTimeMillis(60000) poolConfig.setTimeBetweenEvictionRunsMillis(30000) poolConfig.setNumTestsPerEvictionRun(-1) new JedisPool(poolConfig, re.host, re.port, re.timeout, re.auth, re.dbNum) }) var sleepTime: Int = 4 var conn: Jedis = null while (conn == null) { try { conn = pool.getResource } catch { case e: JedisConnectionException if e.getCause.toString.contains("ERR max number of clients reached") => { if (sleepTime < 500) sleepTime *= 2 Thread.sleep(sleepTime) } case e: Exception => throw e } } conn } }
Example 8
Source File: RedisNode.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.execution.datasources.redis import redis.clients.jedis.Jedis case class RedisNode( val endpoint: RedisEndpoint, val startSlot: Int, val endSlot: Int, val idx: Int, val total: Int) { def connect(): Jedis = { endpoint.connect() } }
Example 9
Source File: L6-26Redis.scala From prosparkstreaming with Apache License 2.0 | 5 votes |
package org.apress.prospark import scala.collection.JavaConversions.asScalaBuffer import scala.collection.JavaConversions.mutableMapAsJavaMap import scala.collection.mutable import org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.streaming.Seconds import org.apache.spark.streaming.StreamingContext import org.json4s.DefaultFormats import org.json4s.jvalue2extractable import org.json4s.jvalue2monadic import org.json4s.native.JsonMethods.parse import org.json4s.string2JsonInput import redis.clients.jedis.Jedis object StatefulRedisApp { def main(args: Array[String]) { if (args.length != 3) { System.err.println( "Usage: StatefulRedisApp <appname> <checkpointDir> <hostname>") System.exit(1) } val Seq(appName, checkpointDir, hostname) = args.toSeq val conf = new SparkConf() .setAppName(appName) .setJars(SparkContext.jarOfClass(this.getClass).toSeq) val batchInterval = 10 val ssc = new StreamingContext(conf, Seconds(batchInterval)) HttpUtils.createStream(ssc, url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22IBM,GOOG,MSFT,AAPL,FB,ORCL,YHOO,TWTR,LNKD,INTC%22)%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env", interval = batchInterval) .flatMap(rec => { implicit val formats = DefaultFormats val query = parse(rec) \ "query" ((query \ "results" \ "quote").children) .map(rec => ((rec \ "symbol").extract[String], ((rec \ "LastTradePriceOnly").extract[String].toFloat, (rec \ "Volume").extract[String].toLong))) }) .foreachRDD(rdd => { rdd.foreachPartition({ part => val jedis = new Jedis(hostname) part.foreach(f => { val prev = jedis.hmget(f._1, "min", "max", "count") if (prev(0) == null) { jedis.hmset(f._1, mutable.HashMap("min" -> Long.MaxValue.toString, "max" -> Long.MinValue.toString, "count" -> 0.toString)) } else { val prevLong = prev.toList.map(v => v.toLong) var newCount = prevLong(2) val newPrice = f._2._1 val newVolume = f._2._2 if (newPrice > 500.0) { newCount += 1 } val newMin = if (newVolume < prevLong(0)) newVolume else prevLong(0) val newMax = if (newVolume > prevLong(1)) newVolume else prevLong(1) jedis.hmset(f._1, mutable.HashMap("min" -> newMin.toString, "max" -> newMax.toString, "count" -> newCount.toString)) } }) jedis.close() }) val jedis = new Jedis(hostname) jedis.scan(0).getResult.foreach(sym => println("Symbol: %s, Stats: %s".format(sym, jedis.hmget(sym, "min", "max", "count").toString))) jedis.close() }) ssc.start() ssc.awaitTermination() } }
Example 10
Source File: RedisHandler.scala From scrapy4s with GNU Lesser General Public License v3.0 | 5 votes |
package com.scrapy4s.scheduler.redis import redis.clients.jedis.{Jedis, JedisPool} class RedisHandler(pool: JedisPool) { def handler(function: (Jedis) => Boolean): Boolean = { var jedis: Jedis = null try { jedis = pool.getResource val result = function(jedis) jedis.close() result } finally { if (jedis != null) { jedis.close() } } } }
Example 11
Source File: RedisWriter.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.redis.sink.writer import java.io.{File, FileNotFoundException} import com.datamountaineer.streamreactor.connect.errors.ErrorHandler import com.datamountaineer.streamreactor.connect.redis.sink.config.RedisSinkSettings import com.datamountaineer.streamreactor.connect.schemas.ConverterUtil import com.datamountaineer.streamreactor.connect.sink._ import com.typesafe.scalalogging.StrictLogging import redis.clients.jedis.Jedis abstract class RedisWriter extends DbWriter with StrictLogging with ConverterUtil with ErrorHandler { var jedis: Jedis = _ def createClient(sinkSettings: RedisSinkSettings): Unit = { val connection = sinkSettings.connectionInfo if (connection.isSslConnection) { connection.keyStoreFilepath match { case Some(path) => if (!new File(path).exists) { throw new FileNotFoundException(s"Keystore not found in: $path") } System.setProperty("javax.net.ssl.keyStorePassword", connection.keyStorePassword.getOrElse("")) System.setProperty("javax.net.ssl.keyStore", path) System.setProperty("javax.net.ssl.keyStoreType", connection.keyStoreType.getOrElse("jceks")) case None => } connection.trustStoreFilepath match { case Some(path) => if (!new File(path).exists) { throw new FileNotFoundException(s"Truststore not found in: $path") } System.setProperty("javax.net.ssl.trustStorePassword", connection.trustStorePassword.getOrElse("")) System.setProperty("javax.net.ssl.trustStore", path) System.setProperty("javax.net.ssl.trustStoreType", connection.trustStoreType.getOrElse("jceks")) case None => } } jedis = new Jedis(connection.host, connection.port, connection.isSslConnection) connection.password.foreach(p => jedis.auth(p)) //initialize error tracker initialize(sinkSettings.taskRetries, sinkSettings.errorPolicy) } def close(): Unit = { if (jedis != null) { jedis.close() } } }
Example 12
Source File: RedisInsertSortedSetTest.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.redis.sink.writer import com.datamountaineer.streamreactor.connect.redis.sink.config.{RedisConfig, RedisConfigConstants, RedisConnectionInfo, RedisSinkSettings} import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct} import org.apache.kafka.connect.sink.SinkRecord import org.mockito.MockitoSugar import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import redis.clients.jedis.Jedis import redis.embedded.RedisServer import scala.collection.JavaConverters._ class RedisInsertSortedSetTest extends AnyWordSpec with Matchers with BeforeAndAfterAll with MockitoSugar { val redisServer = new RedisServer(6379) override def beforeAll() = redisServer.start() override def afterAll() = redisServer.stop() "Redis INSERT into Sorted Set (SS) writer" should { "write Kafka records to a Redis Sorted Set" in { val TOPIC = "cpuTopic" val KCQL = s"INSERT INTO cpu_stats SELECT * from $TOPIC STOREAS SortedSet(score=ts)" println("Testing KCQL : " + KCQL) val props = Map( RedisConfigConstants.REDIS_HOST->"localhost", RedisConfigConstants.REDIS_PORT->"6379", RedisConfigConstants.KCQL_CONFIG->KCQL ).asJava val config = RedisConfig(props) val connectionInfo = new RedisConnectionInfo("localhost", 6379, None) val settings = RedisSinkSettings(config) val writer = new RedisInsertSortedSet(settings) writer.createClient(settings) val schema = SchemaBuilder.struct().name("com.example.Cpu") .field("type", Schema.STRING_SCHEMA) .field("temperature", Schema.FLOAT64_SCHEMA) .field("voltage", Schema.FLOAT64_SCHEMA) .field("ts", Schema.INT64_SCHEMA).build() val struct1 = new Struct(schema).put("type", "Xeon").put("temperature", 60.4).put("voltage", 90.1).put("ts", 1482180657010L) val struct2 = new Struct(schema).put("type", "i7").put("temperature", 62.1).put("voltage", 103.3).put("ts", 1482180657020L) val struct3 = new Struct(schema).put("type", "i7-i").put("temperature", 64.5).put("voltage", 101.1).put("ts", 1482180657030L) val sinkRecord1 = new SinkRecord(TOPIC, 0, null, null, schema, struct1, 1) val sinkRecord2 = new SinkRecord(TOPIC, 0, null, null, schema, struct2, 2) val sinkRecord3 = new SinkRecord(TOPIC, 0, null, null, schema, struct3, 3) val jedis = new Jedis(connectionInfo.host, connectionInfo.port) // Clean up in-memory jedis jedis.flushAll() writer.write(Seq(sinkRecord1)) writer.write(Seq(sinkRecord2, sinkRecord3)) // Redis cardinality should now be 3 jedis.zcard("cpu_stats") shouldBe 3 val allSSrecords = jedis.zrange("cpu_stats", 0, 999999999999L) val results = allSSrecords.asScala.toList results.head shouldBe """{"type":"Xeon","temperature":60.4,"voltage":90.1,"ts":1482180657010}""" results(1) shouldBe """{"type":"i7","temperature":62.1,"voltage":103.3,"ts":1482180657020}""" results(2) shouldBe """{"type":"i7-i","temperature":64.5,"voltage":101.1,"ts":1482180657030}""" } } }
Example 13
Source File: RedisPubSubTest.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.redis.sink.writer import com.datamountaineer.streamreactor.connect.redis.sink.config.{RedisConfig, RedisConfigConstants, RedisConnectionInfo, RedisSinkSettings} import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct} import org.apache.kafka.connect.sink.SinkRecord import org.mockito.MockitoSugar import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import redis.clients.jedis.{Jedis, JedisPubSub} import redis.embedded.RedisServer import scala.collection.JavaConverters._ import scala.collection.mutable.ListBuffer class RedisPubSubTest extends AnyWordSpec with Matchers with BeforeAndAfterAll with MockitoSugar { val redisServer = new RedisServer(6379) override def beforeAll() = redisServer.start() override def afterAll() = redisServer.stop() "Redis PUBSUB writer" should { "write Kafka records to a Redis PubSub" in { val TOPIC = "cpuTopic" val KCQL = s"SELECT * from $TOPIC STOREAS PubSub (channel=type)" println("Testing KCQL : " + KCQL) val props = Map( RedisConfigConstants.REDIS_HOST->"localhost", RedisConfigConstants.REDIS_PORT->"6379", RedisConfigConstants.KCQL_CONFIG->KCQL ).asJava val config = RedisConfig(props) val connectionInfo = new RedisConnectionInfo("localhost", 6379, None) val settings = RedisSinkSettings(config) val writer = new RedisPubSub(settings) writer.createClient(settings) val schema = SchemaBuilder.struct().name("com.example.Cpu") .field("type", Schema.STRING_SCHEMA) .field("temperature", Schema.FLOAT64_SCHEMA) .field("voltage", Schema.FLOAT64_SCHEMA) .field("ts", Schema.INT64_SCHEMA).build() val struct1 = new Struct(schema).put("type", "Xeon").put("temperature", 60.4).put("voltage", 90.1).put("ts", 1482180657010L) val struct2 = new Struct(schema).put("type", "i7").put("temperature", 62.1).put("voltage", 103.3).put("ts", 1482180657020L) val struct3 = new Struct(schema).put("type", "i7-i").put("temperature", 64.5).put("voltage", 101.1).put("ts", 1482180657030L) val sinkRecord1 = new SinkRecord(TOPIC, 0, null, null, schema, struct1, 1) val sinkRecord2 = new SinkRecord(TOPIC, 0, null, null, schema, struct2, 2) val sinkRecord3 = new SinkRecord(TOPIC, 0, null, null, schema, struct3, 3) val jedis = new Jedis(connectionInfo.host, connectionInfo.port) // Clean up in-memory jedis jedis.flushAll() val messagesMap = collection.mutable.Map[String, ListBuffer[String]]() val t = new Thread { private val pubsub = new JedisPubSub { override def onMessage(channel: String, message: String): Unit = { messagesMap.get(channel) match { case Some(msgs) => messagesMap.put(channel, msgs += message) case None => messagesMap.put(channel, ListBuffer(message)) } } } override def run(): Unit = { jedis.subscribe(pubsub, "Xeon", "i7", "i7-i") } override def interrupt(): Unit = { pubsub.punsubscribe("*") super.interrupt() } } t.start() t.join(5000) if (t.isAlive) t.interrupt() writer.write(Seq(sinkRecord1)) writer.write(Seq(sinkRecord2, sinkRecord3)) messagesMap.size shouldBe 3 messagesMap("Xeon").head shouldBe """{"type":"Xeon","temperature":60.4,"voltage":90.1,"ts":1482180657010}""" messagesMap("i7").head shouldBe """{"type":"i7","temperature":62.1,"voltage":103.3,"ts":1482180657020}""" messagesMap("i7-i").head shouldBe """{"type":"i7-i","temperature":64.5,"voltage":101.1,"ts":1482180657030}""" } } }
Example 14
Source File: RedisStreamTest.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.redis.sink.writer /* * Copyright 2017 Datamountaineer. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util import com.datamountaineer.streamreactor.connect.redis.sink.RedisSinkTask import com.datamountaineer.streamreactor.connect.redis.sink.config.{RedisConfig, RedisConfigConstants, RedisConnectionInfo, RedisSinkSettings} import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct} import org.apache.kafka.connect.sink.SinkRecord import org.mockito.MockitoSugar import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import redis.clients.jedis.{Jedis, StreamEntryID} import scala.collection.JavaConverters._ class RedisStreamTest extends AnyWordSpec with Matchers with BeforeAndAfterAll with MockitoSugar { // // val redisServer = new RedisServer(6379) // // override def beforeAll() = redisServer.start() // // override def afterAll() = redisServer.stop() "Redis Stream writer" should { "write Kafka records to a Redis Stream" in { val TOPIC = "cpuTopic" val KCQL = s"INSERT INTO stream1 SELECT * from $TOPIC STOREAS STREAM" println("Testing KCQL : " + KCQL) val props = Map( RedisConfigConstants.REDIS_HOST->"localhost", RedisConfigConstants.REDIS_PORT->"6379", RedisConfigConstants.KCQL_CONFIG->KCQL, RedisConfigConstants.REDIS_PASSWORD -> "" ).asJava val config = RedisConfig(props) val connectionInfo = new RedisConnectionInfo("localhost", 6379, None) val settings = RedisSinkSettings(config) val writer = new RedisStreams(settings) val schema = SchemaBuilder.struct().name("com.example.Cpu") .field("type", Schema.STRING_SCHEMA) .field("temperature", Schema.FLOAT64_SCHEMA) .field("voltage", Schema.FLOAT64_SCHEMA) .field("ts", Schema.INT64_SCHEMA).build() val struct1 = new Struct(schema).put("type", "Xeon").put("temperature", 60.4).put("voltage", 90.1).put("ts", 1482180657010L) val sinkRecord1 = new SinkRecord(TOPIC, 0, null, null, schema, struct1, 1) val jedis = mock[Jedis] writer.jedis = jedis val map = new util.HashMap[String, String]() map.put("type", "Xeon") map.put("temperature", "60.4") map.put("voltage", "90.1") map.put("ts", 1482180657010L.toString) when(jedis.auth("")).isLenient() when(jedis.xadd("stream1", null, map)).thenReturn(mock[StreamEntryID]) writer.initialize(1, settings.errorPolicy) writer.write(Seq(sinkRecord1)) } } }