在 JOLT 中使用来自先前规范块的变量

Using variable from previous spec block in JOLT

我需要将输入属性转换为包含三个部分的 SOLR 请求,其中之一是 solrOptions。 根据我的要求,我有:

{
  ...
  "firstTranDateRange": "IN LAST 365 DAYS",
  "lastTranDateRange": "In last 60 days",
  ...
}

在输出中我需要有以下内容:

{
   "searchAttributes": { ... }
   "searchOptions": { ... }
   "solrOptions: {
     "anm_boost" : "1.0",
     "pnm_boost" : "1.0",
     "lastTranDays" : {
       "min" : "00000",
       "max": "00060"
     },
    "firstTranDays" : {
      "min" : "00000",
      "max" : "00365"
    }
  }
}

我写了以下规范:

[
  {
    "spec": {
      "searchAttrbutes": {
       // filling up some other attributes not related to the question
      "ltdr": "=substring(@(1,lastTranDateRange),8,3)",
      "ltdr1": "=trim",
      "ftdr": "=substring(@(1,firstTranDateRange),8,3)",
      "ftdr1": "=trim"
      }
    },
    "operation": "modify-default-beta"
  },
  {
    "spec": {
      "solrOptions": {
        "anm_boost": "1.0",
        "pnm_boost": "1.0"
      },
      "searchOptions": {
        "maxRecords": 1,
        ...
      }
    },
    "operation": "default"
  },
  {
    "spec": {
      "lastTranDateRange": {
        "#00000": "solrOptions.lastTranDays.min",
        "@(2,ltdr1)": "solrOptions.lastTranDays.max"
      },
      "firstTranDateRange": {
        "#00000": "solrOptions.firstTranDays.min",
        "@(2,ftdr1)": "solrOptions.firstTranDays.max"
      },
      ...
    },
    "operation": "shift"
  }
]

我还没有想出如何在数字前加上适当数量的零。在这方面的帮助也将不胜感激。但是,它首先不起作用。我定义的变量没有出现在输出中。在我得到的输出中

{
  "solrOptions": {
    "anm_boost": "1.0",
    "pnm_boost": "1.0",
    "wcNameOn": "true",
    "fuzzyNameOn": "true",
    "lastTranDays": {
      "min": "00000"
    },
    "firstTranDays": {
      "min": "00000"
    }
  },
  "searchOptions": {
    "maxRecords": "10",
    "matchIndicators": "TRUE",
     ...
  }
}

有人可以告诉我我做错了什么吗? 谢谢

您可以从 modify 转换中删除 "searchAttributes" 开始,假设输入正好是

{
  "firstTranDateRange": "IN LAST 365 DAYS",
  "lastTranDateRange": "In last 60 days"
}

否则保留它,并将其从 default 转换为 overwrite 以覆盖所有现有属性。顺便说一句,Jolt 中的 substring 是一个有趣的不同函数,不同于它们在某些 well-known DBMS 产品中的用法,这里 ending index 应作为 最后一个参数 .

提供

此外,您的案例需要 leftPad 函数来为值添加零。

*通配符用于表示全部或其余表达式(键名),&1 爬上一层(1)层树并获取相关键名(solrOptions)

因此,您可以使用以下规格组合:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=substring(@(1,&),8,11)" // overwrite all attributes at once by using * and & wildcards
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "lastTranDateRange": "=trim", //individual manipulation needed as not having 3 digits
      "*": "=leftPad(@(1,&),5,'0')"
    }
  },
  {
    "operation": "default",
    "spec": {
      "solrOptions": {
        "anm_boost": "1.0",
        "pnm_boost": "1.0"
      },
      "searchOptions": {
        "maxRecords": 1
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      //"searchAttributes": "&",
      "search*": "&",
      "solr*": {
        "*": "&1.&",
        "@(1,firstTranDateRange)": "&1.firstTranDays.max",
        "@(1,lastTranDateRange)": "&1.lastTranDays.max"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "solrOptions": {
        "firstTranDays": {
          "min": "00000"
        },
        "lastTranDays": {
          "min": "00000"
        }
      }
    }
  }
]

产生输出 :

{
  //"searchAttributes": "{....}",
  "solrOptions" : {
    "firstTranDays" : {
      "max" : "00365",
      "min" : "00000"
    },
    "lastTranDays" : {
      "max" : "00060",
      "min" : "00000"
    },
    "pnm_boost" : "1.0",
    "anm_boost" : "1.0"
  },
  "searchOptions" : {
    "maxRecords" : 1
  }
}