Redis Standalone,Redis Cluster的安裝在前面介紹過,地址:http://www.cnblogs.com/liuchangchun/p/5063477.html,這裏不作介紹。html
針對各類編程語言,差很少都有Redis的驅動程序。針對Scala程序,在github上面有幾個可用驅動:java
scala-redis:https://github.com/liuchchc/scala-redisgit
rediscala:https://github.com/etaty/rediscalagithub
jedis:https://github.com/liuchchc/jedisredis
我用的是jedis,感受仍是蠻好用的,支持Cluster編程
Standalone模式中,Cluster提供了哨兵用於監視Redis Server的運行狀態,並選舉出Master,這個模式中須要配置sentinel.conf,在這種模式下Jedis中以下使用編程語言
val sentinelClusterName = "mymaster" val sentinelServerHostHS = new HashSet[String] sentinelServerHostHS.add("192.168.1.103:26379") sentinelServerHostHS.add("192.168.1.104:26379") sentinelServerHostHS.add("192.168.1.105:26379") sentinelServerHostHS.add("192.168.1.106:26379") sentinelServerHostHS.add("192.168.1.107:26379") sentinelServerHostHS.add("192.168.1.108:26379") // 若是賦值爲-1,則表示不限制;若是pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡),默認值是8。 val MAX_ACTIVE = -1; // 控制一個pool最多有多少個狀態爲idle(空閒的)的jedis實例,默認值也是8。 val MAX_IDLE = -1; // 等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時。若是超過等待時間,則直接拋出JedisConnectionException; val MAX_WAIT = -1; // 超時時間,0永不超時 val TIMEOUT = 0; var poolConfig = new GenericObjectPoolConfig poolConfig.setMaxTotal(MAX_ACTIVE) poolConfig.setMaxIdle(MAX_IDLE) poolConfig.setMaxWaitMillis(MAX_WAIT) poolConfig.setTestOnBorrow(true) poolConfig.setTestOnReturn(true) def getPoolConfig: GenericObjectPoolConfig = poolConfig
lazy val jedisSentinelPool = new JedisSentinelPool("mymaster", sentinelServerHostHS, poolConfig, TIMEOUT)
lazy val sentinelConnection = jedisShardedPool.getResource
// Redis 操做……
// Redis 操做完畢
sentinelConnection.close
jedisSentinelPool.destroy
val shardsServerHostList = new ArrayList[JedisShardInfo] val si1 = new JedisShardInfo("192.168.1.103", 6379) val si2 = new JedisShardInfo("192.168.1.104", 6379) val si3 = new JedisShardInfo("192.168.1.105", 6379) val si4 = new JedisShardInfo("192.168.1.106", 6379) val si5 = new JedisShardInfo("192.168.1.107", 6379) val si6 = new JedisShardInfo("192.168.1.108", 6379) shardsServerHostList.add(si1) shardsServerHostList.add(si2) shardsServerHostList.add(si3) shardsServerHostList.add(si4) shardsServerHostList.add(si5) shardsServerHostList.add(si6) lazy val jedisShardedPool = new ShardedJedisPool(poolConfig, shardsServerHostList) lazy val shardedConnection = jedisShardedPool.getResource
// Redis 操做……
// Redis 操做完畢
shardedConnection.close
jedisShardedPool.destroy
在Redis Cluster模式中,我這裏面是3 master,6 slave,HostAndPort只須要傳1個,Redis會本身檢測整個集羣中有哪些Master和Slave。至於在添加數據時候數據存儲到哪一個集羣中,用戶層面不須要關心,讀取數據也同樣。性能
val jedisClusterNodes = new java.util.HashSet[HostAndPort]() jedisClusterNodes.add(new HostAndPort("192.168.1.100", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.101", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.104", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.105", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.106", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.107", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.108", 6379)) jedisClusterNodes.add(new HostAndPort("192.168.1.109", 6379)) // Redis 操做
lazy val jedisCluster = new JedisCluster(jedisClusterNodes)
這裏是Spark處理生物數據,由於中間結果比較多,因此加了一臺Redis Server存儲中間結果,總共1億多條,3.5G左右,大概在分分鐘就能存進去,因此Redis的性能仍是槓槓滴,不過要批量寫,讀,不然也會慢的很。spa