cube.js data Blending

cube.js 的 data Blending,主要是解决多数据schema 数据集成(数据charts的集成)的问题
官方的解决方法还是比较简单的,核心是使用了sql 的union all 同时引用了数据schema 的sql
同时cube.js 也提供了多查询的处理,会自己进行转换处理

参考

  • 数据schema
 
cube(`AllSales`, {
 sql: `
 select amount, user_id as customer_id, created_at, 'Transactions' row_type from ${Transactions.sql()}
 UNION ALL
 select amount, customer_id, created_at, 'Orders' row_type from ${Orders.sql()}
 `,
 measures: {
   customerCount: {
     sql: `customer_id`,
     type: `countDistinct`
   },
   revenue: {
     sql: `amount`,
     type: `sum`
   },
   onlineRevenue: {
     sql: `amount`,
     type: `sum`,
     filters: [{ sql: `${CUBE}.row_type = 'Transactions'` }]
   },
   offlineRevenue: {
     sql: `amount`,
     type: `sum`,
     filters: [{ sql: `${CUBE}.row_type = 'Orders'` }]
   },
   onlineRevenuePercentage: {
     sql: `${onlineRevenue} / NULLIF(${onlineRevenue} + ${offlineRevenue}, 0)`,
     type: `number`,
     format: `percent`
   }
 },
 dimensions: {
   createdAt: {
     sql: `created_at`,
     type: `time`
   },
   revenueType: {
     sql: `row_type`,
     type: `string`
   }
 }
}); 
  • api 查询
import cubejs from '@cubejs-client/core';
const API_URL = 'http://localhost:4000';
const CUBEJS_TOKEN = 'YOUR_TOKEN';
const cubejsApi = cubejs(CUBEJS_TOKEN, {
  apiUrl: `${API_URL}/cubejs-api/v1`
});
const queries = [
  {
    measures: ['Transactions.revenue'],
    timeDimensions: [
      {
        dimension: 'Transactions.createdAt',
        granularity: 'day',
        dateRange: ['2020-08-01', '2020-08-07']
      }
    ]
  },
  {
    measures: ['Orders.revenue'],
    timeDimensions: [
      {
        dimension: 'Orders.createdAt',
        granularity: 'day',
        dateRange: ['2020-08-01', '2020-08-07']
      }
    ]
  }
];
const resultSet = await cubejsApi.load(queries);

参考资料

https://cube.dev/docs/data-blending

posted on 2021-01-01 23:18  荣锋亮  阅读(189)  评论(0编辑  收藏  举报

导航