windows 環境下 dbnamodb 環境搭建與使用

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

安裝 AWS Command Line Interface

pip  install  awscli    命令行環境java

 

安裝好後 輸入  aws  測試 是否安裝成功json

 

在鍵入 aws  configure 進行配置windows

AWS  Access  Key ID[None]:1oracle

AWS Secret  Access Key [None]:1app

Default region name [None]:1ide

Default output format[None]:1測試

以上配置 能夠參考 dynamodb 官方文檔 ui

 

 

dynamodb windows 安裝包下載地址:url

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={})

 

獲取外部數據

from __future__ import print_function # Python 2/3 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):
            if abs(o) % 1 > 0:
                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 = 2015

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



print("PutItem succeeded:")
print(response)
print(json.dumps(response, indent=4, cls=DecimalEncoder))
添加新項目
相關文章
相關標籤/搜索