https://my.oschina.net/u/2338224/blog/3061507
使用Java读取 “Python写入redis” 的数据踩坑记录
https://segmentfault.com/q/10100 ... 0007054559/revision
可以使用Python读取java写入redis 的object数据,并解析吗?
https://github.com/andymccurdy/redis-py/issues/777
how can i set 'class' object and get it from redis ? #777
https://segmentfault.com/q/1010000007043502?_ea=1222915
可以使用Python读取java写入redis 的object数据,并解析吗?
https://www.liangzl.com/get-article-detail-132528.html
使用Java读取 “Python写入redis” 的数据踩坑记录
-Java 存储与python共享数据时,应避免将String字符串当做对象进行序列化存储,应直接以字符串的形式存
储,如果需要共享对象,对象转化为json串存储。
@SuppressWarnings("resource")
public static Map<String,String> getAllHashByKey(String key) {
try {
JedisPool pool = null;
pool = new JedisPool(RedisUtil.REDIS_HOST,RedisUtil.REDIS_PORT);
Jedis jedisClient = pool.getResource();
jedisClient.auth(RedisUtil.REDIS_PASSWORD);
Map<String, String> map = jedisClient.hgetAll(key);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml","classpath:spring-redis.xml"})
public class RedisTest {
@Autowired
private RedisUtil redisUtil;
@SuppressWarnings("resource")
@Test
public void getHashKeyValue() {
JedisPool pool = null;
pool = new JedisPool("localhost",6379);
Jedis jedisClient = pool.getResource();
jedisClient.auth("1234");
String key = "device:49bd573355464bfbad3dce4f68345008";
// List<String> list = jedisClient.hmget("device:49bd573355464bfbad3dce4f68345008");
// System.out.println(list);
String temperature = jedisClient.hget("device:49bd573355464bfbad3dce4f68345008","temperature");
System.out.println(temperature);
// List<String> list = jedisClient.hmget("device:49bd573355464bfbad3dce4f68345008");
// System.out.println(list);
// jedisClient.hmget(key, fields)
Map<String, String> map = jedisClient.hgetAll(key);
System.out.println(map);
}
@Test
public void getAllHashByKey() {
// Map<Object, Object> map = redisUtil.hmget("device:49bd573355464bfbad3dce4f68345008");
// Map<Object, Object> map = redisUtil.hmget("device:DMB28E3ACBD4B22A8633E13E58717038DE");
// Map<Object, Object> map = redisUtil.hmget("device:WPD741705FE5210AE2096D2C05BD3466CA");
Map<String, String> map = RedisUtil.getAllHashByKey("device:DMB28E3ACBD4B22A8633E13E58717038DE");
int size = map.size();
System.out.println(size);
System.out.println(map);
// Integer reportStatus = (Integer) map.get("reportStatus");
// Integer powerConsumption = (Integer) map.get("powerConsumption");
// System.out.println(reportStatus);
// System.out.println(powerConsumption);
}
}
@Component
public class RedisUtil {
// (自动注入redisTemplet)
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public final static String REDIS_HOST = "localhost";
public final static Integer REDIS_PORT = 6379;
public final static String REDIS_PASSWORD = "1234";
public final static Integer MAX_TOTAL = 500;
public final static Integer MAX_WAIT_MILLIS = 2000;
public static JedisPool jedisPool = null;
public static JedisPoolConfig config = null;
/*关闭Jedis*/
public static void close(Jedis jedis){
if(jedis!=null){
jedis.close();
}
}
public static Map<String,String> getAllHashByKey(String key) {
Jedis jedis = null;
try {
// 1 获得连接池配置对象,设置配置项
config = new JedisPoolConfig();
// 1.1 最大连接数
config.setMaxTotal(200);
config.setMaxWaitMillis(2000);
jedisPool = new JedisPool(config,RedisUtil.REDIS_HOST,RedisUtil.REDIS_PORT);
jedis = jedisPool.getResource();
jedis.auth(RedisUtil.REDIS_PASSWORD);
Map<String, String> map = jedis.hgetAll(key);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
close(jedis);
}
}
}
|
|