Skip to content

Instantly share code, notes, and snippets.

@soumyasd
Created June 23, 2013 18:22
Show Gist options
  • Save soumyasd/5845962 to your computer and use it in GitHub Desktop.
Save soumyasd/5845962 to your computer and use it in GitHub Desktop.
Simple Redis ZSET Demo with Dates
package redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* User: ssimanta
* Date: 6/13/13
* Time: 6:36 PM
*
* Simple demo that gets a list from redis on a perodic basis
*/
public class RedisSortedSetQueryByTimeDemo {
public static final String REDIS_HISTORY_TABLE_KEY = "history.demo";
protected JedisPool redisPool;
public static void main(String args[]) throws Exception
{
RedisSortedSetQueryByTimeDemo demo = new RedisSortedSetQueryByTimeDemo("localhost");
//populate some dummy date for testing
//In when running the real program we don't have to do this step. The backend analysis will be populating the Redis database
//all you need to know is the Redis key where the data is being stored
int hoursAgo = 2;
demo.populateDummyDataInRedis(hoursAgo*(60*60), demo.getDateInPast(hoursAgo).getTime(), 1);
Set<String> resutls = demo.getData(demo.getDateInPast(1).getTime(), demo.getDateInPast(0).getTime());
System.out.println(resutls);
}
public RedisSortedSetQueryByTimeDemo(final String redisHost)
{
//create the Redis connection pool
redisPool = new JedisPool(new JedisPoolConfig(), redisHost);
Date dateinPast = getDateInPast(1);
long timeInPast = dateinPast.getTime();
//create a date string
System.out.println("Date: " + dateinPast + " time: " + timeInPast );
System.out.println("Date from mili since epoch:" + new Date(timeInPast));
}
public Set<String> getData(final long startTimeStamp, final long endTimeStamp)
{
Jedis redis = redisPool.getResource();
java.util.Set<String> results = redis.zrangeByScore(REDIS_HISTORY_TABLE_KEY, startTimeStamp, endTimeStamp);
return results;
}
/**
* Return a java.util.Date object that represents the actual date <code>hoursAgo</code> ago in the past.
* For example, if the current time is 12:08 PM then this method will return 11:00 AM
*
* @param hoursAgo
* @return
*/
public Date getDateInPast(int hoursAgo)
{
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - hoursAgo);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
return date;
}
public void populateDummyDataInRedis(final int numberOfDummyEntries, final long startTimeStampInPast, final int gapInSeconds) throws Exception
{
double score = 0.0;
Jedis jedis = redisPool.getResource();
for( int i =0; i<numberOfDummyEntries; i++)
{
int p = new Double(Math.random()*1000).intValue();
int n = new Double(Math.random()*1000).intValue();
String sentimentString = "{ \"P\":" + p + ", \"N\":" + n + "}";
//score = System.currentTimeMillis();
score = startTimeStampInPast + gapInSeconds*1000;
System.out.println("time: " + score + " " + sentimentString);
jedis.zadd(REDIS_HISTORY_TABLE_KEY, score, sentimentString);//ZADD
//Thread.sleep(gapInMS);
}
redisPool.returnResource(jedis);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment