英文:
Convert a single node to distributed java application with Hazelcast
问题
我们有一个当前的Java应用程序,使用Vert.x构建,使用简单的本地HashMaps
。为了在多个节点上分发我们的应用程序,我们想要将本地的HashMaps替换为一个Hazelcast的IMap
。
基本上,我们有两个HashMaps
:
Map<String, Group> // <groupId - group>
Map<String, Set<String> // <memberId - set of groupIds>
我们有两个HashMaps
,以便可以轻松地通过它们的ID查询我们的组,并知道我们的成员属于哪些组。
然而,使用IMap
,根据文档,应该可以创建一个映射:
IMap<String, Group> // <groupId - group>
class Group {
String id;
Set<Member> members;
}
class Member {
String id;
// 一些数据
}
并且我们应该能够执行:
Collection<Group> groups = imap.values(Predicates.in("members", varargs of member ids))
所以,我有4个问题:
- 你能确认这种行为在IMap中是可能的吗?
- 如果是的话,
imap.values
返回的组集合是否可能包含重复项(相同的groupId)?我是否需要执行一种“distinct”操作? - 我们不确定是应该使用Vertx + Hazelcast,还是将所有处理代码移到Hazelcast Jet。仅迁移到Hazelcast Jet有什么优势?
- 如果我们使用Hazelcast Jet,是否可以在
StreamStage
中像这样使用IMap.values(Predicate)功能:
StreamStage.flatMap(memberIds -> Traversers.traverseIterable(imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0]))))
非常感谢!
英文:
We have a current java application built with Vert.x using simple local HashMaps
. In order to distribute our application on several nodes, we would like to replace local HashMaps by a single Hazelcast IMap
.
Basically we have our 2 HashMaps
:
Map<String, Group> // <groupId - group>
Map<String, Set<String> // <memberId - set of groupIds>
We have 2 HashMaps
in order to query easily our groups by their ids and to know to which groups belong our members.
However, with IMap
, according to documentation, it should be possible to create only one map :
IMap<String, Group> // <groupId - group>
class Group {
String id;
Set<Member> members;
}
class Member {
String id;
// some data
}
and we should be able to perform :
Collection<Group> groups = imap.values(Predicates.in("members", varargs of member ids))
So, I have 4 questions :
- Could you confirm this behavior is possible with IMap ?
- If yes, is it possible the collection of groups returned by
imap.values
contain duplicates (same groupId) ? Do I have to perform a kind ofdistinct
operation ? - We are not sure if we should use Vertx + Hazelcast, or move all the processing code to Hazelcast Jet. What would be the advantages to move to Hazelcast Jet only ?
- If we use Hazelcast Jet, is it possible to use IMap.values(Predicate) feature inside
StreamStage
like that :
StreamStage.flatMap(memberIds -> Traversers.traverseIterable(imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0])))
Thanks a lot !
答案1
得分: 2
-
是的
-
如果您的原始映射中不包含重复项,并且IMap没有并发更新,它不应该包含重复项。
-
这取决于您的用例,我不能一般性地告诉您。
-
这不太好,因为
imap.values
调用是阻塞的,而在flatMap
中的函数不能阻塞。我所说的阻塞是指它执行阻塞的IO操作。另外,您不能从局部变量中捕获imap
实例,因为它不是可序列化的,Jet无法将其发送到成员。但是您可以使用mapUsingService
,并像这样使用ServiceFactories.imapService
:
.mapUsingService(
ServiceFactories.iMapService("my_map").toNonCooperative(),
(imap, memberIds) ->
imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0]))))
英文:
-
yes
-
it should not contain duplicates if your original map doesn't contain them and the IMap isn't concurrently updated
-
depends on your use case, I can't tell in general
-
This is not good as the
imap.values
call is blocking and the function inflatMap
must not block. By blocking I mean it does blocking IO operations. Also you can't capture theimap
instance from a local variable because it's not serializable and Jet can't send it to the member. But you can usemapUsingService
and useServiceFactories.imapService
like this:
.mapUsingService(
ServiceFactories.iMapService("my_map").toNonCooperative(),
(imap, memberIds) ->
imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0]))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论