spark读取redis,连接池配置

package com.test.log.makefriends

import redis.clients.jedis.{JedisPool, JedisPoolConfig}
import scala.collection.mutable.ArrayBuffer
/**
  * @Author: jxx
  * @Date: 2018/3/27 16:28
  */
object RedisUtil extends Serializable {
  def redisUtil(data: ArrayBuffer[Array[Byte]]): java.util.ArrayList[AnyRef] = {
    var pool:JedisPool  = null
    if (pool == null) {
      val config = new JedisPoolConfig()

      config.setMaxTotal(96)
      config.setMaxIdle(64)
      config.setMaxWaitMillis(30000)
      config.setTestOnBorrow(false)
      pool = new JedisPool(config, "localhost", port)
    }
    val jedis = pool.getResource

    try {
      val sids = new java.util.ArrayList[AnyRef]
      for(key <- data) sids.add(jedis.get(key))
      sids
    } catch {
      case _: Exception => null
    } finally {
      pool.close()
    }
  }

}

你可能感兴趣的:(spark,redis)