栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据

scala递归的方法快速解析Json数据

大数据 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

scala递归的方法快速解析Json数据

scala递归的方法快速解析Json数据
思路:既然可以得到keyset() ,为什么不根据keyset去进行遍历,拿到所有的数据?主要难点在于我们不知道我们拿到的key去解析成一个对象还是去解析成一个字符串,我在这里用到了模式匹配,以下提供代码以供参考(Array类型的我没有处理,如果要处理根据业务去进行相应的处理):


import java.util

import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import scala.util.parsing.json.JSonArray
import scala.collection.mutable.ListBuffer

object test_str {


  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("wc").setMaster("local")
    val sc = new SparkContext(conf)

    val list = new ListBuffer[util.HashMap[String,String]]
    val rddMap: RDD[util.HashMap[String, String]] = sc.textFile("C:\Users\names\Desktop\td-ecm-vendor-life-proxy-service-bussiness-v1.1632672000489.log").map(_.split("\s+", 4).last).map(str => {

      val jsonObject: JSonObject = JSON.parseObject(str)
      val map = new util.HashMap[String, String]()
      val tuple: (JSONObject, util.HashMap[String, String]) = str2json(jsonObject, map)
      tuple._2
    })
    rddMap.foreach(println)

  }

  def str2json(tp: Tuple2[JSONObject,util.HashMap[String,String]]):(JSONObject,util.HashMap[String,String]) = {
    var jSonObject = new JSonObject()
    if(!tp._1.isEmpty){
      val set: util.Set[String] = tp._1.keySet()
      val arr = set.toArray()
      for (i <- arr) {
        if (getType(tp._1, i.toString) == "JSONObject") {
          jSonObject = str2json(tp._1.getJSonObject(i.toString),tp._2)._1
        } else if (getType(tp._1, i.toString) == "String") {
          tp._2.put(i.toString,tp._1.getString(i.toString))
        }
      }
    }
    (jSONObject,tp._2)
  }


  val getType = (jsonObject: JSONObject, str: String) => {
    val str1: String = jsonObject.getString(str)
    var value: AnyRef = "str"
    try {
      value = JSON.parse(str1)
    } catch {
      case e: Exception =>
        null
    }
    value match {
      case _: JSonObject =>
        "JSONObject"
      case _: JSonArray =>
        "JSONArray"
      case _ =>
        "String"
    }
  }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/278171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号