简体   繁体   English

如何在 Django 中使用 AWS 的 Dynamo Db?

[英]How can I use AWS's Dynamo Db with Django?

I am developing web applications, APIs, and backends using the Django MVC framework.我正在使用 Django MVC 框架开发 Web 应用程序、API 和后端。 A major aspect of Django is its implementation of an ORM for models. Django 的一个主要方面是它对模型的 ORM 的实现。 It is an exceptionally good ORM.这是一个非常好的 ORM。 Typically when using Django, one utilizes an existing interface that maps one's Django model to a specific DBMS like Postgres, MySQL, or Oracle for example.通常,在使用 Django 时,人们会利用现有的接口将自己的 Django 模型映射到特定的 DBMS,例如 Postgres、MySQL 或 Oracle。

I have some specific needs, requirements regarding performance and scalability, so I really want to use AWS's Dynamo DB because it is highly cost efficient, very performant, and scales really well.我有一些特定的需求,对性能和可扩展性的要求,所以我真的很想使用 AWS 的 Dynamo DB,因为它具有很高的成本效益、非常好的性能并且扩展性非常好。

While I think Django allows one to implement their own interface for a DBMS if one wishes to do so, it is clearly advantageous to be able to use an existing DBMS interface when constructing one's Django models if one exists.虽然我认为 Django 允许人们为 DBMS 实现自己的接口,但如果有人愿意的话,在构建一个人的 Django 模型时能够使用现有的 DBMS 接口显然是有利的(如果存在的话)。

Can someone recommend a Django model interface to use so I can construct a model in Django that uses AWS's Dynamo DB?有人可以推荐使用 Django 模型接口,以便我可以在使用 AWS Dynamo DB 的 Django 中构建模型吗?

How about one using MongoDB?使用 MongoDB 怎么样?

As written by others, Django does not have NoSQL DBMS.正如其他人所写,Django 没有 NoSQL DBMS。 There are third-party packages, but given the flexible nature of NoSQL data, no 'ready-made batteries', as @slajma said.有第三方软件包,但考虑到 NoSQL 数据的灵活性,正如@slajma 所说,没有“现成的电池”。

PynamoDB seems fine, I never used it, so I can't recommend. PynamoDB 看起来不错,我从来没有用过它,所以我不能推荐。 In all use-cases I came across boto3 was sufficient.在我遇到的所有用例中,boto3 就足够了。 Setup is pretty simple, but the devil is in details (in the data structure and how nested it is, to be precise).设置非常简单,但问题在于细节(准确地说是数据结构以及它的嵌套方式)。 Basically, three steps are needed:基本上,需要三个步骤:

  1. connect with db and perform operation you want ( boto3 )与数据库连接并执行您想要的操作( boto3
  2. parse incoming data into Python dictionary (eg with dynamodb-json , boto3.dynamodb.types.TypeDeserializer or you can build your own)将传入数据解析为 Python 字典(例如,使用dynamodb-jsonboto3.dynamodb.types.TypeDeserializer或者您可以构建自己的字典)
  3. do business logic, store data into relational db using Django ORM or whatever you need做业务逻辑,使用 Django ORM 或任何你需要的东西将数据存储到关系数据库中

Simplest example:最简单的例子:

from dynamodb_json import json_util as dynamodb_json
from .models import YourModel

def get(request, partition_key):
    table = boto3.resource(
        'dynamodb',
        aws_access_key_id=...,
        aws_secret_access_key=...,
        region_name=...,
    ).Table(some_table_name)
    try:
        response = table.get_item(
            Key={partition_key: partition_key})
    except ClientError as e:
        logger.warning(e.response['Error']['Message'])
    else:
        data_str = response['Item']
        _data_dict = dynamodb_json.loads(data_str)

        # Validation and modification of incoming data goes here.
        data_dict = validation_and_modification(_data_dict)
        # Then you can do whatever you need, for example:
        obj, created = YourModel.objects.update_or_create(**data_dict)
        ...

Hope this helps someone.希望这可以帮助某人。 Examples for create, delete, list and update views can be found in the serverless repo .可以在serverless repo 中找到创建、删除、列表和更新视图的示例。

It's not like ready made battery for django, but worth looking at it regardless.它不像 django 的现成电池,但无论如何都值得一看。 https://github.com/pynamodb/PynamoDB https://github.com/pynamodb/PynamoDB

You can try Dynamorm or pynamoDB.您可以尝试 Dynamorm 或 pynamoDB。 I haven't tried them maybe they can help.我还没有尝试过,也许他们可以提供帮助。

DynamoDB 是非关系型的,我认为这使它在架构上与 Django 之类的 ORM 不兼容。

AWS DynamoDB 没有 Django 模型接口,但您可以使用 AWS 提供的 boto3 软件从那种数据库中检索数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM