如何在 Oracle Data Integrator 12c 中为按字段分组创建字符串

How to make a string for the group by field in Oracle Data Integrator 12c

我是 ODI 12c 的新手。我最近安装了它并对其进行了一些测试。我有一个 table 这样的:

我的目标是根据 customer_id 进行分组并制作一个 JSON 格式字符串对于每个 customer_id。在 Oracle 数据库中,我可以使用下面的查询来做到这一点:

  select customer_id,'[' || listagg('{"TRX_ID":' 
    || '"' || trx_id || '"' || ',"count_rules":' 
    || '"' || count_rules || '"'  
    || '}',',') within group(order by count_rules) || ']' as JSON_RULES
  from (select customer_id,trx_id,count(rules) as count_rules from test_rules group by 
  customer_id,trx_id) group by customer_id

结果是这样的:

但是,我想在 ODI 12c 中做同样的工作,请您指导我如何做?

非常感谢任何帮助。

我对 ODI 的经验不多,但它支持 Oracle 的 JSON SQL 功能吗?

查询

WITH
    test_rules (trx_id, customer_id, rules)
    AS
        (SELECT 1, 'a', 3 FROM DUAL
         UNION ALL
         SELECT 1, 'a', 1 FROM DUAL
         UNION ALL
         SELECT 2, 'a', 5 FROM DUAL
         UNION ALL
         SELECT 2, 'a', 1 FROM DUAL
         UNION ALL
         SELECT 3, 'b', 1 FROM DUAL
         UNION ALL
         SELECT 3, 'b', 2 FROM DUAL
         UNION ALL
         SELECT 3, 'b', 3 FROM DUAL
         UNION ALL
         SELECT 4, 'c', 2 FROM DUAL
         UNION ALL
         SELECT 4, 'c', 3 FROM DUAL
         UNION ALL
         SELECT 5, 'd', 1 FROM DUAL
         UNION ALL
         SELECT 5, 'd', 4 FROM DUAL)
  SELECT customer_id,
         json_arrayagg (json_object (KEY 'TRX_ID' VALUE trx_id, KEY 'count_rules' VALUE count_rules))    AS json_rules
    FROM (  SELECT customer_id, trx_id, COUNT (*) AS count_rules
              FROM test_rules
          GROUP BY customer_id, trx_id)
GROUP BY customer_id;

结果

   CUSTOMER_ID                                                     JSON_RULES
______________ ______________________________________________________________
a              [{"TRX_ID":2,"count_rules":2},{"TRX_ID":1,"count_rules":2}]
b              [{"TRX_ID":3,"count_rules":3}]
c              [{"TRX_ID":4,"count_rules":2}]
d              [{"TRX_ID":5,"count_rules":2}]

ODI 版本 12c (12.1.3) 有一些 JSON 支持。即使你安装的 ODI 的 subversion 不支持 JSON 格式,你仍然可以在数据库中创建一个视图

CREATE OR REPLACE VIEW v_test_rules AS
WITH t AS
(
SELECT JSON_OBJECT(KEY 'trx_id' IS TO_CHAR(trx_id) FORMAT JSON,  
                   KEY 'count_rules' IS TO_CHAR(COUNT(rules)) FORMAT JSON) AS jo,
       customer_id                          
  FROM test_rules 
 GROUP BY customer_id, trx_id
)
SELECT customer_id, JSON_ARRAYAGG( jo ) AS json_rules
  FROM t
 GROUP BY customer_id

然后从 ODI 调用

SELECT *
  FROM v_test_rules

Demo

如果需要,可以对映射中的聚合组件进行参数化以使用自定义 GROUP BY 子句。

下面是如何使用这个组件:

  1. 将聚合组件从组件窗格拖放到映射 canvas
  2. 将 CUSTOMER_ID 列从源数据存储拖到聚合组件
  3. 单击聚合组件,进入属性选项卡并添加一个新列 JSON_RULES
  4. 单击映射中的新属性 canvas 并设置表达式 '[' || listagg('{"TRX_ID":' || '"' || trx_id || '"' || ',"count_rules":' || '"' || count_rules || '"' || '}',',') within group(order by count_rules) || ']'
  5. 默认情况下,聚合组件会将所有属性的 Is Group By 属性 设置为 Auto。 Auto 意味着所有在其表达式中没有聚合函数的属性都将成为 GROUP BY 子句的一部分。所以在你的情况下,只有 CUSTOMER_ID 应该在 GROUP BY 子句中,你应该没问题。如果在GROUP BY子句中误加了JSON_RULES属性,仍然可以将Is Group By属性设置为No.
  6. 如果需要更高级的 GROUP BY 子句,仍然可以在聚合组件的“常规”选项卡中手动设置一个。如果需要,它也是我们可以设置 HAVING 子句的地方

[编辑] 我没有看到你在那里有子查询。您只需要将第一个聚合组件放在我显示的聚合组件之前 CUSTOMER_ID 和 TRX_ID.