Spark-Sql的UDF功能实际运用、Spark-Sql中使用if返回多条件查询结果、Mysql中case-when多条件查询

项目实战之Spark-Sql的UDF功能实际运用、Spark-Sql中使用if返回多条件查询结果、Mysql中case-when多条件查询

1、Spark-Sql的UDF功能实际运用

	通过spark.udf.register("udfName", func) 来进行注册
	使用:select udfName(name) from people 来直接使用

a) 注册UDF方法

val sqlContext = new SQLContext(sc)
sqlContext.udf.register("feiif", (bool: Boolean, right: Double, wrong: Double) => if (bool) right else wrong)

b) sql查询时使用UDF函数

	val sqlContext = new SQLContext(sc)
  val resultQueryfeiIf = sqlContext.sql(
 	 """
        |select substring(requestdate,0,10) date,provincename,cityname,
        |sum(feiif(requestmode=1 and processnode >= 1,1,0)) requestumber,
        |sum(feiif(requestmode=1 and processnode >= 2 ,1,0)) validrequest
        |from RegionalStatisticsKpi group by substring(requestdate,0,10),provincename,cityname
  """.stripMargin)

2、Spark-Sql中使用if返回多条件查询结果

具体用法:select sum(if(条件 满足返回1,不满足返回0)),再对值进行聚合

		   val sqlContext = new SQLContext(sc)
		val resultQueryIf = sqlContext.sql(
	      """
	        |select substring(requestdate,0,10) date,provincename,cityname,
	        |sum(if(requestmode=1 and processnode >= 1,1,0)) requestumber,
	        |sum(if(requestmode=1 and processnode >= 2 ,1,0)) validrequest
	        |from RegionalStatisticsKpi group by substring(requestdate,0,10),provincename,cityname
	      """.stripMargin)

3、Mysql中case-when多条件查询在Spark Sql中的使用

具体用法:case when 条件 then 满足 1 else 不满足 0 end

		使用case when then else end查询结果时需要使用聚合函数
		    val sqlContext = new SQLContext(sc)
			sqlContext.sql(
  //case when 条件 then 1 else 0
  """
select substring(requestdate,0,10) date,provincename,cityname,
	 |sum(case when requestmode=1 and processnode >= 1 then 1 else 0 end ) 		requestumber,
	 |sum(case when requestmode=1 and processnode >= 2 then 1 else 0 end ) validrequest,
	 | sum(case when requestmode=1 and processnode = 3 then 1 else 0 end ) advertisRequest 
	 |from RegionalStatisticsKpi group by substring(requestdate,0,10),provincename,cityname
	    """.stripMargin) 

你可能感兴趣的:(Spark-Sql的UDF功能实际运用、Spark-Sql中使用if返回多条件查询结果、Mysql中case-when多条件查询)