scala:管理jedis实例方式、操作redis服务

引入jar包

 <redis.version>3.7.0</redis.version> 

<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>${redis.version}</version>
    </dependency>

创建配置文件:

在maven工程的同一目录下,创建配置文件config/sparkstream.properties

内容:

redis.host=single01
redis.port=6379
redis.auth=kb16nb
redis.maxCon=3
redis.maxIdle=1
redis.timeOut=10000

scala编程,操作redis

import java.io.{File, FileReader}
import java.util.Properties


import org.apache.commons.pool2.impl.GenericObjectPoolConfig
import redis.clients.jedis.{Jedis, JedisPool}

import scala.collection.mutable

//解析获取配置的路径
object PathUtil{
  def find(dir:String):Option[File]={
    //找到项目类文件路径
    var classPath: String = Thread.currentThread()
      .getContextClassLoader
      .getResource("")
      .getPath

     classPath= new File(classPath)
      .getParentFile
      .getParentFile
      .getParentFile
      .getAbsolutePath
    val file = new File(s"$classPath/$dir")
    if(file.exists){
      Option.apply(file)
    }else{
      Option.empty
    }
  }
}
//解析配置
object ConfigUtil{
  val config = new Properties()
  import PathUtil.find
  //加载配置
  def load(dir:String)={
    val opt: Option[File] = find(dir)
    if(opt.isEmpty){
      println(s"$dir not exist")
      System.exit(-1)
    }
    config.load(new FileReader(opt.get))
  }
  //取一个键
  def get(key:String):String={
    config.getProperty(key)
  }
  //取多个键
  def filter(keyPrefix:String):mutable.Map[String,String]={
    //可变的HashMap,用作存储取出的配置信息
    val map = new mutable.HashMap[String, String]()
    config
      .stringPropertyNames()  //获取全部配置
      .forEach(name=>
        if(name.startsWith(keyPrefix))
          map.put(name,config.getProperty(name))
      )
    map
  }

  def main(args: Array[String]): Unit = {
//    println(find("config/sparkstream.properties"))
    /*load("config/sparkstream.properties")
    filter("redis")
        .foreach(println)*/
//    println(get[String]("kafka.topic"))
  }
}
//关闭方法
object CloseUtil{
  def close(closes:AutoCloseable*)=
    closes
    .filter(null!=_)
    .foreach(c=>{
      try{
        c.close()
      }catch {
        case e:Throwable=>e.printStackTrace()
      }
    })
}
//操作redis服务
//jedis方式:通过redis.clients.jedis.JedisPool来管理,即通过池来管理;
// 通过池对象获取jedis实例,然后通过jedis实例直接操作redis服务
object JedisUtil {
  import ConfigUtil._
  //创建连接池
  var pool:JedisPool=null
  init()
  private def init()={
    load("config/sparkstream.properties")
   val config = new GenericObjectPoolConfig[Jedis]()
    config.setMaxTotal(get("redis.maxCon").toInt) //最大连接数
    config.setMaxIdle(get("redis.maxIdle").toInt)//最大空闲数
    pool =new JedisPool(
      config,
      get("redis.host"),
      get("redis.port").toInt,
      10000,
      get("redis.auth")
    )
  }

//写
  def write()={
    var con: Jedis = null
    try{
      con=pool.getResource
      con.hset("aa","0","123")//主题,分区,偏移量 键键值
      con.hset("aa","1","234")
      con.hset("aa","2","345")
    }catch {
      case e:Throwable=>e.printStackTrace()
    }finally {
      pool.returnResource(con)//关连接池
    }
  }
//读
  def read()={
    var con: Jedis =null
    try{
      con=pool.getResource
      con.hgetAll("aa")//根据键,取值
        .forEach((k,v)=>println(s"$k,$v"))
    }catch {
      case e:Throwable=>e.printStackTrace()
    }finally {
      pool.returnResource(con)//关连接池
    }
  }

  def main(args: Array[String]): Unit = {
    write()

    read()
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值