Ignite dataStreamer无法正常工作

时间:2016-12-12 11:19:27

标签: ignite

我正在使用以下代码探索Ignite dataStreamer。 但输出是:

对于MessageKey0001,输出all显示的数据为null。 对于MessageKey0003,输出也显示所有数据都为空 对于MessageKey0002,输出不显示任何内容,看起来没有运行接收器代码

当我改变

dataStreamer.addData(i, "data-" + i);

    IgniteFuture future = dataStreamer.addData(i, "data-" + i);
    future.get();

future.get()没有返回,看起来addData还没有完成? 我不确定问题出在哪里,有人可以看看吗?谢谢!

package ignite.streamer;

import org.apache.ignite.*;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.stream.StreamReceiver;

import javax.cache.Cache;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

class IgniteDataStreamer_Person implements Serializable {
    @QuerySqlField(index = true)
    private String name;

    @QuerySqlField(index = true)
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class IgniteDataStreamerTest {
    public static void main(String[] args) {
        String configPath = "D:/Software/apache-ignite-fabric-1.7.0-bin/apache-ignite-fabric-1.7.0-bin/config/default-config.xml";
        Ignite ignite = Ignition.start(configPath);
        CacheConfiguration<Integer, String> cfg = new CacheConfiguration<Integer, String>();
        String cacheName = "stream_cache";
        cfg.setName(cacheName);
        cfg.setIndexedTypes(Integer.class, IgniteDataStreamer_Person.class);
        Cache cache = ignite.getOrCreateCache(cfg);

        IgniteDataStreamer<Integer, String> dataStreamer = ignite.dataStreamer(cacheName);

        for (int i = 0; i < 3; i++) {
            dataStreamer.addData(i, "data-" + i);
        }

        //null is got from cache
        for (int i = 0; i < 3; i++) {
            System.out.println(String.format("0001: data is %s ", cache.get(i)));
        }

        dataStreamer.receiver(new StreamReceiver<Integer, String>() {
            public void receive(IgniteCache<Integer, String> cache, Collection<Map.Entry<Integer, String>> entries) throws IgniteException {
                //nothing is printed to console
                for (Map.Entry<Integer, String> entry : entries) {
                    System.out.println(String.format("0002: key is: %s, value is: %s", entry.getKey(), entry.getValue()));
                }
            }
        });

        //null is got from cache
        for (int i = 0; i < 3; i++) {
            System.out.println(String.format("0003: data is %s ", cache.get(i)));
        }
        ignite.close();
    }
}

1 个答案:

答案 0 :(得分:1)

DataStreamer使用批次以提供良好的性能。您应该在阻止flush()方法之前刷新案例中的数据(使用future.get()方法)。

请参阅IgniteDataStreamer javadocs了解详情。