Advertisement
elielsonfs

Untitled

Jan 22nd, 2021
1,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.96 KB | None | 0 0
  1. package com.telefonica.sittel.udf
  2.  
  3. import org.apache.spark.sql.api.java.UDF2
  4.  
  5. import scala.util.Try
  6.  
  7. import org.json4s.jackson.JsonMethods
  8. import org.json4s.jackson.JsonMethods._
  9.  
  10. class FilterJSON extends UDF2[String, String, Boolean] {
  11.  
  12.  
  13.     def isEquals(field: String, condition: String, row: JValue, objFilter: JValue, arrResult: List[Boolean]): Boolean = {
  14.         var arrCondition = condition.split(".")
  15.         var value = objFilter \ arrCondition(0)
  16.         if(arrCondition.size == 2) {
  17.             value = objFilter \ arrCondition(0) \ arrCondition(1)
  18.         }
  19.         if(value != JNothing) {
  20.             val rowValue =  row \ field
  21.             if (rowValue != JNothing) {
  22.                 arrResult.append(rowValue.toString().trim().equals(value.toString().trim()))
  23.             }
  24.         }
  25.  
  26.     }
  27.  
  28.  
  29.     override def call(row: String, filter: String): Boolean = {
  30.         val rowJSON    = JsonMethods.parse(row)
  31.         val filterJSON = JsonMethods.parse(filter)
  32.        
  33.         val arrResult: List[Boolean] = List()
  34.         val filtered = for {
  35.             JObject(obj) <- filterJSON
  36.             isEquals("num_terminal_linha_assinante", "numero_terminal", rowJSON, filterJSON, arrResult)
  37.             //isEquals("rpon"             , "numero_terminal", rowJSON, filterJSON, arrResult)
  38.             //isEquals("rpon_migrado_rnc" , "numero_terminal", rowJSON, filterJSON, arrResult)
  39.             //isEquals("designador"       , "numero_terminal", rowJSON, filterJSON, arrResult)
  40.             //isEquals("cpf_cnpj"         , "CNPJ" , rowJSON, filterJSON, arrResult)  
  41.             //isEquals("cpf_cnpj"         , "CPF"  , rowJSON, filterJSON, arrResult)
  42.             //isEquals("iccid"            , "iccid", rowJSON, filterJSON, arrResult)
  43.             val arrDocument: List[Boolean] = List()
  44.             //isEquals("documento_assinante" , "documento.numero", rowJSON, filterJSON, arrDocument)
  45.             //isEquals("tipo_documento"      , "documento.tipo"  , rowJSON, filterJSON, arrDocument)
  46.             //if(arrDocument.size > 0) {
  47.             //    arrResult.append( arrDocument.filter(_ == true) == arrDocument.size )            
  48.             //}
  49.             val arrEndereco: List[Boolean] = List()
  50.             //isEquals("bairro"        , "endereco.bairro",   rowJSON, filterJSON, arrEndereco)
  51.             //isEquals("tipo_endereco" , "endereco.tipo_endereco", rowJSON, filterJSON, arrEndereco)
  52.             //isEquals("endereco"      , "endereco.endereco", rowJSON, filterJSON, arrEndereco)
  53.             //isEquals("uf"            , "endereco.uf",       rowJSON, filterJSON, arrEndereco)
  54.             //isEquals("cep"           , "endereco.cep",      rowJSON, filterJSON, arrEndereco)
  55.             //if(arrEndereco.size > 0) {
  56.             //    arrResult.append( arrDocument.filter(_ == true) == arrDocument.size )
  57.             // }
  58.  
  59.             JField(res) <- arrResult.filter(_ == true).size == arrResult.size
  60.         } yield res
  61.        
  62.         return res
  63.   }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement