如何检查 Apache Camel SQL 组件结果集 (select) 是否为空?
How to check an Apache Camel SQL component resultset (select) is empty?
我是 运行 从数据库中读取标志记录的 Camel 路由。如果不存在标志,则将其插入并执行一些其他操作。我希望能够检查查询的结果集是否为空,但我在路由(选择)上使用的条件似乎被忽略了,所以它的工作就像它总是在数据库中找到一些东西,即使我确定它不是(事实上记录 ${body} 显示为空。
我正在使用 spring XML DSL,这是路线:
<from uri="file:/D:/LOCAL/?include=(?i).*.zip&moveFailed=ErrorFiles&move=D:/LOCAL/WRK/"/> <!--Catch a zip file as trigger for route-->
<to uri="sql:SELECT LOAD_DATE FROM IMPORT_CUSTOMER_CTRL WHERE LOAD_DATE = CURRENT_DATE?datasource=#customerDS&routeEmptyResultSet=true&outputType=SelectOne"/> <!-- Read a flag record from db -->
<log message="Query result: ${body}" loggingLevel="INFO"/>
<choice>
<when>
<simple>${body)} == null</simple> <!--IF RESULTSET IS EMPTY THEN DO SOMETHING. THIS CONDITION FAILS AND ALWAYS GOES BY OTHERWISE BRANCH-->**strong text**
<log message="Do something" loggingLevel="INFO"/>
<!--Insert flag record -->
<to uri="sql:INSERT INTO IMPORT_CUSTOMER_CTRL (LOAD_DATE) VALUES(CURRENT_DATE)?dataSource=#customerDS" />
</when>
<otherwise>
<log message="Flag record already exists, ignoring:${body}" loggingLevel="INFO"/>
</otherwise>
</choice>
对于 when 条件我试过 ${body)} == null 和 ${body)} == '' 甚至 ${bodyAs(String)} == '' 但是 choice 的行为总是被填满并且按照其他方式进行。我知道,因为我总是在日志中收到“标志记录已存在..”消息。
判断结果集是否为空的正确方法是什么?
根据 Sql component documentation:
For select operations, the result is an instance of List<Map<String, Object>> type, as returned by the JdbcTemplate.queryForList() method.
因此 body
永远不会 null
- 您必须检查返回的 List
对象的内容以查看结果集包含的内容。
下面是一条完整的路线,它对 body
:
进行了多项检查
from("direct:mainRoute")
.routeId("MainRoute")
.process(e ->{
List<String> list = new ArrayList<String>();
list.add("foo");
e.getMessage().setBody(list, List.class);
})
.log("MainRoute BEGINS: BODY: ${body}")
.choice()
.when(simple("${body} == null"))
.log("body is null")
.otherwise()
.log("body is not null")
.end()
.choice()
.when(simple("${body} is 'java.util.List'"))
.log("body is a list")
.otherwise()
.log("body is not a list")
.end()
.choice()
.when(simple("${body.isEmpty()}"))
.log("list in body is empty")
.otherwise()
.log("list in body is not empty")
.end()
.log("MainRoute ENDS: BODY: ${body}")
.end()
;
当路由为 运行 时,它会打印
MainRoute INFO MainRoute BEGINS: BODY: [foo]
MainRoute INFO body is not null
MainRoute INFO body is a list
MainRoute INFO list in body is not empty
MainRoute INFO MainRoute ENDS: BODY: [foo]
或
MainRoute INFO MainRoute BEGINS: BODY: []
MainRoute INFO body is not null
MainRoute INFO body is a list
MainRoute INFO list in body is empty
MainRoute INFO MainRoute ENDS: BODY: []
取决于 List
是否有物品。
would you post your comment as answer so I can mark it as accepted
answer
如上所述,无论输出类型(我指的是列表还是单个元组),通用解决方案都是分析发布为 的各种“meta-data” headers 来自骆驼:https://camel.apache.org/components/3.17.x/sql-component.html#_message_headers
您应该特别看看 CamelSqlRowCount
,正如其名称所示,它将为您提供有关 SQL 查询返回的记录数的信息。
我是 运行 从数据库中读取标志记录的 Camel 路由。如果不存在标志,则将其插入并执行一些其他操作。我希望能够检查查询的结果集是否为空,但我在路由(选择)上使用的条件似乎被忽略了,所以它的工作就像它总是在数据库中找到一些东西,即使我确定它不是(事实上记录 ${body} 显示为空。
我正在使用 spring XML DSL,这是路线:
<from uri="file:/D:/LOCAL/?include=(?i).*.zip&moveFailed=ErrorFiles&move=D:/LOCAL/WRK/"/> <!--Catch a zip file as trigger for route-->
<to uri="sql:SELECT LOAD_DATE FROM IMPORT_CUSTOMER_CTRL WHERE LOAD_DATE = CURRENT_DATE?datasource=#customerDS&routeEmptyResultSet=true&outputType=SelectOne"/> <!-- Read a flag record from db -->
<log message="Query result: ${body}" loggingLevel="INFO"/>
<choice>
<when>
<simple>${body)} == null</simple> <!--IF RESULTSET IS EMPTY THEN DO SOMETHING. THIS CONDITION FAILS AND ALWAYS GOES BY OTHERWISE BRANCH-->**strong text**
<log message="Do something" loggingLevel="INFO"/>
<!--Insert flag record -->
<to uri="sql:INSERT INTO IMPORT_CUSTOMER_CTRL (LOAD_DATE) VALUES(CURRENT_DATE)?dataSource=#customerDS" />
</when>
<otherwise>
<log message="Flag record already exists, ignoring:${body}" loggingLevel="INFO"/>
</otherwise>
</choice>
对于 when 条件我试过 ${body)} == null 和 ${body)} == '' 甚至 ${bodyAs(String)} == '' 但是 choice 的行为总是被填满并且按照其他方式进行。我知道,因为我总是在日志中收到“标志记录已存在..”消息。
判断结果集是否为空的正确方法是什么?
根据 Sql component documentation:
For select operations, the result is an instance of List<Map<String, Object>> type, as returned by the JdbcTemplate.queryForList() method.
因此 body
永远不会 null
- 您必须检查返回的 List
对象的内容以查看结果集包含的内容。
下面是一条完整的路线,它对 body
:
from("direct:mainRoute")
.routeId("MainRoute")
.process(e ->{
List<String> list = new ArrayList<String>();
list.add("foo");
e.getMessage().setBody(list, List.class);
})
.log("MainRoute BEGINS: BODY: ${body}")
.choice()
.when(simple("${body} == null"))
.log("body is null")
.otherwise()
.log("body is not null")
.end()
.choice()
.when(simple("${body} is 'java.util.List'"))
.log("body is a list")
.otherwise()
.log("body is not a list")
.end()
.choice()
.when(simple("${body.isEmpty()}"))
.log("list in body is empty")
.otherwise()
.log("list in body is not empty")
.end()
.log("MainRoute ENDS: BODY: ${body}")
.end()
;
当路由为 运行 时,它会打印
MainRoute INFO MainRoute BEGINS: BODY: [foo]
MainRoute INFO body is not null
MainRoute INFO body is a list
MainRoute INFO list in body is not empty
MainRoute INFO MainRoute ENDS: BODY: [foo]
或
MainRoute INFO MainRoute BEGINS: BODY: []
MainRoute INFO body is not null
MainRoute INFO body is a list
MainRoute INFO list in body is empty
MainRoute INFO MainRoute ENDS: BODY: []
取决于 List
是否有物品。
would you post your comment as answer so I can mark it as accepted answer
如上所述,无论输出类型(我指的是列表还是单个元组),通用解决方案都是分析发布为 的各种“meta-data” headers 来自骆驼:https://camel.apache.org/components/3.17.x/sql-component.html#_message_headers
您应该特别看看 CamelSqlRowCount
,正如其名称所示,它将为您提供有关 SQL 查询返回的记录数的信息。