如何在spark中实现多条件试捕

hkmswyz6  于 2021-05-27  发布在  Spark
关注(0)|答案(2)|浏览(471)

下面的spark代码用于创建datapipeline。

  1. package Test
  2. import org.apache.log4j.{Level, Logger}
  3. import org.apache.spark.sql.SparkSession
  4. import org.apache.spark.sql.functions._
  5. import org.apache.spark.sql.expressions.Window
  6. object myjson {def main(args: Array[String]): Unit = {
  7. val spark = SparkSession.builder()
  8. .appName("Readfile")
  9. .config("spark.driver.memory", "2g")
  10. .master("local[*]")
  11. //.enableHiveSupport()
  12. .getOrCreate()
  13. import spark.implicits._
  14. val df = spark.read.option("multiLine", true).json("D:mypathTest/myfile.json")
  15. df.printSchema()
  16. val newdf = ds.withColumn("upTime",regexp_replace(col("upTime"),"[a-zA-Z]","")).
  17. }
  18. }

有没有办法在scala中创建日志记录和警报机制。或者如何实现错误处理,比如如果文件不在路径错误中。请帮帮我。

rqdpfwrv

rqdpfwrv1#

对于错误处理,可以使用 try/catch 声明。https://alvinalexander.com/scala/scala-try-catch-finally-syntax-examples-exceptions-wildcard/
对于日志记录,可以使用log4j。 https://logging.apache.org/log4j/2.x/manual/scala-api.html

hmtdttj4

hmtdttj42#

在spark read api中传递无效源时引发invalidinputexception。。。
您可以使用下面这样的scala代码

  1. try{
  2. // reading through spark
  3. }catch{
  4. case filenotfound : InvalidInputException => {log.error("please check input ",filenotfound)
  5. handleException()
  6. }
  7. case others : Exception => handleException()
  8. }
  9. def handleException() = {
  10. // have a notification system like AWS SES or some other alerting systems here
  11. }

对于日志记录,可以使用log4j框架,创建日志对象并使用它记录错误。

展开查看全部

相关问题