gpt4 book ai didi

NiFi | How to replace text based on length of field in JSON records(NiFi|如何根据JSON记录中的字段长度替换文本)

转载 作者:bug小助手 更新时间:2023-10-28 09:48:01 25 4
gpt4 key购买 nike



I have data coming into NiFi in JSON format. Below is the sample

我有JSON格式的数据进入NiFi。以下是样品


{
"company_name": "mycompany",
"companytyp": "producer",
"Welcomemes": "welcome_message1",
"logo": "//somepath/to/logo URL "
}

I have been trying to apply a JOLT transformation for the following condition:

我一直在尝试对以下情况应用Jolt变换:



  • If the length of logo field is more than 10 characters then replace it with blank value

  • If the length of logo field is less than or equal to 10 characters then keep the provided value


I have not been able to achieve this. Would really appreciate some help & guidance. I am open to using any other processor too but the preference will be JOLT transformation

我还没能做到这一点。我会非常感谢你的帮助和指导。我也对使用任何其他处理器持开放态度,但首选是Jolt变换


Input 1:

输入1:


{
"company_name": "mycompany",
"companytyp": "producer",
"Welcomemes": "welcome_message1",
"logo": "This logo value is more than 10 characters "
}

Expected Output 1

预期产出%1


{
"company_name": "mycompany",
"companytyp": "producer",
"Welcomemes": "welcome_message1",
"logo": ""
}

Input 2:

输入2:


  "company_name": "mycompany",
"companytyp": "producer",
"Welcomemes": "welcome_message1",
"logo": "short logo"
}

Expected Output 2

预期产量2


{
"company_name": "mycompany",
"companytyp": "producer",
"Welcomemes": "welcome_message1",
"logo": "short logo"
}

更多回答
优秀答案推荐

I couldn't figure out how to put in an empty string but this spec will replace those logo fields with a single space:

我想不出如何将一个空字符串放入其中,但此规范将用一个空格替换这些徽标字段:


[
{
"operation": "modify-overwrite-beta",
"spec": {
"logo": "=substring(@(1,logo),0,11)",
"logo_size": "=size(@(1,logo))"
}
},
{
"operation": "shift",
"spec": {
"logo_size": {
"11": {
"# ": "logo"
},
"*": {
"@(2,logo)": "logo"
}
},
"logo": null,
"*": "&"
}
}
]


This is the final spec based on solution provided by @mattyb

这是基于@ martyb提供的解决方案的最终规范


 [
{
"operation": "modify-overwrite-beta",
"spec": {
"logo": "=substring(@(1,logo),0,11)",
"logo_size": "=size(@(1,logo))"
}
},
{
"operation": "shift",
"spec": {
"logo_size": {
"11": {
"# ": "logo"
},
"*": {
"@(2,logo)": "logo"
}
},
"logo": null,
"*": "&"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"logo": "=trim"
}
}]


The trim function trims in an case even if the length of the logo's value is less than 11. For example : "logo":" abc " will be converted eventually to "logo":"abc"

TRIM函数在大小写情况下进行裁剪,即使徽标值的长度小于11。例如:“LOGO”:“ABC”最终将转换为“LOGO”:“ABC”


You can try to convert the related value to null for the cases with the length more than 10, presuming that you have quoted values for the attribute logo for each case.

您可以尝试将长度大于10的案例的相关值转换为NULL,假设您已经为每个案例引用了属性徽标的值。


So, you might consider using the following one :

因此,您可以考虑使用以下方法:


[
{ // prepare the criteria of the string length comparison to be used within the the next spec
"operation": "modify-overwrite-beta",
"spec": {
"logo_size": "=size(@(1,logo))",
"x": "=intSum(-10,@(1,logo_size))",
"xAbs": "=abs(@(1,x))",
"sz": ["=divide(@(1,x),@(1,xAbs))", -1.0]
}
},
{
"operation": "shift",
"spec": {
"*om*": "&",
"sz": {
"*": {
"@": "logo"
},
"-1.0": { // the negative approach is used to determine the cases in which the related length exceeds ten
"@2,logo": "logo"
}
}
}
},
{ // convert the values to blanks for the null-valued logo attributes
"operation": "modify-overwrite-beta",
"spec": {
"logo": ["=notNull", ""]
}
}
]


更多回答

This worked as expected. Thank you so much!!! I edited the spec to replace the whitespace out. My not be elegant but performing the trim operation removes the white space. 'code' [ { "operation": "modify-overwrite-beta", "spec": { "logo": "=trim" } }]

果然奏效。非常感谢!我编辑了规范以替换掉空白。我不优雅,但执行修剪操作删除空白。“code”[ {“operation”:“modify-overwrite-beta”,“spec”:{“logo”:“=trim”} }]

Hi Aditya . Be aware that the trim function trims in an case, eg. whether the size is less than 11.

嗨,阿迪亚。请注意,Trim函数在案例中进行修剪,例如。大小是否小于11。

Btw, will you have any "logo": null, and if so do you need to convert them to "logo": "" as well ?

顺便说一句,你会有任何“LOGO”:空的,如果是的话,你需要把它们也转换成“LOGO”:“”吗?

@BarbarosÖzhan I liked your input. Thank you. However, out of curiosity-would the trim not work like this (a) if the value in logo field is more than 10 characters then blank space will be passed in logo field which after trimming will remove the blank space. (b) if the logo field originally has less than equal to 10 characters then original value will be passes and it will be trimmed. Either case It will satisfy the requirement. Am I missing something here?

@BarbarosÖzhan我喜欢你的意见。谢谢。然而,出于好奇-裁剪不会像这样工作:(A)如果徽标字段中的值超过10个字符,则徽标字段中将传递空格,裁剪后将删除空格。(B)如果徽标字段最初少于10个字符,则原始值将为PASS,并将被修剪。无论是哪种情况,它都会满足要求。我是不是漏掉了什么?

Sure there's no problem with (a), but if you say either for (b), then nothing to say left more for me. OK, have a nice day.

当然,(A)没有问题,但如果你说(B),那么我就没有什么可说的了。好的,祝你今天过得愉快。

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com