Azure Data Factory筛选活动报错:folder属性不存在的问题求助
Azure Data Factory筛选活动报错排查:folder属性不存在问题
问题场景
在Azure Data Factory中,Web活动成功返回JSON输出后,配置筛选活动过滤folder.name为"livecode"的项:
- Item字段配置:
@activity('Web1').output.value - 条件字段配置:
@startswith(item().properties.folder.name,'livecode')
执行筛选活动时失败,报错提示folder属性不存在,可用属性仅为content、type。
Web活动输出示例
{ "value": [ { "id": "/subscriptions/xy_csv", "name": "xy_csv", "type": "Microsoft.code", "etag": "6200", "properties": { "folder": { "name": "samplecodes" }, "content": { "query": "select * from table 1", "metadata": { "language": "sql" }, "currentConnection": { "databaseName": "demo", "poolName": "Built-in" }, "resultLimit": 5000 }, "type": "SqlQuery" } }, { "id": "/subscriptions/ab_csv", "name": "ab_csv", "type": "Microsoft.code", "etag": "6200", "properties": { "folder": { "name": "livecode" }, "content": { "query": "select * from table 2", "metadata": { "language": "sql" }, "currentConnection": { "databaseName": "demo", "poolName": "Built-in" }, "resultLimit": 5000 }, "type": "SqlQuery" } } ] }
报错信息
{ "errorCode": "InvalidTemplate", "message": "The execution of template action 'FilterFilter1' failed: The evaluation of 'query' action 'where' expression '@startswith(item().properties.folder.name,'sql')' failed: 'The expression 'startswith(item().properties.folder.name,'sql')' cannot be evaluated because property 'folder' doesn't exist, available properties are 'content, type'.", "failureType": "UserError", "target": "Filter1", "details": "" }
解决方案
报错根源是部分返回项的properties中缺少folder属性,直接访问item().properties.folder.name会触发空引用错误。需要先判断属性是否存在,再执行匹配逻辑,推荐两种写法:
写法一:先检查属性存在性再匹配
@and(contains(item().properties, 'folder'), startswith(item().properties.folder.name, 'livecode'))
contains(item().properties, 'folder')先验证properties中存在folder字段,避免空引用startswith(...)再对存在的folder.name进行前缀匹配
写法二:用coalesce处理空值
@startswith(coalesce(item().properties.folder.name, ''), 'livecode')
coalesce会返回第一个非空值,若folder.name不存在则返回空字符串- 空字符串与
livecode的前缀匹配不成立,自然会被过滤,同时避免报错
内容的提问来源于stack exchange,提问作者Anbu Dhan
相关产品推荐
相关产品推荐

