简体   繁体   English

对于 Hazelcast python 客户端,我如何在服务器端没有保留_all() 的情况下在多个 Hazelcast 集实体之间设置交集?

[英]For Hazelcast python client, how do i do Hazelcast set intersection between multiple Hazelcast set entities without retain_all() on server-side?

I have multiple Hazelcast sets for which I want to find the Intersection, however I want to avoid pulling any data on the client side.我有多个 Hazelcast 集,我想为其找到交集,但是我想避免在客户端提取任何数据。 My current approach is exactly that with this code.我目前的方法正是使用这段代码。 It finds intersection between the 1st set and the list of the rest of set so that set1 is now the intersection of all.它找到第 1 个集合和其余集合的列表之间的交集,因此 set1 现在是所有集的交集。

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

Hazelcast's retain_all doesn't work with 2 set entities, only with a set and a collection which is not what I am looking for. Hazelcast 的 retain_all 不适用于 2 个集合实体,只能使用一个集合和一个集合,这不是我正在寻找的。 For example, it can be done with Redis with this code, so I want its Hazelcast equivalent.例如,它可以用 Redis 和这段代码来完成,所以我想要它的 Hazelcast 等价物。

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

Any help would be appreciated!任何帮助,将不胜感激!

Since Hazelcast's ISet is a Set which is a Collection the following code should work:由于 Hazelcast 的 ISet 是一个集合,因此以下代码应该可以工作:

set1.retainAll(cur);

But, it doesn't seem like you'd like set1 to be modified but would rather store the result in a different set much like redis' sinterstore function.但是,您似乎并不希望修改 set1,而是希望将结果存储在不同的集合中,就像 redis 的 sinterstore 函数一样。

The following is an example of an alternative implementation:以下是替代实现的示例:

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;
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM