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