ここでは、Realtime Compute を使用して、リアルタイムの状況認識と注文の地理的分布を実装する方法を説明するユースケースを示します。

概要

リアルタイムの状況認識と注文の地理的分布により、企業はプロダクトカテゴリの割り当てとリリースをタイムリーに最適化できます。 次のユースケースでは、食品の E コマース企業が Realtime Compute を使用して、リアルタイムの状況認識と注文の地理的分布を実装する方法について説明します。

ユースケース

コアロジックに焦点を当てるため、注文のデータ形式を単純化して、ユースケースに関連する属性のみを保持することとします。
  • データを格納する複数のテーブルを作成します。

    E コマースシステムでは、注文と配送先住所が別々に保存されます。 購買者は複数の配送先住所を使用して注文できます。 注文の作成時に配送先住所は指定されません。 特定の配送先住所は、注文を送信したときにのみ取得できます。 配送先住所テーブルには、都市 ID (city_idで指定) が格納されています。 また、都市に関する地理情報を格納するための都市テーブルを作成する必要があります。 これらのテーブルを作成して、さまざまな地域での注文の分布 (GMV) に関する統計を日ごとに収集します。

    • 注文テーブルを作成します。
      CREATE TABLE source_order (
          id VARCHAR, -- The ID of the order.
          seller_id VARCHAR, -- The ID of the seller.
          account_id VARCHAR, -- The ID of the buyer.
          receive_address_id VARCHAR, -- The ID of the shipping address.
          total_price VARCHAR, -- The order amount.
          pay_time VARCHAR -- The payment time of the order.
      ) WITH (
          type='datahub',
          endPoint='http://dh-cn-hangzhou.aliyun-inc.com',
          project='yourProjectName', -- Your project name.
          topic='yourTopicName', -- Your topic name.
          roleArn='yourRoleArn', -- Your role ARN.
          batchReadSize='500'
      );
    • 配送先住所テーブルを作成します。
      CREATE TABLE source_order_receive_address ( 
           id VARCHAR, -- The ID of the shipping address. 
           full_name VARCHAR, -- The full name of the consignee. 
           mobile_number VARCHAR, -- The mobile number of the consignee. 
           detail_address VARCHAR, -- The detailed shipping address. 
           province VARCHAR, -- The province in the shipping address. 
           city_id VARCHAR, -- The city in the shipping address. 
           create_time VARCHAR -- The time when the order was created. 
       ) WITH ( 
           type='datahub', 
           endPoint='http://dh-cn-hangzhou.aliyun-inc.com', 
           project='yourProjectName', -- Your project name. 
           topic='yourProjectName', -- Your topic name. 
           roleArn='yourProjectName', -- Your role ARN. 
           batchReadSize='500' 
       );                             
    • 都市テーブルを作成します。
      CREATE TABLE dim_city ( 
           city_id varchar, 
           city_name varchar, -- The name of the city. 
           province_id varchar, -- The ID of the province to which the city belongs. 
           zip_code varchar, -- The postal code. 
           lng varchar, -- The longitude of the city. 
           lat varchar, -- The latitude of the city. 
        PRIMARY KEY (city_id), 
        PERIOD FOR SYSTEM_TIME -- Specify that this is a dimension table. 
       ) WITH ( 
           type= 'rds', 
           url = 'yourDatabaseURL', -- Your database URL. 
           tableName = 'yourTableName', -- Your table name. 
           userName = 'yourDatabaseName', -- Your username. 
           password = 'yourDatabasePassword' -- Your password. 
       );
    • さまざまな地域の注文の分布 (GMV) に関する統計を日ごとに収集します。
      CREATE TABLE result_order_city_distribution ( 
           summary_date bigint, -- The date when the statistics are collected. 
           city_id bigint, -- The ID of the city. 
           city_name varchar, -- The name of the city. 
           province_id bigint, -- The ID of the province to which the city belongs. 
           gmv double, -- The GMV. 
           lng varchar, -- The longitude of the city. 
           lat varchar, -- The latitude of the city. 
           primary key (summary_date,city_id) 
          ) WITH ( 
              type= 'rds',     
              url = 'yourDatabaseURL', -- Your database URL. 
              tableName = 'yourTableName', -- Your table name. 
              userName = 'yourDatabaseName', -- Your username. 
              password = 'yourDatabasePassword' -- Your password. 
          );
  • ビジネスロジックを編集します。
     insert into result_order_city_distribution 
     select 
     d.summary_date 
     ,cast(d.city_id as BIGINT) 
     ,e.city_name 
     ,cast(e.province_id as BIGINT) 
     ,d.gmv 
     ,e.lng 
     ,e.lat 
     ,e.lnglat 
     from 
     ( 
             select 
             DISTINCT 
             DATE_FORMAT(a.pay_time,'yyyyMMdd') as summary_date 
             ,b.city_id as city_id 
             ,round(sum(cast(a.total_price as double)),2) as gmv 
             from source_order as a 
             join source_order_receive_address as b on a.receive_address_id =b.id 
             group by DATE_FORMAT(a.pay_time,'yyyyMMdd'),b.city_id 
             -- Join the order table with the shipping address table and compute the sales distribution by date and city ID. 
     )d join dim_city FOR SYSTEM_TIME AS OF PROCTIME() as e on d.city_id = e.city_id 
     -- Join the dimension table to complete the geographic information about cities and obtain the final computing result. 
     ;