MULE 4:DATAWEAVE 2.0:如何获得 Dataweave 2.0 中两个时间实例之间的小时差?
MULE 4 : DATAWEAVE 2.0 : How to get the difference in hours between Two Time instances in Dataweave 2.0?
我的要求是获取文件的年龄。
我使用 #[attributes.creationTime]
获取文件创建时间
为了获得年龄,我对当前时间进行了修改。
<set-variable value="#[now() - attributes.creationTime]" doc:name="File Duration" doc:id="a7c594b0-3a03-4054-a65a-d32dc08c06e1" variableName="fileAge"/>
这会将文件年龄设置为:PT-3H-23M-1.577259S
根据 Mule 4 文档,https://docs.mulesoft.com/mule-runtime/4.3/dataweave-types#duration-coercion
我试图通过以下方式获取年龄:
案例 1:vars.fileAge as Number {unit: "hours"}
和
案例 2:vars.fileAge.hours
在情况 1 中,我得到错误:
org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Cannot coerce Object { encoding: UTF-8, mediaType: application/java; charset=UTF-8, mimeType: application/java, raw: org.mule.weave.v2.module.pojo.reader.JavaBeanObjectValue$JavaBeanObjectSeq@23e1079b, class: java.time.Duration } ({nano: 422741000 as Number {class: "java.lang.Integer"},units: ["SECONDS" as ...) to Number
1| vars.fileAge as Number {unit: "hours"}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
at main (line: 1, column: 1)" evaluating expression: "vars.fileAge as Number {unit: "hours"} ".
在案例 2 中,我得到 Null。
有人可以帮我吗?
但我在
中遇到错误
更改您的变量,以便生成 JSON 作为输出:
output application/json --- now() - attributes.creationTime
编写 DW 表达式时的默认输出是 application/java
。如果你注意你的变量的类型设置为 java 的 java.time.Duration
当它应该是 DW 的 Period
如果你想执行 as Number {units: "hours"}
类型转换。切换到 JSON 允许 DW 为您的数据创建 Period
类型。
我敢打赌,如果我足够努力,我将能够弄清楚如何(或在哪里我可以采取 Duration
并铸造成 Period
。如果我以后有更多时间,我 '我会研究一下,或者其他人可以提供更好的答案。
您应该在设置vars.fileAge
时进行计算。下面的 dw 将只输出两个日期时间之间的小时差。在 DW 2.3.0
上测试
%dw 2.0
output application/java
var creationTime = "2020-07-17T10:16:53.196Z"
---
(now() - creationTime) as Number {unit: "hours"}
我觉得是DW版本或者Mule Studio版本的问题。
我正在使用 DW 2.0 和 Mule studio 7.4.2
当我做不同时:now() - attributes.creationTime
我在响应中只得到秒和纳秒。如果我从那里检索秒数并除以 3600,我会得到准确的小时数,如差异结果所示。
但是,如果我尝试将强制转换用作 Number {unit : "hours" } 它会失败。
我的要求是获取文件的年龄。
我使用 #[attributes.creationTime]
获取文件创建时间
为了获得年龄,我对当前时间进行了修改。
<set-variable value="#[now() - attributes.creationTime]" doc:name="File Duration" doc:id="a7c594b0-3a03-4054-a65a-d32dc08c06e1" variableName="fileAge"/>
这会将文件年龄设置为:PT-3H-23M-1.577259S
根据 Mule 4 文档,https://docs.mulesoft.com/mule-runtime/4.3/dataweave-types#duration-coercion
我试图通过以下方式获取年龄:
案例 1:vars.fileAge as Number {unit: "hours"}
和
案例 2:vars.fileAge.hours
在情况 1 中,我得到错误:
org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Cannot coerce Object { encoding: UTF-8, mediaType: application/java; charset=UTF-8, mimeType: application/java, raw: org.mule.weave.v2.module.pojo.reader.JavaBeanObjectValue$JavaBeanObjectSeq@23e1079b, class: java.time.Duration } ({nano: 422741000 as Number {class: "java.lang.Integer"},units: ["SECONDS" as ...) to Number
1| vars.fileAge as Number {unit: "hours"}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
at main (line: 1, column: 1)" evaluating expression: "vars.fileAge as Number {unit: "hours"} ".
在案例 2 中,我得到 Null。
有人可以帮我吗?
但我在
中遇到错误更改您的变量,以便生成 JSON 作为输出:
output application/json --- now() - attributes.creationTime
编写 DW 表达式时的默认输出是 application/java
。如果你注意你的变量的类型设置为 java 的 java.time.Duration
当它应该是 DW 的 Period
如果你想执行 as Number {units: "hours"}
类型转换。切换到 JSON 允许 DW 为您的数据创建 Period
类型。
我敢打赌,如果我足够努力,我将能够弄清楚如何(或在哪里我可以采取 Duration
并铸造成 Period
。如果我以后有更多时间,我 '我会研究一下,或者其他人可以提供更好的答案。
您应该在设置vars.fileAge
时进行计算。下面的 dw 将只输出两个日期时间之间的小时差。在 DW 2.3.0
%dw 2.0
output application/java
var creationTime = "2020-07-17T10:16:53.196Z"
---
(now() - creationTime) as Number {unit: "hours"}
我觉得是DW版本或者Mule Studio版本的问题。 我正在使用 DW 2.0 和 Mule studio 7.4.2
当我做不同时:now() - attributes.creationTime
我在响应中只得到秒和纳秒。如果我从那里检索秒数并除以 3600,我会得到准确的小时数,如差异结果所示。
但是,如果我尝试将强制转换用作 Number {unit : "hours" } 它会失败。