我一直在尝试使用Hazelcast PartitionPredicate,并注意到同一个密钥可以出现在多个分区中。有人可以解释一下这是为什么吗?或者如果你知道一篇解释这种行为的好文章?
示例代码:
创建测试缓存:
def hzCreateTestCache(): Unit = {
val hzClient: HazelcastInstance = getClient
val testCache = hzClient.getMap[HzMapKey, HzMapValue]("Test")
// delete all entries.
testCache.destroy()
for (i <- 1 to 100000) {
var d = 20171224
if (i > 50 * 1000) d = 20171225
val key = new HzMapKey {_date = d; _Id = "id_" + f"$i%010d"}
val value = new HzMapValue {_body = "body_" + key}
testCache.put(key, value)
}
}
从缓存中读取:
def test_read_all_entries_by_partition(): Unit = {
val client = getClient
val testsMap = client.getMap[HzMapKey, HzMapValue]("Test")
println("Size: " + testsMap.size())
val datePredicate = new PredicateBuilder().getEntryObject.get("__key#_date").equal(20171224)
var c = 0
var partValues = Array[String]()
// foreach Hz partition
client.getPartitionService.getPartitions.toArray().foreach(p => {
// create partitionPredicate with PartitionId
val partPredicate = new PartitionPredicate(p.asInstanceOf[Partition].getPartitionId, datePredicate)
val values = testsMap.keySet(partPredicate)
// Increment counter
c = c + values.size()
// Insert into partValues for later...
values.toArray().foreach(i => partValues = partValues :+ i.asInstanceOf[HzMapKey].toString)
})
println("Count: " + partValues.length)
// find duplicates
partValues.groupBy(i => i.toString).map(t => (t._1, t._2.length)).foreach(e => if (e._2 > 1) println(e._1 + ": " + e._2))
}
返回:
尺寸:100000 数:50295 ...