运行Ignite流媒体字数例

时间:2016-12-08 03:59:15

标签: ignite

我正在执行https://apacheignite.readme.io/docs/streaming-example中的步骤。探索Ignite流式字数统计示例。当我运行查询时,不会查询任何数据。 以下是我的代码:

public class CacheConfig {

    public static CacheConfiguration<String, Long> createWordCache() {
        CacheConfiguration<String, Long> cfg = new CacheConfiguration<String, Long>("words");
        cfg.setIndexedTypes(String.class, Long.class);
        return cfg;
    }
}


public class WorkStreamIntoCache {
    public static void main(String[] args) throws Exception {
        Ignition.setClientMode(true);
        String configPath = "D:/apache-ignite-fabric-1.7.0-bin/config/default-config.xml";
        Ignite ignite = Ignition.start(configPath);
        IgniteCache<String, Long> cache = ignite.getOrCreateCache(CacheConfig.createWordCache());

        IgniteDataStreamer<String, Long> streamer = ignite.dataStreamer(cache.getName());

        streamer.allowOverwrite(true);


        streamer.receiver(StreamTransformer.from(new CacheEntryProcessor<String, Long, Object>() {

            public Object process(MutableEntry<String, Long> mutableEntry, Object... objects) throws EntryProcessorException {
                Long preValue = mutableEntry.getValue();
                if (preValue == null) {
                    mutableEntry.setValue(1L);
                } else {
                    mutableEntry.setValue(preValue + 1);
                }
                return null;
            }
        }));


        while (true) {
            InputStream in = WorkStreamIntoCache.class.getResourceAsStream("/words.txt");
            List<String> lines = IOUtils.readAsLines(in);
            for (String line : lines) {
                String[] words = line.split(" ");
                for (String word : words) {
                    word = word.trim();
                    if (word != null && word.length() > 0) {
                        streamer.addData(word, 1L);
                        System.out.println(word + " is writing to the cache @" + new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date()));
                    }
                }
            }
            Thread.sleep(120 * 1000);
        }

    }
}

public class QueryWords {
    public static void main(String[] args) throws Exception {
        Ignition.setClientMode(true);

        String configPath = "D:/apache-ignite-fabric-1.7.0-bin/config/default-config.xml";
        Ignite ignite = Ignition.start(configPath);
        IgniteCache<String, Long> cache = ignite.getOrCreateCache(CacheConfig.createWordCache());

        SqlFieldsQuery sql = new SqlFieldsQuery("select * from Long");

        while (true) {
            List<List<?>> data = cache.query(sql).getAll();
            if (data == null || data.size() <= 0) {
                System.out.println("No data is found in the cache");
            }
            for (List<?> datum : data) {
                for (Object record : datum) {
                    System.out.println("record: " + record);
                }
            }
            Thread.sleep(3000);
        }
    }
}

运行示例的步骤:

  1. 首先,我使用bin/ignite.bat D:/apache-ignite-fabric-1.7.0-bin/config/default-config.xml
  2. 启动点火服务器
  3. 然后在IDEA ide中,我运行WorkStreamIntoCache程序,我可以看到单词写入缓存的日志
  4. 当我运行QueryWords时,我无法查询数据,只能在控制台中找到No data is found in the cache
  5. 问题出在哪里有帮助?谢谢!

0 个答案:

没有答案