windows 环境下 dbnamodb 环境搭建与使用

时间:2025-04-22 12:05:01

https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/installing.html

安装 AWS Command Line Interface

pip  install  awscli    命令行环境

安装好后 输入  aws  测试 是否安装成功

在键入 aws  configure 进行配置

AWS  Access  Key ID[None]:1

AWS Secret  Access Key [None]:1

Default region name [None]:1

Default output format[None]:1

以上配置 可以参考 dynamodb 官方文档

dynamodb windows 安装包下载地址:

https://s3-ap-southeast-1.amazonaws.com/dynamodb-local-singapore/dynamodb_local_latest.zip

java 运行环境 JRE   下载地址:

https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

下载对应的jre 版本

安装完后,需要添加环境变量

DynamoDB  的启动 awscli 环境

java -Djava.library.path=./dynamodb_local_latest-jar DynamoDBLocal.jar -sharedDb

Windows  PowerShell 运行:window 命令行

cd  dynamodb_local_latest

java -D"java.library.path=./dynamodb_local_latest" -jar DynamoDBLocal.jar

输入后 不要关闭 运行窗口

from __future__ import print_function # Python 2/3 compatibility

import boto3 import json import decimal

from boto3.dynamodb.conditions import Key, Attr

from botocore.exceptions import ClientError

dynamodb = boto3.resource('dynamodb',, region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table("table_name")

table.get_item(Item={})

获取外部数据

/ compatibility
# 设置 打印方式
import boto3
import json
import decimal

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table('Movies')

with open("moviedata.json") as json_file:
    movies = json.load(json_file, parse_float = decimal.Decimal)
    for movie in movies:
        year = int(movie['year'])
        title = movie['title']
        info = movie['info']
        print("Adding movie:", year, title)
        table.put_item(
           Item={
               'year': year,
               'title': title,
               'info': info,
            }
        )
        

获取外部数据

from __future__ import print_function
import boto3
import json
import decimal

class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
             > :
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table('Movies')

title = "The Big New Movie"
year = 

# 添加 新项目 方法1
response = table.put_item(
   Item={
        'year': year,
        'title': title,
        'info': {
            'plot':"Nothing happens at all.",
            )
        }
    }
)

print("PutItem succeeded:")
print(response)
print(json.dumps(response, indent=, cls=DecimalEncoder))

添加新项目