2013-10-29 58 views
2

我有問題,理解Hazelcast分佈式執行的概念。據說能夠對特定密鑰的所有者實例執行執行。
從技術文檔:Hazelcast分佈式執行器服務KeyOwner

<T> Future<T> submitToKeyOwner(Callable<T> task, Object key) 
    Submits task to owner of the specified key and returns a Future representing that task. 
     Parameters: 
      task - task 
      key - key 
     Returns: 
      a Future representing pending completion of the task 

我相信,我不是一個人有多個地圖可能實際上不同用途使用相同的密鑰建立集羣,持不同的對象(例如,沿着以下安裝的東西) :

IMap<String, ObjectTypeA> firstMap = HazelcastInstance.getMap("firstMap"); 
IMap<String, ObjectTypeA_AppendixClass> secondMap = HazelcastInstance.getMap("secondMap"); 

對我來說,似乎很混淆文檔說的關鍵所有者。我真正感到沮喪的是,我不知道哪一個 - 它指的是哪個map-key?
文檔也給出了這種方法的「演示」:

import com.hazelcast.core.Member; 
import com.hazelcast.core.Hazelcast; 
import com.hazelcast.core.IExecutorService; 
import java.util.concurrent.Callable; 
import java.util.concurrent.Future; 
import java.util.Set; 
import com.hazelcast.config.Config; 

public void echoOnTheMemberOwningTheKey(String input, Object key) throws Exception { 
    Callable<String> task = new Echo(input); 
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(); 
    IExecutorService executorService = hz.getExecutorService("default"); 
    Future<String> future = executorService.submitToKeyOwner(task, key); 
    String echoResult = future.get(); 
} 

下面是文檔網站的鏈接:Hazelcast MultiHTML Documentation 3.0 - Distributed Execution
沒有任何你們的過去搞清楚它想要什麼鍵?

回答

1

也許我可以用一個代碼示例更好地解釋它:

Callable<String> task = new Echo(input); 

    String key = "foo"; 
    IMap map1 = hz.getMap("m1"); 
    IMap map2 = hz.getMap("m2"); 
    map1.put(key,1); 
    map2.put(key,2); 

    IExecutorService executorService = hz.getExecutorService("default"); 
    Future<String> future = executorService.submitToKeyOwner(task, key); 
    String echoResult = future.get(); 

正如你可以看到有2個地圖,MAP1和MAP2。

這兩個映射都有一個具有相同鍵'foo'但不同值的映射條目。

但是這兩個映射條目最終會在同一個分區中(所以在同一個成員上),因爲密鑰用於確定分區。

在最後幾行中,任務發送給密鑰所有者,在這種情況下,我們將把任務發送給擁有密鑰'foo'的成員。所以這個任務將被髮送到同一臺機器上,因爲這兩個地圖條目都存儲在這個機器上。

我還沒有檢查過您的支持;這是你在Stack Overflow上發佈的原始問題的答案。

0

在Hazelcast中,您可以對數據進行分區並在地圖中使用密鑰。

E.g.當我有兩張使用相同鍵的地圖時,該鍵的地圖條目將被放置在同一分區中的兩張地圖上。

所以使用哪張地圖並不重要。

您可以使用執行程序將任務發送給擁有該分區的成員。

我希望這會回答你的問題,否則請隨時索取更多信息。

+0

這使得絕對零感。那麼,至少我知道這種行爲的優勢。 昨天與Hazelcast支持部門討論這個問題,我的問題的解決方案就暴露了出來。我將不得不使用單獨的IMap來存儲具有objectId對象的hazelcast實例的Id,我正在尋找它。這樣我就可以執行會員(HazelcastInstance.getMembers()。get(id))。 –