跳转到主要内容

Python的高级DynamoDB接口,封装boto的底层接口

项目描述

Python的高级DynamoDB接口,封装boto的底层接口

安装

从PyPi

$ pip install bynamodb

从GitHub

$ pip install git+https://github.com/teddychoi/BynamoDB#egg=bynamodb

修补DynamoDBConnection

设置DynamoDBConnection默认主机和端口。

from bynamodb.patcher import patch_dynamodb_connection

patch_dynamodb_connection(host='localhost', port=8000)

模型定义

import datetime
from bynamodb.attributes import StringAttribute, StringSetAttribute
from bynamodb.indexes import GlobalAllIndex
from bynamodb.model import Model

class Article(Model):
    published_at = StringAttribute(hash_key=True)
    id = StringAttribute(range_key=True)
    title = StringAttribute()
    content = StringAttribute()
    author = StringAttribute()
    write_time = StringAttribute(
        default=lambda: str(datetime.datetime.now()))
    tags = StringSetAttribute(default=set())
    thumbnail = StringAttribute(null=True)

    class AuthorIndex(GlobalAllIndex):
        read_throughput = 5
        write_throughput = 5
        hash_key = 'author'
        range_key = 'published_at'

插入项目 & 获取项目

Article.put_item(
    published_at='2014-12-09',
    id='1',
    title='This is the title',
    content='This is the content',
    author='Bochul Choi'
)
article = Article.get_item(hash_key='2014-12-09', range_key='1')

从原始数据获取项目

您可以从从的底层API检索的原始数据中获取项目

from boto.dynamodb2.layer1 import DynamoDBConnection

conn = DynamoDBConnection()
raw_data = conn.get_item(
    'Article',
    {
        'published_at': {'S': '2014-12-09'},
        'id': {'S': '1'}
    }
)
article = Article.from_raw_data(raw_data['Item'])

简单扫描 & 查询

# Scan all articles that the title starts with "Title"
articles = Article.scan(title__startswith='Title')
# Get the total count of matching items
count = articles.count()
# Get the iterator of matching items
items = iter(articles)

# Query articles that author is "Bochul Choi"
articles = Article.query(author__eq='Bochul Choi', index_name='AuthorIndex')
# The query result also provide the count method and iterator
count = articles.count()
items = iter(articles)

扫描 & 查询中的复杂查找

from bynamodb.filterexps import Contains, GT

keyword = 'bynamodb'
filter_exp = GT('published_at', '2014-12-01') & (
    Contains('title', keyword) | Contains('content', keyword.upper()))

# Scan all articles that match the filter expression
articles = Article.scan(filter_exp)

# Query articles that match the filter expression and the author condition
author = 'Bochul Choi'
articles = Atricle.query(author__eq=author, filter_builder=filter_exp,
                         index_name='AuthorIndex')

批量写入 & 批量读取

with Article.batch_write() as batch:
    batch.put_item({
        'published_at': '2015-02-23',
        'id': '1',
        'title': 'Article 1',
        'content': 'This is the content',
        'author': 'Bochul Choi'
    })
    batch.put_item({
        'published_at': '2015-02-23',
        'id': '2',
        'title': 'Article 2',
        'content': 'This is the content',
        'author': 'Bochul Choi'
    })

articles = Article.batch_get(
    ('2015-02-23', '1'),
    ('2015-02-23', '2'),
)

变更日志

0.1.7
  • 跳过解码不存在的属性。

0.1.6
  • 修复ResultSet以停止检索项目,无论限制参数如何。

0.1.5
  • 为查询API添加对scan_index_forwardlimit参数的支持。

0.1.4
  • ExclusiveStartKey设置为未转换的LastEvaluatedKey

0.1.3
  • Model.delete需要使用编码键。

0.1.2
  • Model.put_item返回创建的项目。

0.1.1
  • NumberAttribute仅在数据库中以数字保存。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

bynamodb-0.1.7.tar.gz (8.5 kB 查看哈希值)

上传时间 源代码

由以下支持