我正在尝试使用分区键从dynamodb获取值,如果找到了分区键值,则代码可以正常工作。但是,当找不到分区键时,将引发异常并且不会被捕获。
在代码中,我使用了自定义层d_c_fun,其中已定义了记录器功能。 我已经尝试了以下逻辑的elseif函数。 如果找到分区键“ data_id”,则将正确返回值。但是当不存在键值时,它将引发错误,而不是像下面的异常块那样给出响应。
import json
import boto3
import logging
import d_c_fun as cf
LOGGER = cf.get_logger()
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('tablename')
def lambda_handler(event, context):
LOGGER.lambda_init(event, context)
# Event value received through trigger
D_Id = event['D_Id']
if not D_Id:
raise Exception ("data_id value not passed")
#LOGGER.error(""data_id value not passed"")
elif D_Id :
resp = table.get_item(Key={
"data_id":D_Id})
LOGGER.info("Response Output value for the %s: %s", D_Id, resp['Item'])
return resp['Item']
else:
raise Exception ("The data_id - %s provided doesn't exist in db", D_Id)
1)o / p:当下表中的data_id匹配时。
输入事件
{“ D_Id”:“ data2”}
代码输出:
{“ table_name”:“ data2”,“ data_id”:“ data2”}
2)o / p:当data_id与下表不匹配时。
输入事件值:
{“ D_Id”:“数据”}
代码输出:
{“ errorMessage”:“'Item'”,“ errorType”:“ KeyError”,
“堆栈跟踪”: [ 在lambda_handler \ n LOGGER.info(\“%s中的响应输出值:%s \”,D_Id, resp ['Item'])\ n“]}
如果data_id不匹配,则预期o / p。
data_id-提供的数据在db中不存在。”
3)o / p:当data_id作为空值传递时。
输入事件值:
{“ D_Id”:“”}
代码输出:
{“ errorMessage”:“未传递data_id值”,“ errorType”: “异常”,“ stackTrace”:[ lambda_handler中的“ File \” / var / task / demo.py \”,第18行\ n引发异常(\“ data_id未设置\”)\ n“]}
应该是预期的输出。
“ errorMessage”:“未传递data_id值”,
答案 0 :(得分:0)
我的原始答案完全出于您的目的。试试这个。
import json
import boto3
import logging
import d_c_fun as cf
LOGGER = cf.get_logger()
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('tablename')
def lambda_handler(event, context):
LOGGER.lambda_init(event, context)
# Event value received through trigger
D_Id = event['D_Id']
if not D_Id:
raise Exception ("data_id value not passed")
#LOGGER.error(""data_id value not passed"")
else:
try:
resp = table.get_item(Key={"data_id":D_Id})
LOGGER.info("Response Output value for the %s: %s", D_Id, resp['Item'])
return resp['Item']
except:
raise Exception ("The data_id - %s provided doesn't exist in db", D_Id)
答案 1 :(得分:0)
我也已经开发了此代码,并且可以按预期工作:)。
declare @Drug table ([Name] int, ID varchar(64), ID2 varchar(64), ID3 varchar(64), ID4 varchar(64), [Match] varchar(64))
insert into @Drug ([Name], ID, ID2, ID3, ID4, [Match])
select 1, 'BG234560', null, null, null, 'BB78458745' union all
select 2, 'AC678900', 'BB7868877', null, null, 'ZZ5421896' union all
select 3, 'AT9808744', null, null, null, 'BB546897' union all
select 4, '989878AC', null, 'BBIIT9807', null, 'ZZ5874896' union all
select 5, 'A9045678', null, null, null, 'BB748596' union all
select 6, 'BB98076', 'AC9800876', null, null, 'ZZ2536987' union all
select 7, 'ZZ8793456', 'YY879654', null, null, 'BB4587961' union all
select 8, 'UIT90876', null, null, null, 'ZZ365289' union all
select 9, 'BH98048474', null, 'YYCC123456', null, 'BB6584123' union all
select 10, 'FT7849562', 'ZZ56980T', null, null, 'ZZ6952314' union all
select 11, '3TY875759', null, null, null, 'BB258963' union all
select 12, '333IY78698', null, null, null, 'ZZ5463214' union all
select 13, '548I345', null, 'ZZ56980T', null, 'BB254879' union all
select 14, 'OPQ4567', 'BB45678', null, null, 'BB258963' union all
select 15, '2345YYY', null, null, null, 'BB58214785'
select *
from @Drug
where ID like 'BB%'
and ID like 'zz%'
and ID2 like 'BB%'
and ID2 like 'zz%'
and ID3 like 'BB%'
and ID3 like 'zz%'
and ID4 like 'BB%'
and ID4 like 'zz%'