anirudhas
anirudhas

Reputation: 71

Is it possible to join two different IMaps in Hazelcast?

I have two hazelcast maps 1. map2 2. map2

I wanted to join both maps on the key.

Can I do it in the hazelcast?

Thank you

Upvotes: 1

Views: 1102

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200148

Updated answer for Jet 0.7:

If you are joining two IMaps on their "primary key" (the actual key used in the map), you should use the hashJoin pipeline transform. Let's start with these source stages:

BatchStage<Entry<String, Person>> person = p.drawFrom(Sources.map("person"));
BatchStage<Entry<String, Employee>> employee = p.drawFrom(Sources.map("employee"));

This is a basic way to join them:

BatchStage<Tuple2<Person, Employee>> joined =
    persons.hashJoin(employees, joinMapEntries(Entry::getKey),
        (personEntry, employee) -> tuple2(personEntry.getValue(), employee));

You'll get pairs of joined items (Tuple2<Person, Employee>). Another common case is attaching one item to the other, for example Employee could have a person property:

class Employee { 
    Person person; 

    Employee setPerson(Person person) { 
        this.person = person;
        return this;
    } 
}

With this you can write

BatchStage<Employee> joined =
    persons.hashJoin(employees, joinMapEntries(Entry::getKey),
        (personEntry, employee) -> employee.setPerson(personEntry.getValue()));

Old answer for Jet 0.4:

You can use Hazelcast Jet to join two maps in a streaming fashion. Check out the cogroup-operator code sample in which a custom joining processor is implemented (the terms "co-group" and "join" are closely related). You'll be able to supply two map sources and in the output stream you'll get a pair of collections per distinct key, holding all the items with the same key from each of the sources. You can adapt it to produce pairs of items as well.

First-class support for stream joining is a feature planned for the upcoming release of Jet.

Upvotes: 6

Hiten
Hiten

Reputation: 53

By joining what you mean or what operation you want to do by joining? You can have 2 different map with same key and configure as a PartitionAware so that both keys will be on same partition/member (i.e. operations can be executed without the cost of extra network calls and extra wire data). http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#data-affinity

Upvotes: 0

Related Questions