进击的大数据(三):java操作redis

一、redis的java驱动

java驱动下载地址:http://repo1.maven.org/maven2/redis/clients/jedis/,当前最近版本是3.1.0-m3。

       剩下的不详细写了,直接上代码。没有那么复杂,整体感觉上redis还是很不错的,方便、快速、简单。我没有对redis进行并发和大数据测试,对它的性能还没有概念,有机会的话我会进行深度测试。

 

package java_redis;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import model.return_result;
import redis.clients.jedis.Jedis;

/**
 * @author Song
 * @category redis操作类,用于进行数据交互
 * @serial 
 * 【2019年7月3日】	建立对象,完成 redis_connection、insert_string、insert_list、get_list函数 @author Song
 * 【2019年7月4日】	将catch抛出,由调用者捕捉异常	@author Song
 * 【2019年7月4日】	完成del_data函数
 * */
public class redis_dop
{
	//#region 链接redis数据库
	/**
	 * @author Song
	 * @category 连接redis数据库
	 * @serial 
	 * 【2019年7月3日】	建立对象 @author Song
	 * */
		public  Jedis redis_connection() throws Exception
		{
			InputStream is = getClass().getClassLoader().getResourceAsStream("dbconfig.Properties");
			Properties properties = new Properties();
			properties.load(is);
			String redis_host = properties.getProperty("redis_host");
			String redis_port = properties.getProperty("redis_port");
			String redis_pwd = properties.getProperty("redis_pwd");
				
			Jedis jedis = new Jedis(redis_host,Integer.parseInt(redis_port));
				
			jedis.auth(redis_pwd);
			System.out.println("链接成功,服务器正在运行......"+jedis.ping());
			return jedis;
		}
		//#endregion
		
	//#region 写入string实例
	/**
	 * @author Song
	 * @category 向redis写入string类型数据
	 * @param jedis 数据库连接
	 * @serial 
	 * 【2019年7月3日】	建立对象 @author Song
	 * */
	public  void insert_string(Jedis jedis) throws Exception
	{
		jedis.set("name1","宋骏超1");
		jedis.set("sex1","男1");
		jedis.set("age1","151");
	}
	//#endregion
		
	//#region 写入list实例
	/**
	 * @author Song
	 * @category 向redis写入list类型数据
	 * @param jedis 数据库连接
	 * @param data_json json数据字符串
	 * @serial 
	 * 【2019年7月3日】	建立对象 @author Song
	 * */
	public void insert_list(Jedis jedis,String data_json) throws Exception
	{
		jedis.lpush("user-list", data_json);
	}
	//#endregion
	
	//#region list数据查询
	/**
	 * @author Song
	 * @category 查询list数据
	 * @param jedis 数据库连接
	 * @param key redis key值
	 * @param start_index 开始index
	 * @param end_index 结束index
	 * @serial 
	 * 【2019年7月3日】	建立对象 @author Song
	 * */
	public ArrayList<String> get_list(Jedis jedis,String key,int start_index,int end_index) throws Exception
	{
		ArrayList<String> datalist = (ArrayList<String>)jedis.lrange(key,start_index,end_index);
		return datalist;
	}
	//#endregion
	
	//#region 数据删除
	/**
	 * @author Song
	 * @category 删除list数据
	 * @param jedis 数据库连接
	 * @param key redis key值
	 * @param start_index 开始index
	 * @param end_index 结束index
	 * @serial 
	 * 【2019年7月4日】	建立函数	@author Song
	 * */
	public void del_data(Jedis jedis,String key,int start_Index,int end_index) throws Exception
	{
		/**
		 * 删除方法类型:
		 *  lrem key 0 "value" //从mylist中删除全部等值value的元素   0为全部,负值为从尾部开始。 
		 *  ltrim key 1 -1 //保留mylist中 1到末尾的值,即删除第一个值。 
		 *  lpop key //删除最上面的一个值 
		 *  rpop key //删除最下面的一个值
		 **/
		String aa = jedis.ltrim(key, start_Index, end_index);
		//return jedis.del(key);
	}
	//#endregion
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值