【问题标题】:Apache Ignite Continuous Queries : How to get the field names and field values in the listener updates when there are dynamic fields?Apache Ignite Continuous Queries:当有动态字段时,如何获取侦听器更新中的字段名称和字段值?
【发布时间】:2018-11-05 01:36:45
【问题描述】:

我正在研究是否应该继续将 Apache Ignite 用于商业和企业用途。虽然我们正在尝试找到一个用例,但有一个用例。

前提条件

动态创建表,即可能会有新字段被放入缓存中。这意味着没有定义表/缓存属性的预编译 POJO(Model)。

用例

我想编写一个 SELECT 连续查询,它可以为我提供修改后的结果。所以我写了那个查询,但问题是当侦听器收到通知时,我无法找到从任何方法调用修改的所有字段名称。我希望能够在某种 Map 中获取所有字段名称和字段值,我可以使用它们然后提交给其他系统。

【问题讨论】:

    标签: sql ignite


    【解决方案1】:

    您可以使用二进制对象和连续查询来跟踪所有修改的字段值:

        IgniteCache<Integer, BinaryObject> cache = ignite.cache("person").withKeepBinary();
    
        ContinuousQuery<Integer, BinaryObject> query = new ContinuousQuery<>();
        query.setLocalListener(events -> {
            for (CacheEntryEvent<? extends Integer, ? extends BinaryObject> event : events) {
    
                BinaryType type = ignite.binary().type("Person");
                if (event.getOldValue() != null && event.getValue() != null) {
    
                    HashMap<String,Object> oldProps = new HashMap<>();
                    HashMap<String,Object> newProps = new HashMap<>();
    
                    for (String field : type.fieldNames()) {
                        oldProps.put(field,event.getOldValue().field(field));
                        newProps.put(field,event.getValue().field(field));
                    }
    
                    com.google.common.collect.MapDifference<Object, Object> diff = com.google.common.collect.Maps.difference(oldProps, newProps);
    
                    System.out.println(diff.entriesDiffering());
                }
            }
        });
        cache.query(query);
    
        cache.put(1, ignite.binary().builder("Person").setField("name","Alice").build());
        cache.put(1, ignite.binary().builder("Person").setField("name","Bob").build());
    

    【讨论】:

    • 谢谢帕维尔。我想知道是否可以让侦听器针对特定的 SQL 查询执行。例如,假设我想让此侦听器仅针对如下查询运行:“SELECT * FROM CACHE_1 p1 inner join CACHE_2 p2 on (p1.id = p2.id)”。可能吗?还有 cache.put(1, ignite.binary().builder("Person").setField("name","Alice").build());它不会把它放在 SQL 表中。我有 3 个重要用途: 1> 将数据插入 SQL。 2> 连续查询特定的 SQL 选择监听更新 3> 偶尔能够从外部客户端运行 SELECT。
    • 1.请尝试通过apacheignite-sql.readme.io/docs/getting-started 中描述的sql 连接创建表。 2. 无法监听特定查询的事件。仅通过 ContinuousQuery#setRemoteFilterFactory 3 指定事件触发器。您可以使用 sql 连接来处理缓存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多