温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jolt json中json mapping是什么

发布时间:2021-10-20 09:53:45 来源:亿速云 阅读:394 作者:柒染 栏目:大数据

jolt json中json mapping是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

demo 解读

有了第一篇的基础,操作jolt已经不成问题,对于大部分json的mapping已经得心应手了,本片主要聚焦jolt除了json的mapping功能以外的其他功能。

模式的含义

  • modify-default-beta 修改-默认 当左手边不存在或是为空的时候进行转换。

  • modify-overwrite-beta 修改-覆盖 保留老数据,如果值相同会更新

数组(Array)相关 - List Functions

json input

{
  "scores": [
    4,
    2,
    8,
    7,
    5
  ]
}

json spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      // 计算数组长度
      "numScores": "=size(@(1,scores))",
      // 数组取头取尾
      "firstScore": "=firstElement(@(1,scores))",
      "lastScore": "=lastElement(@(1,scores))",
      // 出不来值
      "scoreAtMidPoint": "=elementAt(@(1,scores),2)",
      // 数组排序
      "sortedScores": "=sort(@(1,scores))"    }
  }
]

json output

{
  "scores" : [ 4, 2, 8, 7, 5 ],
  "numScores" : 5,
  "firstScore" : 4,
  "lastScore" : 5,
  "sortedScores" : [ 2, 4, 5, 7, 8 ]
}

数学(Math)相关 - Math Functions

json input

{
  "intData" : [ 2, 7, 5 ],
  "doubleData" : [ 0.25, 1.5, 1 ],

  "a" : 10,
  "b" : 5,
  "c" : 3,

  "negative" : "-1.0"
}

json spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      // 数组 求和
      "sumIntData": "=intSum(@(1,intData))",
      "sumLongData": "=intSum(@(1,intData))", // 和intSum一样,不同的是返回Java Long
      "sumDoubleData": "=doubleSum(@(1,doubleData))",
      // 数组 求平均
      "avgIntData": "=avg(@(1,intData))", // 返回double类型
      "avgDoubleData": "=avg(@(1,doubleData))",
      // 数组 排序
      "sortedIntScores": "=sort(@(1,intData))",
      // 获取 最小值
      "minAB": "=min(@(1,a),@(1,b))", 
      // 获取 最大值
      "maxAB": "=max(@(1,a),@(1,b))", 
      // 获取 绝对值
      "abs": "=abs(@(1,negative))",
      // 除法
      "aDivB": "=divide(@(1,a),@(1,b))",
      "aDivC": "=divide(@(1,a),@(1,c))", 
      //
      // 除法 四舍五入
      "aDivCRounded4": "=divideAndRound(4,@(1,a),@(1,c))"
    }
  }
]

json output

{
  "intData" : [ 2, 7, 5 ],
  "doubleData" : [ 0.25, 1.5, 1 ],
  "a" : 10,
  "b" : 5,
  "c" : 3,
  "negative" : "-1.0",
  "sumIntData" : 14,
  "sumLongData" : 14,
  "sumDoubleData" : 2.75,
  "avgIntData" : 4.666666666666667,
  "avgDoubleData" : 0.9166666666666666,
  "sortedIntScores" : [ 2, 5, 7 ],
  "minAB" : 5,
  "maxAB" : 10,
  "abs" : 1.0,
  "aDivB" : 2.0,
  "aDivC" : 3.3333333333333335,
  "aDivCRounded4" : 3.3333
}

类型转换 - Type Conversion

json input

{
  "happy": "true",
  "meh": "meh",
  "answer": 42,

  "statistics" : [
    {
      "id" : "A",
      "min" : "2.0",
      "max" : "10.0",
      "avg" : "7.9"
    },
    {
      "min" : "6",
      "max" : "6",
      "avg" : "6"
    },
    {
      "id" : "C"
    }
  ]
}

json spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      // 字符串 转 布尔
      "happy": "=toBoolean",
      // 如果原来不是布尔,转boolean可以设置false
      "meh": ["=toBoolean", false],
      //
      // 数字 转 字符串
      "answer": "=toString",
      // 下面做一些类型转换练习,缺省数据给默认值
      "statistics": {
        "*": {
          // 转成 整型 缺省设置0
          "min": ["=toInteger", 0],
          // 转成 整型 缺省设置null
          "max": ["=toInteger", null],
          // 转成 浮点型 缺省设置null
          "avg": ["=toDouble", null],
          // id列缺省时 设置 UNKNOWN
          "_id": "UNKNOWN"
        }
      }
    }
  }
]

json output

{
  "happy" : true,
  "meh" : false,
  "answer" : "42",
  "statistics" : [ {
    "id" : "A",
    "min" : 2,
    "max" : 10,
    "avg" : 7.9
  }, {
    "min" : 6,
    "max" : 6,
    "avg" : 6.0,
    "id" : "UNKNOWN"
  }, {
    "id" : "C",
    "min" : 0,
    "max" : null,
    "avg" : null
  } ]
}

字符串连接 - String Concatenation

json input

{
  "x": [ 3, 2, 1, "go"  ],
  "small": "small",
  "BIG": "BIG",

  "people": [
    {
      "firstName": "Bob",
      "lastName": "Smith",
      "address": {
        "state": null
      }
    },
    {
      "firstName": "Sterling",
      "lastName": "Archer"
    }
  ]
}

json spec

[
  {
  //modify-default-beta模式的含义是,当左手边不存在或是为空的时候进行转换。
    "operation": "modify-default-beta",
    "spec": {
      // @(1,x)将x数组中各个元素解析出来,再组合
      // y通过join将x数组中元素通过 逗号 组合
      // z通过join将x数组中元素通过 空格 组合
      "y": "=join(',',@(1,x))",
      "z": "=join(' ',@(1,x))",
      //
      // 英文字符全部大写或小写转换
      "small_toUpper": "=toUpper(@(1,small))",
      "BIG_toLower": "=toLower(@(1,BIG))",
      "people": {
        "*": {
          //  1表示,钻取第二层数据
          "fullName": "=concat(@(1,firstName),' ',@(1,lastName))",
          // 后缀问好的意思是,实际有address这个字段时才会解析
          "address?": {
            "state": "Texas"
          }
        }
      }
    }
  }
]

json output

{
  "x" : [ 3, 2, 1, "go" ],
  "small" : "small",
  "BIG" : "BIG",
  "people" : [ {
    "firstName" : "Bob",
    "lastName" : "Smith",
    "address" : {
      "state" : "Texas"
    },
    "fullName" : "Bob Smith"
  }, {
    "firstName" : "Sterling",
    "lastName" : "Archer",
    "fullName" : "Sterling Archer"
  } ],
  "y" : "3,2,1,go",
  "z" : "3 2 1 go",
  "small_toUpper" : "SMALL",
  "BIG_toLower" : "big"
}

关于jolt json中json mapping是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI