首页 文章

Apache Ignite在分布式缓存中存储JSON Map

提问于
浏览
3

我编写了下面的代码来从JSON文件中读取数据并将其推送到Ignite分布式缓存中,这段代码运行正常,但是,创建“ContainerAgg”类的要求对我来说是一个问题 . 我们的数据结构不是预定义的提取,它是根据用户选择动态生成的 .

我尝试使用BinaryObject但是当我使用BinaryObject时我无法运行SQL查询,您是否有任何使用BinaryObject的示例,并且不使用预编译的java类作为模式 .

有这个“StreamVisitorExample”,但是,它使用预编译的Java类(Instrument.class)

public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
      if (!ExamplesUtils.hasServerNodes(ignite))
        return;

      CacheConfiguration<String, ContainerAgg> config = new CacheConfiguration<>(MASSIVE_CACHE);

      config.setIndexedTypes(String.class, ContainerAgg.class);

      ignite.getOrCreateCache(config);


      try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {

        IgniteCache<String, ContainerAgg> cache = Ignition.ignite().cache(MASSIVE_CACHE);

        String line = null;
        Long cnt = 0L;
        while ((line = br.readLine()) != null) {
          ContainerAgg inst = mapper.readValue(line, ContainerAgg.class);
          cache.put(cnt.toString(), inst);
          cnt++;
        }


        long startTime = System.currentTimeMillis();
        String sql =
            "SELECT SFID, LABEL, PARTID, PROVIDERID, SUM(TOTALCNT) "
            + "FROM CONTAINERAGG GROUP BY SFID, LABEL, PARTID, PROVIDERID";

        QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(sql));
        long endTime = System.currentTimeMillis();

        for (List<?> row : cursor.getAll()){
          System.out.println(row);
        }
        System.out.println("Total Time: " + (endTime - startTime));

      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

}

为您提供有关 BinaryObject 所做事情的更多背景信息 . 我将JSON转换为 map 并在 BinaryObjectBuilder 中添加了每个条目并创建了 BinaryObject 实例并将其存储在 IgniteCache<String, BinaryObject>

1 回答

相关问题