@@ -41,55 +41,56 @@ class RedisContext(@transient val sc: SparkContext) extends Serializable {
41
41
42
42
/**
43
43
* @param kvs Pair RDD of K/V
44
+ * @param ttl time to live
44
45
*/
45
- def toRedisKV (kvs : RDD [(String , String )])
46
+ def toRedisKV (kvs : RDD [(String , String )], ttl : Int = 0 )
46
47
(implicit redisConfig : RedisConfig = new RedisConfig (new RedisEndpoint (sc.getConf))) {
47
48
48
- kvs.foreachPartition(partition => setKVs(partition, redisConfig))
49
+ kvs.foreachPartition(partition => setKVs(partition, ttl, redisConfig))
49
50
}
50
51
51
52
/**
52
53
* @param kvs Pair RDD of K/V
53
54
* @param hashName target hash's name which hold all the kvs
55
+ * @param ttl time to live
54
56
*/
55
- def toRedisHASH (kvs : RDD [(String , String )],
56
- hashName : String )
57
+ def toRedisHASH (kvs : RDD [(String , String )], hashName : String , ttl : Int = 0 )
57
58
(implicit redisConfig : RedisConfig = new RedisConfig (new RedisEndpoint (sc.getConf))) {
58
59
59
- kvs.foreachPartition(partition => setHash(hashName, partition, redisConfig))
60
+ kvs.foreachPartition(partition => setHash(hashName, partition, ttl, redisConfig))
60
61
}
61
62
62
63
/**
63
64
* @param kvs Pair RDD of K/V
64
65
* @param zsetName target zset's name which hold all the kvs
66
+ * @param ttl time to live
65
67
*/
66
- def toRedisZSET (kvs : RDD [(String , String )],
67
- zsetName : String )
68
+ def toRedisZSET (kvs : RDD [(String , String )], zsetName : String , ttl : Int = 0 )
68
69
(implicit redisConfig : RedisConfig = new RedisConfig (new RedisEndpoint (sc.getConf))) {
69
70
70
- kvs.foreachPartition(partition => setZset(zsetName, partition, redisConfig))
71
+ kvs.foreachPartition(partition => setZset(zsetName, partition, ttl, redisConfig))
71
72
}
72
73
73
74
/**
74
75
* @param vs RDD of values
75
76
* @param setName target set's name which hold all the vs
77
+ * @param ttl time to live
76
78
*/
77
- def toRedisSET (vs : RDD [String ],
78
- setName : String )
79
+ def toRedisSET (vs : RDD [String ], setName : String , ttl : Int = 0 )
79
80
(implicit redisConfig : RedisConfig = new RedisConfig (new RedisEndpoint (sc.getConf))) {
80
81
81
- vs.foreachPartition(partition => setSet(setName, partition, redisConfig))
82
+ vs.foreachPartition(partition => setSet(setName, partition, ttl, redisConfig))
82
83
}
83
84
84
85
/**
85
86
* @param vs RDD of values
86
87
* @param listName target list's name which hold all the vs
88
+ * @param ttl time to live
87
89
*/
88
- def toRedisLIST (vs : RDD [String ],
89
- listName : String )
90
+ def toRedisLIST (vs : RDD [String ], listName : String , ttl : Int = 0 )
90
91
(implicit redisConfig : RedisConfig = new RedisConfig (new RedisEndpoint (sc.getConf))) {
91
92
92
- vs.foreachPartition(partition => setList(listName, partition, redisConfig))
93
+ vs.foreachPartition(partition => setList(listName, partition, ttl, redisConfig))
93
94
}
94
95
95
96
/**
@@ -114,15 +115,21 @@ object RedisContext extends Serializable {
114
115
/**
115
116
* @param arr k/vs which should be saved in the target host
116
117
* save all the k/vs to the target host
118
+ * @param ttl time to live
117
119
*/
118
- def setKVs (arr : Iterator [(String , String )], redisConfig : RedisConfig ) {
120
+ def setKVs (arr : Iterator [(String , String )], ttl : Int , redisConfig : RedisConfig ) {
119
121
120
122
arr.map(kv => (redisConfig.getHost(kv._1), kv)).toArray.groupBy(_._1).
121
123
mapValues(a => a.map(p => p._2)).foreach {
122
124
x => {
123
125
val conn = x._1.endpoint.connect()
124
126
val pipeline = x._1.endpoint.connect.pipelined
125
- x._2.foreach(x => pipeline.set(x._1, x._2))
127
+ if (ttl <= 0 ) {
128
+ x._2.foreach(x => pipeline.set(x._1, x._2))
129
+ }
130
+ else {
131
+ x._2.foreach(x => pipeline.setex(x._1, ttl, x._2))
132
+ }
126
133
pipeline.sync
127
134
conn.close
128
135
}
@@ -131,43 +138,49 @@ object RedisContext extends Serializable {
131
138
132
139
133
140
/**
134
- * @param key
141
+ * @param hashName
135
142
* @param arr k/vs which should be saved in the target host
136
143
* save all the k/vs to hashName(list type) to the target host
144
+ * @param ttl time to live
137
145
*/
138
- def setHash (key : String , arr : Iterator [(String , String )], redisConfig : RedisConfig ) {
146
+ def setHash (hashName : String , arr : Iterator [(String , String )], ttl : Int , redisConfig : RedisConfig ) {
139
147
140
- val conn = redisConfig.connectionForKey(key )
148
+ val conn = redisConfig.connectionForKey(hashName )
141
149
val pipeline = conn.pipelined
142
- arr.foreach(x => pipeline.hset(key, x._1, x._2))
150
+ arr.foreach(x => pipeline.hset(hashName, x._1, x._2))
151
+ if (ttl > 0 ) pipeline.expire(hashName, ttl)
143
152
pipeline.sync
144
153
conn.close
145
154
}
146
155
147
156
/**
148
- * @param key
157
+ * @param zsetName
149
158
* @param arr k/vs which should be saved in the target host
150
159
* save all the k/vs to zsetName(zset type) to the target host
160
+ * @param ttl time to live
151
161
*/
152
- def setZset (key : String , arr : Iterator [(String , String )], redisConfig : RedisConfig ) {
162
+ def setZset (zsetName : String , arr : Iterator [(String , String )], ttl : Int , redisConfig : RedisConfig ) {
153
163
154
- val jedis = redisConfig.connectionForKey(key )
164
+ val jedis = redisConfig.connectionForKey(zsetName )
155
165
val pipeline = jedis.pipelined
156
- arr.foreach(x => pipeline.zadd(key, x._2.toDouble, x._1))
166
+ arr.foreach(x => pipeline.zadd(zsetName, x._2.toDouble, x._1))
167
+ if (ttl > 0 ) pipeline.expire(zsetName, ttl)
157
168
pipeline.sync
158
169
jedis.close
159
170
}
160
171
161
172
/**
162
- * @param key
173
+ * @param setName
163
174
* @param arr values which should be saved in the target host
164
175
* save all the values to setName(set type) to the target host
176
+ * @param ttl time to live
165
177
*/
166
- def setSet (key : String , arr : Iterator [String ], redisConfig : RedisConfig ) {
178
+ def setSet (setName : String , arr : Iterator [String ], ttl : Int , redisConfig : RedisConfig ) {
167
179
168
- val jedis = redisConfig.connectionForKey(key )
180
+ val jedis = redisConfig.connectionForKey(setName )
169
181
val pipeline = jedis.pipelined
170
- arr.foreach(pipeline.sadd(key, _))
182
+ arr.foreach(pipeline.sadd(setName, _))
183
+ if (ttl > 0 ) pipeline.expire(setName, ttl)
171
184
pipeline.sync
172
185
jedis.close
173
186
}
@@ -176,12 +189,14 @@ object RedisContext extends Serializable {
176
189
* @param listName
177
190
* @param arr values which should be saved in the target host
178
191
* save all the values to listName(list type) to the target host
192
+ * @param ttl time to live
179
193
*/
180
- def setList (listName : String , arr : Iterator [String ], redisConfig : RedisConfig ) {
194
+ def setList (listName : String , arr : Iterator [String ], ttl : Int , redisConfig : RedisConfig ) {
181
195
182
196
val jedis = redisConfig.connectionForKey(listName)
183
197
val pipeline = jedis.pipelined
184
198
arr.foreach(pipeline.rpush(listName, _))
199
+ if (ttl > 0 ) pipeline.expire(listName, ttl)
185
200
pipeline.sync
186
201
jedis.close
187
202
}
0 commit comments