05 Jun 2018, 19:00

AWS Athena, DynamoDB and boto3

Share

Athena

import boto3

result_bucket = 'atehna-query-execution-result'
athena = boto3.client('athena')

res = athena.start_query_execution(
    QueryString = ,
    QueryExecutionContext = {
        'Database' = athena_database
    },
    ResutltConfiguration = {
        'OutputLocation': 's3://' + result_bucket + '/'
    }
)

query_id = res["QueryExecutionId"]

get_execution = athena.get_query_execution(QueryExecutionId=query_id)
query_state = get_execution['QueryExecution']['Status']['State']

while query_state == 'QUEUED' or query_state == 'RUNNING':
    time.sleep(3)
    query_state = athena.get_query_execution(QueryExecutionId=query_id)["QueryExecution"]["Status"]["State"]
    if query_state == "SUCCEEDED":
        # do some
        break
    elif query_state == 'CANCELLED' or query_state == 'FAILED':
        # do
        break

Dynamodb

Create Table

import boto3

table_name = ''
dynamodb = boto3.resource('dynamodb')

table = dynamodb.create_table(
    TableName=table_name,
    KeySchema=[
        {
            'AttributeName': '',
            'KeyType': ''
        },
        {
            'AttributeName': '',
            'KeyType': ''
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': '',
            'Attributetype': ''
        },
        {
            'AttributeName': '',
            'Attributetype': ''
        }
    ],
    ProvisionedTheoughput={
        'ReadCapacityUnits': 1,
        'WriteCapacityUnits': 1
    }
)

Scan

import boto3

table_name = ''
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1')
table = dynamodb.Table(table_name)

result = table.scan()

Query

import boto3
from boto3.dynamodb.conditions import Key

table_name = ''
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)

result = table.query(
    KeyConditionExpression = Key('PartitionKey').eq('Pattern') & Key('RangeKey').between(start_date, end_date)
)

items = result['Items']

comments powered by Disqus