跳转到主要内容

Django应用的水平数据库分片(水平分区)库。

项目描述

Django Horizon

https://travis-ci.org/uncovertruth/django-horizon.svg?branch=master https://api.codacy.com/project/badge/Grade/6f4ba73576904beaa41d68f40970bda9 https://codebeat.co/badges/74f07702-68ed-47e7-91e6-9088b0532342 https://www.codefactor.io/repository/github/uncovertruth/django-horizon/badge https://codecov.io/gh/uncovertruth/django-horizon/branch/master/graph/badge.svg Documentation Status Updates Python 3 https://img.shields.io/pypi/v/django-horizon.svg

目的

Django应用的水平数据库分片(水平分区)库。

Logo

特性

  • 通过某些外键字段(如用户账户)进行分片(水平分区)。

安装

要安装Django Horizon,请在您的终端中运行以下命令

$ pip install django-horizon

这是安装Django Horizon的首选方法,因为它将始终安装最新的稳定版本。

如果您还没有安装pip,此Python安装指南可以指导您完成整个过程。

使用

设置

在您的settings.py中添加数据库路由配置

水平数据库组和元数据存储
HORIZONTAL_CONFIG = {
    'GROUPS': {
        'group1': {  # The name of database horizontal partitioning group
            'DATABASES': {
                1: {
                    'write': 'member1-primary',
                    'read': ['member1-replica-1', 'member1-replica-2'],  # Pick randomly by router
                },
                2: {
                    'write': 'member2-primary',
                    'read': ['member2-replica'],
                },
                3: {
                    'write': 'a3',  # Used by 'read' too
                },
            },
            'PICKABLES': [2, 3],  # Group member keys to pick new database
        },
    },
    'METADATA_MODEL': 'app.HorizontalMetadata',  # Metadata store for horizontal partition key and there database
}
数据库路由
DATABASE_ROUTERS = (
    'horizon.routers.HorizontalRouter',
    ...
)

示例模型

按用户进行水平分区

元数据存储
from horizon.models import AbstractHorizontalMetadata

class HorizontalMetadata(AbstractHorizontalMetadata):
    pass

在示例中,元数据存储保存以下内容。

  • group:水平分区的组名称。

  • key:确定表记录在水平分区组中的分布。

  • index:在水平分区组中选择的数据库索引。

分片模型
from django.conf import settings

from horizon.manager import HorizontalManager  # For Django<1.10
from horizon.models import AbstractHorizontalModel


class SomeLargeModel(AbstractHorizontalModel):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.DO_NOTHING,
        db_constraint=False,  # May be using anothor database
    )
    ...

    objects = HorizontalManager()  # For Django<1.10

    class Meta(object):
        horizontal_group = 'group1'  # Group name
        horizontal_key = 'user'  # Field name to use group key

在许多情况下,使用UUIDField字段作为id。默认情况下,AbstractHorizontalModel使用UUIDField作为主键字段。

使用模型
from django.contrib.auth import get_user_model


user_model = get_user_model()
user = user_model.objects.get(pk=1)

# Get by foreign instance
SomeLargeModel.objects.filter(uses=user)

# Get by foreign id
SomeLargeModel.objects.filter(uses_id=user.id)
模型限制
  • 当未指定水平键字段进行过滤时,会发生django.db.utils.IntegrityError异常

    SomeLargeModel.objects.all()
  • 无法通过外键字段查找,因为还有其他(如default)数据库

    list(user.somelargemodel_set.all())

历史

1.1.2 (2018-11-15)

  • 更新_create_object_from_params为新接口

  • 添加对Django 2.1的支持

  • 添加对Python 3.7的支持

1.1.1 (2018-08-03)

  • QuerySet作为混入迁移

1.1.0 (2018-03-30)

  • 停止支持Django 1.9、1.10

1.0.0 (2018-02-02)

  • 添加对Django 2.0的支持

  • 停止支持Django 1.8

0.0.1 (2017-05-22)

  • 首次发布在PyPI上。

项目详情


下载文件

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

源分布

django-horizon-1.1.2.tar.gz (239.6 kB 查看散列)

上传时间

构建分布

django_horizon-1.1.2-py2.py3-none-any.whl (10.9 kB 查看散列)

上传时间 Python 2 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面