开发者社区 问答 正文

对于Hazelcast python客户机,如何在服务器端不使用retain_all()的情况下在多

我有多个Hazelcast集,我想找到它们的交集,但是我想避免在客户端获取任何数据。我当前的方法就是使用这段代码。它找到了第一个集合和集合其余部分列表的交集因此set1现在是所有集合的交集。

for i in range(1, len(sets)):
    cur = sets[i]
    set1.retain_all(cur.get_all())

Hazelcast的retain_all不能处理两个集合实体,只能处理一个集合和一个集合,这不是我要找的。例如,这段代码可以用Redis完成,所以我希望它的Hazelcast等价。

set_result = "set_result"
redisClient.sinterstore(set_result, *list(sets))

任何帮助将不胜感激! 问题来源StackOverflow 地址:/questions/59384545/for-hazelcast-python-client-how-do-i-do-hazelcast-set-intersection-between-mult

展开
收起
kun坤 2019-12-26 14:35:08 695 分享 版权
1 条回答
写回答
取消 提交回答
  • 为Hazelcast的ISet是一个集合,它是一个集合,下面的代码应该工作:

    set1.retainAll(cur);
    

    但是,您似乎不希望对set1进行修改,而是希望将结果存储在不同的集合中,就像redis的烧结存储功能一样。 下面是一个替代实现的例子:

    public class RetainAllExample {
    
    public static void main(String[] args) {
    
        HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
        HazelcastInstance h2 = Hazelcast.newHazelcastInstance();
    
        Set<String> set1 = h1.getSet("set1");
        Set<String> set2 = h1.getSet("set2");
    
        set1.add("a");
        set1.add("b");
        set1.add("c");
        set1.add("d");
    
        set2.add("c");
        set2.add("d");
        set2.add("e");
    
        String resultName = "result";
        String[] setNames = new String[] { "set1", "set2"};
        RetainAll retainAll = new RetainAll(resultName, setNames;
        IExecutorService exec = h1.getExecutorService("HZ-Executor-1");
        Future<Boolean> task = exec.submit(retainAll);
    
        try {
            if(task.get(1_000, TimeUnit.MILLISECONDS)) {
                Set<String> result = h1.getSet(resultName);
                result.forEach(str -> System.out.println(str + ", "));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        System.exit(0);
    }
    
    static class RetainAll implements Callable<Boolean>, HazelcastInstanceAware, Serializable {
        private HazelcastInstance hazelcastInstance;
        private String resultSetName;
        private String[] setNames;
        public RetainAll(String resultSetName, String[] setNames) {
            this.resultSetName = resultSetName;
            this.setNames = setNames;
        }
        @Override
        public Boolean call() {
            try {
                Set[] sets = new Set[setNames.length];
                IntStream.range(0, setNames.length).forEach(i -> sets[i] = hazelcastInstance.getSet(setNames[i]));
    
                ISet resultSet = hazelcastInstance.getSet(resultSetName);
                resultSet.addAll(sets[0]);
                IntStream.range(1, sets.length).forEach(i -> resultSet.retainAll(sets[i]));
            }
            catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        @Override
        public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
            this.hazelcastInstance = hazelcastInstance;
        }
    }
    

    }

    2019-12-26 14:35:21
    赞同 展开评论