加入收藏 | 设为首页 | 会员中心 | 我要投稿 鹤壁站长网 (https://www.0392zz.cn/)- 分布式云、存储数据、视频终端、媒体处理、内容创作!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

Apache Flink 漫谈系列 - SQL概览

发布时间:2018-11-14 23:44:22 所属栏目:教程 来源:孙金城
导读:一、SQL简述 SQL是Structured Query Language的缩写,最初是由美国计算机科学家Donald D. Chamberlin和Raymond F. Boyce在20世纪70年代早期从 Early History of SQL 中了解关系模型后在IBM开发的。该版本最初称为[SEQUEL: A Structured English Query Lang

ScalarFunction#eval()`

  1. class MySplit extends TableFunction[String] { 
  2. def eval(str: String): Unit = { 
  3. if (str.contains("#")){ 
  4. str.split("#").foreach(collect) 
  5. } 
  6. } 
  7.  
  8. def eval(str: String, prefix: String): Unit = { 
  9. if (str.contains("#")) { 
  10. str.split("#").foreach(s => collect(prefix + s)) 
  11. } 
  12. }} 

b. 使用

  1. ... 
  2. val fun = new MySplit() 
  3. tEnv.registerFunction("mySplit", fun) 
  4. val sql = "SELECT c, s FROM MyTable, LATERAL TABLE(mySplit(c)) AS T(s)" 
  5. ... 

3. UDAF

a. 定义

UDAF 要实现的接口比较多,我们以一个简单的CountAGG为例,做简单实现如下:

  1. /** The initial accumulator for count aggregate function */ 
  2. class CountAccumulator extends JTuple1[Long] { 
  3. f0 = 0L //count 
  4. } 
  5.  
  6. /** 
  7. * User-defined count aggregate function 
  8. */ 
  9. class MyCount 
  10. extends AggregateFunction[JLong, CountAccumulator] { 
  11.  
  12. // process argument is optimized by Calcite. 
  13. // For instance count(42) or count(*) will be optimized to count(). 
  14. def accumulate(acc: CountAccumulator): Unit = { 
  15. acc.f0 += 1L 
  16. } 
  17.  
  18. // process argument is optimized by Calcite. 
  19. // For instance count(42) or count(*) will be optimized to count(). 
  20. def retract(acc: CountAccumulator): Unit = { 
  21. acc.f0 -= 1L 
  22. } 
  23.  
  24. def accumulate(acc: CountAccumulator, value: Any): Unit = { 
  25. if (value != null) { 
  26. acc.f0 += 1L 
  27. } 
  28. } 
  29.  
  30. def retract(acc: CountAccumulator, value: Any): Unit = { 
  31. if (value != null) { 
  32. acc.f0 -= 1L 
  33. } 
  34. } 
  35.  
  36. override def getValue(acc: CountAccumulator): JLong = { 
  37. acc.f0 
  38. } 
  39.  
  40. def merge(acc: CountAccumulator, its: JIterable[CountAccumulator]): Unit = { 
  41. val iter = its.iterator() 
  42. while (iter.hasNext) { 
  43. acc.f0 += iter.next().f0 
  44. } 
  45. } 
  46.  
  47. override def createAccumulator(): CountAccumulator = { 
  48. new CountAccumulator 
  49. } 
  50.  
  51. def resetAccumulator(acc: CountAccumulator): Unit = { 
  52. acc.f0 = 0L 
  53. } 
  54.  
  55. override def getAccumulatorType: TypeInformation[CountAccumulator] = { 
  56. new TupleTypeInfo(classOf[CountAccumulator], BasicTypeInfo.LONG_TYPE_INFO) 
  57. } 
  58.  
  59. override def getResultType: TypeInformation[JLong] = 
  60. BasicTypeInfo.LONG_TYPE_INFO} 

(编辑:鹤壁站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读