zoukankan      html  css  js  c++  java
  • spark-sql中视图关联表结果不匹配问题

    在sparkSQL 中将计算结果保存为视图,关联其他表后出现结果匹配错误,通过分析发现,是因为sql语句中使用了表达式

    row_number() over(order by 1)

    其实该表达式并没有执行,真正执行的时候是需要触发action (例如 show, count, top .......)算子的。或者在保存为视图之前,将结果持久化到内存中。

    (1)结果不匹配

          println(" --------增加自增列,保存为视图----------- ")
          sqlBF.setLength(0)
          sqlBF.append(" select row_number() over(order by 1) as id, ")
          sqlBF.append("   a.* from students a                       ")
          val addId = sqlC.sql(sqlBF.toString)
          addId.createOrReplaceTempView("temp_addid")
          println(" --------增加自增列成功----------- ")
    
    
          println("---------视图关联其他表-------")
          sqlBF.setLength(0)
          sqlBF.append(" select a.id, a.student_name, a.class_id, ")
          sqlBF.append("        b.class_name, b.teacher,          ")
          sqlBF.append("     from  temp_addid  a,                 ")
          sqlBF.append("           class       b                  ")
          sqlBF.append("  where a.class_id = b.class_id           ")
          val results = sqlC.sql(sqlBF.toString)
    
          results.show()

    (2)带表达式的,在保存为视图之前,将结果持久化到内存中

          println(" --------增加自增列,保存为视图----------- ")
          sqlBF.setLength(0)
          sqlBF.append(" select row_number() over(order by 1) as id, ")
          sqlBF.append("   a.* from students a                       ")
          val addId = sqlC.sql(sqlBF.toString)
          addId.persist() //持久化到内存中
          addId.createOrReplaceTempView("temp_addid")
          println(" --------增加自增列成功----------- ")
    
    
          println("---------视图关联其他表-------")
          sqlBF.setLength(0)
          sqlBF.append(" select a.id, a.student_name, a.class_id, ")
          sqlBF.append("        b.class_name, b.teacher,          ")
          sqlBF.append("     from  temp_addid  a,                 ")
          sqlBF.append("           class       b                  ")
          sqlBF.append("  where a.class_id = b.class_id           ")
          val results = sqlC.sql(sqlBF.toString)
    
          results.show()

    (3)或者直接保存为table

    println(" --------增加自增列,保存为视图----------- ")
          sqlBF.setLength(0)
          sqlBF.append(" select row_number() over(order by 1) as id, ")
          sqlBF.append("   a.* from students a                       ")
          val addId = sqlC.sql(sqlBF.toString)
          hc.saveTable(addId , "temp_addid  ") //保存为table
          println(" --------增加自增列成功----------- ")
    
    
          println("---------table关联其他表-------")
          sqlBF.setLength(0)
          sqlBF.append(" select a.id, a.student_name, a.class_id, ")
          sqlBF.append("        b.class_name, b.teacher,          ")
          sqlBF.append("     from  temp_addid  a,                 ")
          sqlBF.append("           class       b                  ")
          sqlBF.append("  where a.class_id = b.class_id           ")
          val results = sqlC.sql(sqlBF.toString)
    
          results.show()

      如果视图中没有使用类似的计算表达式,不做持久化操作,直接保存为视图,然后关联其他表是不会 影响结果的,使用视图只是将大量的SQL进行分解,简化计算。

  • 相关阅读:
    python下multiprocessing和gevent的组合使用
    TCP的三次握手与四次挥手理解及面试题(很全面)
    Python设计模式
    python定义接口继承类
    pycharm 中自动补全代码提示前符号 p,m ,c,v, f 是什么意思
    21天打造分布式爬虫-urllib库(一)
    redis的使用
    Memcached的使用
    12.Flask-Restful
    11.Flask钩子函数
  • 原文地址:https://www.cnblogs.com/wangleBlogs/p/13212494.html
Copyright © 2011-2022 走看看