Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从hazelcast映射中获取随机键值_Java_Hazelcast_Hazelcast Imap - Fatal编程技术网

Java 如何从hazelcast映射中获取随机键值

Java 如何从hazelcast映射中获取随机键值,java,hazelcast,hazelcast-imap,Java,Hazelcast,Hazelcast Imap,我试着从地图上得到一个像这样的随机元素 IMap<Integer, Integer> workWaitTasks = hazelcastInstance.getMap(WORK_WAIT_TASKS); collectionTask = Collections.singleton(workWaitTasks.values().stream() .skip(workWaitTasks.isEmpty() ? 0 : new Random().nextIn

我试着从地图上得到一个像这样的随机元素

IMap<Integer, Integer> workWaitTasks = hazelcastInstance.getMap(WORK_WAIT_TASKS);
collectionTask = Collections.singleton(workWaitTasks.values().stream()
                .skip(workWaitTasks.isEmpty() ? 0 : new Random().nextInt(workWaitTasks.size()))
                .findFirst()
                .get());
int taskId = collectionTask.iterator().next();
 SELECT column FROM table
 ORDER BY RAND()
 LIMIT 1
如何在hazelcast中生成正确的谓词


你能举个例子吗?请帮帮我

我认为使用公共API没有简单有效的方法来实现这一点。一种选择是使用Jet:

IMap sourceMap=instance.getMap(“表”);
IList targetList=instance.getList(“结果”);
int samplingFactor=sourceMap.size()/10;
Pipeline p=Pipeline.create();
p、 readFrom(Sources.map(sourceMap))
.filter(item->item.getKey().hashCode()%samplingFactor==ThreadLocalRandom.current().nextInt(samplingFactor))
.writeTo(Sinks.list(targetList));
newJob(p.join();
上面的代码应该向结果列表中添加大约10个元素,从中很容易获得一个随机条目,但也可能以一个空列表结束-您可以尝试在
samplingFactor
中增加乘数,以获得满意的结果概率,而无需重新运行作业

使用聚合(
IMap.aggregate
)和自定义聚合器也可以实现这一点,一些同事可能会给出答案:-)