在WSO2中将JSON内容追加到JSON文件。

huangapple go评论54阅读模式
英文:

Appending JSON content to a JSON file in WSO2

问题

以下是翻译好的部分:

I have few JSON Payloads that have to be appended to a JSON file.
这是我的 JSON 负载将会如何看起来:

{"variant": {"940894": {"attributes": {"Size": "XL" } } } }

我将获得多个这种类型的 JSON 负载。
最后,我需要形成一个如下所示的文件:

[
{"variant": {"940894": {"attributes": {"Size": "XL" } } } },
{"variant": {"940895": {"attributes": {"Size": "Med" } } } },
{"variant": {"940895": {"attributes": {"Size": "Small" } } } }
]

这些是我为要求设置的属性:

<property expression="fn:concat('Test',get-property('File_Name'),'.txt')" name="FILE_NAME" scope="default"
<propertyGroup description="File and OutputPayload properties">
        <property name="messageType" scope="axis2" type="STRING" value="text/plain"/>
        <property name="ContentType" scope="axis2" type="STRING" value="text/plain"/>
        <property expression="$ctx:FILE_NAME" name="transport.vfs.ReplyFileName" scope="transport" type="STRING"/>
        <property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
	</propertyGroup>
			<call>
        <endpoint>
            <address uri="vfs:sftp://username:password/home/user/test/out/json?transport.vfs.Append=true">
                <suspendOnFailure>
                    <initialDuration>-1</initialDuration>
                    <progressionFactor>1</progressionFactor>
                </suspendOnFailure>
                <markForSuspension>
                    <retriesBeforeSuspension>0</retriesBeforeSuspension>
                </markForSuspension>
            </address>
        </endpoint>
    </call>

但我收到一个错误,错误消息是:“文件类型不支持追加模式”。在这里还可以尝试什么?

英文:

I have few JSON Payloads that have to be appended to a JSON file.
This is how my JSON Payload will look:

{"variant": {"940894": {"attributes": {"Size": "XL"	} } } }

I will be getting multiple json Payloads of this type.
At the end I need to form a file that looks like this:

[
{"variant": {"940894": {"attributes": {"Size": "XL"	} } } },
{"variant": {"940895": {"attributes": {"Size": "Med"	} } } },
{"variant": {"940895": {"attributes": {"Size": "Small"	} } } }
]

These are the properties I have set for the requirement:

<property expression="fn:concat('Test',get-property('File_Name'),'.txt')" name="FILE_NAME" scope="default"
<propertyGroup description="File and OutputPayload properties">
        <property name="messageType" scope="axis2" type="STRING" value="text/plain"/>
        <property name="ContentType" scope="axis2" type="STRING" value="text/plain"/>
        <property expression="$ctx:FILE_NAME" name="transport.vfs.ReplyFileName" scope="transport" type="STRING"/>
        <property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
	</propertyGroup>       
			<call>
        <endpoint>
            <address uri="vfs:sftp://username:password/home/user/test/out/json?transport.vfs.Append=true">
                <suspendOnFailure>
                    <initialDuration>-1</initialDuration>
                    <progressionFactor>1</progressionFactor>
                </suspendOnFailure>
                <markForSuspension>
                    <retriesBeforeSuspension>0</retriesBeforeSuspension>
                </markForSuspension>
            </address>
        </endpoint>
    </call>

But I get an error saying "The file type does not support append mode". What else can I try here.

答案1

得分: 2

我查看了代码,正如你已经注意到的,SFTP 协议不允许进行文件追加操作。SFTP 支持的操作在代码中这里列出:

Capability.CREATE, Capability.DELETE, Capability.RENAME, Capability.GET_TYPE,
Capability.LIST_CHILDREN, Capability.READ_CONTENT, Capability.URI, Capability.WRITE_CONTENT,
Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE, Capability.RANDOM_ACCESS_READ

但如果你使用 FTP 协议,你应该能够进行文件追加操作,因为它在这里有支持:

Capability.CREATE, Capability.DELETE, Capability.RENAME,
Capability.GET_TYPE, Capability.LIST_CHILDREN,
Capability.READ_CONTENT, Capability.GET_LAST_MODIFIED,
Capability.URI, Capability.WRITE_CONTENT,
Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ

文件追加操作也适用于本地文件系统。

如果你想追加文件但仍然要使用 SFTP,你将不得不执行读取 -> 追加 -> 写入文件的操作。或者可以考虑编写一个自定义类中介器。

英文:

I had a look at the code, and as you already noticed SFTP protocol doesn't allow you to do file appending. The supported operations for SFTP are here in the code.

Capability.CREATE, Capability.DELETE, Capability.RENAME, Capability.GET_TYPE,
Capability.LIST_CHILDREN, Capability.READ_CONTENT, Capability.URI, Capability.WRITE_CONTENT,
Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE, Capability.RANDOM_ACCESS_READ

But if you use FTP protocol instead you should be able to do File Appending as it's supported here.

Capability.CREATE, Capability.DELETE, Capability.RENAME,
Capability.GET_TYPE, Capability.LIST_CHILDREN,
Capability.READ_CONTENT, Capability.GET_LAST_MODIFIED,
Capability.URI, Capability.WRITE_CONTENT,
Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ

Appending works with the Local file system too.

If you want to append the file and still use SFTP you will have to READ -> APPEND -> WRITE the file. Or can consider writing a Custom Class mediator.

huangapple
  • 本文由 发表于 2023年2月8日 19:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定