当前位置: 首页 > 知识库问答 >
问题:

Hazelcast Jet Query

孙泉
2023-03-14

关于Hazelcast Jet我有以下问题

还有另一个应用程序(应用程序B,部署在集群中)实例化JetInstance并在每个节点上单独运行作业以处理记录。

目前,该作业从事件日记中读取数据并添加到IList中(reference-hazelcast-jet-0.5.1\code-samples\streaming\map-journal-source.java)

由于作业在多个节点上运行,事件日记中的记录由多个节点处理。这会导致ILIST中有多个条目。

下面是一个示例代码(应用程序B)

        Pipeline p = Pipeline.create();
        p.drawFrom(Sources.<Integer, Integer, Integer>remoteMapJournal(MAP_NAME, clientConfig,
                e -> e.getType() == EntryEventType.ADDED, EventJournalMapEvent::getNewValue, true))
         .peek()
         .drainTo(Sinks.list(SINK_NAME));

        JobConfig jc= new JobConfig();
        jc.setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE);

        localJet.newJob(p,jc);

下面是一个完整的代码。

应用程序的源代码。

public class RemoteMapJournalSourceSrv1 {

private static final String MAP_NAME = "map";
private static final String SINK_NAME = "list";

public static void main(String[] args) throws Exception {
    System.setProperty("remoteHz.logging.type", "log4j");

    Config hzConfig = getConfig();
    HazelcastInstance remoteHz = startRemoteHzCluster(hzConfig);

    try {
        IMap<Integer, Integer> map = remoteHz.getMap(MAP_NAME);
        System.out.println("*************** Initial Map  address " +  map.size() );

        while(true) {
            System.out.println("***************map size "+map.size());  
            TimeUnit.SECONDS.sleep(20);
        }

    } finally {
        Hazelcast.shutdownAll();
    }
}

private static HazelcastInstance startRemoteHzCluster(Config config) {
    HazelcastInstance remoteHz = Hazelcast.newHazelcastInstance(config);
    return remoteHz;
}

private static Config getConfig() {
    Config config = new Config();
    // Add an event journal config for map which has custom capacity of 1000 (default 10_000)
    // and time to live seconds as 10 seconds (default 0 which means infinite)
    config.addEventJournalConfig(new EventJournalConfig().setEnabled(true)
                                                         .setMapName(MAP_NAME)
                                                         .setCapacity(10000)
                                                         .setTimeToLiveSeconds(100));
    return config;
}
public class RemoteMapJournalSourceCL1 {

private static final String MAP_NAME = "map";
private static final String SINK_NAME = "list";

public static void main(String[] args) throws Exception {
    System.setProperty("remoteHz.logging.type", "log4j");

      JetInstance localJet = startLocalJetCluster();

    try {
        ClientConfig clientConfig = new ClientConfig();
        GroupConfig groupConfig = new GroupConfig();

        clientConfig.getNetworkConfig().addAddress("localhost:5701");
        clientConfig.setGroupConfig(groupConfig);

        IList list1 = localJet.getList(SINK_NAME);

        int size1 = list1.size();

        System.out.println("***************List Initial size "+size1);

        Pipeline p = Pipeline.create();
        p.drawFrom(Sources.<Integer, Integer, Integer>remoteMapJournal(MAP_NAME, clientConfig,
                e -> e.getType() == EntryEventType.ADDED, EventJournalMapEvent::getNewValue, false))
         .peek()
         .drainTo(Sinks.list(SINK_NAME));

        JobConfig jc= new JobConfig();
        jc.setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE);
        localJet.newJob(p,jc);

        while(true){
            TimeUnit.SECONDS.sleep(10); 
            System.out.println("***************Read " + list1.size() + " entries from remote map journal.");
        }           

    } finally {
        Hazelcast.shutdownAll();
        Jet.shutdownAll();
    }

}

private static String getAddress(HazelcastInstance remoteHz) {
    Address address = remoteHz.getCluster().getLocalMember().getAddress();
    System.out.println("***************Remote address " + address.getHost() + ":" + address.getPort() );
    return address.getHost() + ":" + address.getPort();
}

private static JetInstance startLocalJetCluster() {
      JetInstance localJet = Jet.newJetInstance();
    return localJet;
}
public class RemoteMapJournalSourceCL2 {

private static final String MAP_NAME = "map";
private static final String SINK_NAME = "list";

public static void main(String[] args) throws Exception {
    System.setProperty("remoteHz.logging.type", "log4j");

      JetInstance localJet = startLocalJetCluster();

    try {
        ClientConfig clientConfig = new ClientConfig();
        GroupConfig groupConfig = new GroupConfig();

        clientConfig.getNetworkConfig().addAddress("localhost:5701");
        clientConfig.setGroupConfig(groupConfig);

        IList list1 = localJet.getList(SINK_NAME);
        int size1 = list1.size();
        System.out.println("***************List Initial size "+size1);


        Pipeline p = Pipeline.create();
        p.drawFrom(Sources.<Integer, Integer, Integer>remoteMapJournal(MAP_NAME, clientConfig,
                e -> e.getType() == EntryEventType.ADDED, EventJournalMapEvent::getNewValue, true))
         .peek()
         .drainTo(Sinks.list(SINK_NAME));

        JobConfig jc= new JobConfig();
        jc.setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE);

        localJet.newJob(p,jc);

        while(true){
            TimeUnit.SECONDS.sleep(10); 
            System.out.println("***************Read " + list1.size() + " entries from remote map journal.");
        }           
    } finally {
        Hazelcast.shutdownAll();
        Jet.shutdownAll();
    }

}
private static JetInstance startLocalJetCluster() {
      JetInstance localJet = Jet.newJetInstance();
    return localJet;
}
public class HZClient {

  public static void main(String[] args) {

    ClientConfig clientConfig = new ClientConfig();
    GroupConfig groupConfig = new GroupConfig();

    clientConfig.getNetworkConfig().addAddress("localhost:5701");
    clientConfig.setGroupConfig(groupConfig);

    HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    IMap<Integer, Integer> map = client.getMap("map");
    Scanner in = new Scanner(System.in);
    int startIndex= 0;
    int endIndex= 0;

    while(true) {
        if(args !=null && args.length > 0 && args[0].equals("BATCH")) {

            System.out.println("Please input the batch size");
            int b = in.nextInt();
            startIndex= endIndex + 1;
            endIndex+= b;
            System.out.println("Batch starts from  "+ startIndex +"ends at"+endIndex);
            putBatch(map,startIndex,endIndex);

        }
        else {
            System.out.println("Please input the map entry");
            int a = in.nextInt();
            System.out.println("You entered integer "+a);
            put(map,a,a);
        }
    }
}

public static void putBatch(IMap map,int startIndex, int endIndex) {
    int index= startIndex;
    System.out.println("Start Index" + startIndex +"End Index"+endIndex );
    while(index<=endIndex){
        System.out.println("Map Values"+ index);
        put(map,index,index);
        index+=1;
    }

}

public static void put(IMap map,int key,int value) {
    map.set(key, value);
}

运行应用程序B节点2-Java程序RemoteMapJournalSourceCL2

为应用程序A-Java程序HZClient运行Hazelcast客户端

此客户端程序根据控制台输入将条目放入映射中。请提供整数输入。

在执行时,peek()记录应用程序B的两个节点的值,在应用程序A映射中插入1个条目时,列表计数变为2。

共有1个答案

常哲彦
2023-03-14

您似乎提交了来自两个Jet客户机的两个独立作业。每个作业接收所有IMap事件日记项,并将它们推送到同一个IList,因此,预期的结果是IList包含每个项的两个实例。

请记住,您只从Jet客户机提交作业,但它实际上在Jet集群中同时在所有成员上运行。如果只想要接收器中数据的一个副本,则不要将同一作业提交两次。

 类似资料:

相关问答

相关文章

相关阅读