且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JOIN语句

更新时间:2022-09-13 21:39:49

实时计算的join和传统批处理join的语义一致,都用于两张表之间的的join(例如,table a join table b)。不同的是实时计算的table atable b是两张动态表,join的结果也会动态更新以保证最终结果和批处理的一致性。

语法


  1. tableReference [, tableReference ]* | tableexpression
  2. [ LEFT ] JOIN tableexpression [ joinCondition ];

说明

  • 只支持等值连接, 不支持不等连接。
  • 只支持inner join和left outer join两种

注意:DDL声明的字段、主键等要和真实表里面定义的一致。

示例一

测试语句


  1. SELECT o.rowtime, o.productId, o.orderId, o.units,
  2. p.name, p.unitPrice
  3. FROM Orders AS o
  4. JOIN Products AS p
  5. ON o.productId = p.productId;

查询结果

rowtime productId orderId units name unitPrice
10:17:00 30 5 4 Cheese 17
10:17:05 10 6 1 Beer 0.25
10:18:05 20 7 2 Wine 6
10:18:07 30 8 20 Cheese 17
11:02:00 10 9 6 Beer 0.25
11:04:00 10 10 1 Beer 0.25
11:09:30 40 11 12 Bread 100
11:24:11 10 12 4 Beer 0.25

示例二

测试数据

datahub_stream1:

a(bigint) b(bigint) c(VARCHAR)
0 10 test11
1 10 test21

datahub_stream2:

a(bigint) b(bigint) c(VARCHAR)
0 10 test11
1 10 test21
0 10 test31
1 10 test41

测试语句


  1. --创建DataHub table作为数据源1引用
  2. create table datahub_stream1 (
  3. a int,
  4. b int,
  5. c varchar
  6. ) WITH (
  7. type='datahub',
  8. endpoint='',
  9. accessId='',
  10. accessKey='',
  11. projectName='',
  12. topic='',
  13. project=''
  14. );
  15. --创建DataHub表作为数据源2引用
  16. create table datahub_stream2 (
  17. a int,
  18. b int,
  19. c varchar
  20. ) WITH (
  21. type='datahub',
  22. endpoint='',
  23. accessId='',
  24. accessKey='',
  25. projectName='',
  26. topic='',
  27. project=''
  28. );
  29. --用RDS作为结果表
  30. create table rds_output(
  31. s1_c varchar,
  32. s2_c varchar
  33. )with(
  34. type='rds',
  35. url='jdbc:mysql://XXXXXXX3306/test',
  36. tableName='udf',
  37. userName='tXXXXt',
  38. password='XXXX6'
  39. );
  40. insert into rds_output
  41. select
  42. s1.c,s2.c
  43. from datahub_stream1 AS s1
  44. join datahub_stream2 AS s2 on s1.a =s2.a
  45. where s1.a = 0;

测试结果

s1_c(varchar) s2_c(varchar)
test1 test11
test1 test31
本文转自实时计算——JOIN语句