Python Java Scala连接redis数据库

Java连接redis

maven:

 <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
public class App 
{
    public static void main( String[] args )
    {
        Jedis jedis = new Jedis("192.168.253.150");
        System.out.println("连接成功");
        jedis.set("zjw","game");
        System.out.println(jedis.get("zjw"));
    }
}
//输出
连接成功
game
192.168.253.150:6379> get zjw
"game"
public class App 
{
    public static void main( String[] args )
    {
        Jedis jedis = new Jedis("192.168.253.150");
        System.out.println("连接成功");
       	jedis.lpush("xxx","zs","hehe","xixi","haha");
    }
}
192.168.253.150:6379> LRANGE xxx 0 10
1) "haha"
2) "xixi"
3) "hehe"
4) "zs"

scala连接redis

maven:

	<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.11</artifactId>
      <version>2.3.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.11</artifactId>
      <version>2.3.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.redislabs/spark-redis -->
    <dependency>
      <groupId>com.redislabs</groupId>
      <artifactId>spark-redis</artifactId>
      <version>2.4.0</version>
    </dependency>
import org.apache.spark.sql.SparkSession
import redis.clients.jedis.Jedis

object rwRedis {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("ttt").master("local[*]").getOrCreate()
    val jd = new Jedis("192.168.253.150")
    val dd = jd.lrange("xxx",0,10)
    val rdd = spark.sparkContext.parallelize(Seq(dd))
    rdd.foreach(x=>println(x))
  }
}

//输出结果
[haha, xixi, hehe, zs]

Python连接redis

添加redis:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d8Qx4i7I-1598438315308)(../../../software/typora/img/image-20200825173052420.png)]

import redis
if __name__ == '__main__':
    handler = redis.Redis(host="192.168.253.150",port=6379,decode_responses=True)
    print(handler.get("zjw"))
    
    //输出
    game
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值