Dynamodb 在十进制过滤时扫描“不支持浮点类型。请使用小数类型”

问题描述 投票:0回答:1

我被这个难住了..

这是我的扫描:

        response = table.scan(
        FilterExpression=Attr('ttl').gte(Decimal(time.time()-900)) & Attr('SessionStatus').eq('failed'))

但是当我运行它时,我收到错误:

Float types are not supported. Use Decimal types instead.

我做错了什么?

amazon-web-services amazon-dynamodb aws-cdk
1个回答
0
投票

在 DynamoDB 中,不直接支持浮点类型,建议使用 Decimal 类型来在处理十进制数时保持精度。请尝试以下代码

import boto3
import time
from boto3.dynamodb.conditions import Attr
from decimal import Decimal

# Initialize DynamoDB client
dynamodb_client = boto3.client('dynamodb', region_name='YOUR_REGION')

# Function to perform the scan
def scan_with_filter():
    try:
        # Calculate the time threshold for 15 minutes ago
        time_threshold = Decimal(str(time.time() - 900))  # 900 seconds = 15 minutes

        # Define scan parameters with the updated FilterExpression
        scan_params = {
            'TableName': 'YOUR_TABLE_NAME',
            'FilterExpression': Attr('ttl').gte(time_threshold) & Attr('SessionStatus').eq('failed')
        }

        response = dynamodb_client.scan(**scan_params)

        # Process scanned items
        items = response.get('Items', [])
        print("Scanned items:", items)
    except Exception as e:
        print("Error scanning DynamoDB table:", e)

# Perform the scan with the specified FilterExpression
scan_with_filter()

© www.soinside.com 2019 - 2024. All rights reserved.