MySQL MaxCompute与AnalyticDB实现数据处理与转换过程

Crq
Crq
管理员
1721
文章
0
粉丝
Linux教程评论22字数 619阅读2分3秒阅读模式
摘要AnalyticDB MySQL(简称ads)与 MaxCompute(简称odps)进行数据转换时,个别语法有差别,记录下来,方便备查,文中通过示例代码介绍的非常详细,对大家的学...
1、行转列函数

odps 中行转列函数 wm_concat,将列信息转成行显示,可指定分隔符。

语法格式

wm_concat(separator, string) ,其中separator为分隔符,支持字符串去重,即distinct.

select wm_concat('|',sku_id)
from t_business_order_odps
where
create_time >= '2022-09-30 00:00:00';

ads 中行转列函数 group_concat,即将列信息转成行显示,默认逗号分隔。如在mysql中查询表的所有列名。

select group_concat(COLUMN_NAME)
from information_schema.COLUMNS
where
TABLE_NAME = 't_business_order';
2、判空函数

odps 中判空函数 coalesce ,如若无运费,则显示为0。

select coalesce(freight_amount,0)
from t_business_order_odps
where
create_time >= '2022-09-30 00:00:00';

odps 中判断函数 decode ,如若无运费,则显示为0。

语法格式

decode(expression, search, result[, search, result]…[, default])

表示:三元运算符,即实现IF-THEN-ELSE分支选择的功能

select decode(order_amount,freight_amount,0)
from t_business_order_odps
where
create_time >= '2022-09-30 00:00:00';

ads 中判空函数 ifnull,如若无运费,则显示为0。

select ifnull(freight_amount,0)
from t_business_order
where
create_time >= '2022-09-30 00:00:00';
3、时间函数

odps 中 时间字段类型为 datetime,获取当前时间函数为 getdate()。

select coalesce(freight_amount,0),getdate() as etl_time
from t_business_order_odps
where
create_time >= '2022-09-30 00:00:00';

ads 中时间字段类型为 timestamp,获取当前时间函数为 now()。

select ifnull(freight_amount,0),now() as etl_time
from t_business_order
where
create_time >= '2022-09-30 00:00:00';
4、建表语句

odps 中 字符串字段类型为 string,时间字段类型为 datetime。

CREATE TABLE t_business_order_odps (
    prov_name string COMMENT '省名称',
    city_name string COMMENT '地市名',
    cty_name string COMMENT '区县名称',
    order_no string COMMENT '订单号',
    order_amount bigint COMMENT '运费',
    freight bigint COMMENT '运费',
    insert_month bigint COMMENT '插入月份',
    created_time datetime COMMENT '创建时间'
)
COMMENT '业务订单主表;

ads 中字符串字段类型为 varchar,时间字段类型为 timestamp。

CREATE TABLE t_business_orde (
    prov_name varchar COMMENT '省名称',
    city_name varchar COMMENT '地市名',
    cty_name varchar COMMENT '区县名称',
    order_no varchar COMMENT '订单号',
    order_amount bigint COMMENT '运费',
    freight bigint COMMENT '运费',
    insert_month bigint COMMENT '插入月份',
    created_time timestamp COMMENT '创建时间'
)
PARTITION BY HASH KEY (order_no) PARTITION NUM 128
TABLEGROUP ads_sc_rpt
OPTIONS (UPDATETYPE='batch')
COMMENT '业务订单主表';

知识梳理

OPTIONS 类型有两种

1)batch 批量表,即数据批量入库,适用于从odps同步到ads;

2)realtime 实时表,即数据单条入库,适用于日志等信息单条入库。

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2025年2月17日 09:24:36
  • 转载请注明:https://www.cncrq.com/13089.html
实用!开发者的 Vim 插件(一) Linux教程

实用!开发者的 Vim 插件(一)

作为 Vi 的升级版,Vim 毫无争议是 Linux 中最受欢迎的命令行编辑器之一。除了是一个多功能编辑器外,世界各地的软件开发者将 Vim 当做 IDE(集成开发环境Integr...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证