a-h-abid/eloquent-cassandra 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

a-h-abid/eloquent-cassandra

Composer 安装命令:

composer require a-h-abid/eloquent-cassandra

包简介

Cassandra driver for Eloquent ORM

README 文档

README

Cassandra driver for Eloquent ORM.

Installation

Note: Make sure you have the Cassandra PHP driver installed (version 1.3+). You can find more information at DataStax PHP Driver.

Installation using composer:

composer require a-h-abid/eloquent-cassandra

Laravel

And add the service provider in config/app.php:

AHAbid\EloquentCassandra\CassandraServiceProvider::class,

The service provider will register a cassandra database extension with the original database manager. There is no need to register additional facades or objects. When using cassandra connections, Laravel will automatically provide you with the corresponding cassandra objects.

For usage outside Laravel, check out the Capsule manager and add:

$capsule->getDatabaseManager()->extend('cassandra', function($config)
{
    return new AHAbid\EloquentCassandra\Connection($config);
});

Lumen

Add next lines to your bootstrap.php

    $app->configure('database');
    $app->register(AHAbid\EloquentCassandra\CassandraServiceProvider::class);

Configuration

Change your default database connection name in config/database.php:

'default' => env('DB_CONNECTION', 'cassandra'),

And add a new cassandra connection:

'cassandra' => [
    'driver'          => 'cassandra',
    'scheme'          => env('DB_SCHEME', 'tcp'),
    'host'            => env('DB_HOST', 'localhost'),
    'port'            => env('DB_PORT', 9042),
    'keyspace'        => env('DB_DATABASE'),
    'username'        => env('DB_USERNAME'),
    'password'        => env('DB_PASSWORD'),
    'page_size'       => env('DB_PAGE_SIZE', 5000),
    'consistency'     => Cassandra::CONSISTENCY_LOCAL_ONE,
    'timeout'         => null,
    'connect_timeout' => 5.0,
    'request_timeout' => 12.0,
    'ssl' => [
        'verify_peer' => nv('DB_SSL_VERIFY_PEER', false),
        'trusted_cert' => nv('DB_SSL_TRUSTED_CERT_FILE', null),
        'client_cert' => nv('DB_SSL_CLIENT_CERT_FILE', null),
        'private_cert' => nv('DB_SSL_PRIVATE_CERT_FILE', null),
        'private_passphrase' => nv('DB_SSL_PRIVATE_PASSPHRASE', null),
    ],
],

You can connect to multiple servers with the following configuration:

'cassandra' => [
    'driver'          => 'cassandra',
    'host'            => ['192.168.0.1', '192.168.0.2'], //or '192.168.0.1,192.168.0.2'
    'port'            => env('DB_PORT', 9042),
    'keyspace'        => env('DB_DATABASE'),
    'username'        => env('DB_USERNAME'),
    'password'        => env('DB_PASSWORD'),
    'page_size'       => env('DB_PAGE_SIZE', 5000),
    'consistency'     => Cassandra::CONSISTENCY_LOCAL_ONE,
    'timeout'         => null,
    'connect_timeout' => 5.0,
    'request_timeout' => 12.0,
],

Note: you can enter all of your nodes in .env like :

# .env
DB_HOST=192.168.0.1,192.168.0.2,192.168.0.3

Note: list of available consistency levels (php constants):

Cassandra::CONSISTENCY_ANY
Cassandra::CONSISTENCY_ONE
Cassandra::CONSISTENCY_TWO
Cassandra::CONSISTENCY_THREE
Cassandra::CONSISTENCY_QUORUM
Cassandra::CONSISTENCY_ALL
Cassandra::CONSISTENCY_SERIAL
Cassandra::CONSISTENCY_QUORUM
Cassandra::CONSISTENCY_LOCAL_QUORUM
Cassandra::CONSISTENCY_EACH_QUORUM
Cassandra::CONSISTENCY_LOCAL_SERIAL
Cassandra::CONSISTENCY_LOCAL_ONE

Note: you can set specific consistency level according to the query using options

Eloquent

Model Usage

Supported most of eloquent query build features, events, fields access.

    $users = User::all();

    $user = User::where('email', 'tester@test.com')->first();

    $user = User::find(new \Cassandra\Uuid("7e4c27e2-1991-11e8-accf-0ed5f89f718b"))

Relations - NOT SUPPORTED

Attributes casting

There is ability to use UUID as model primary key

class Item
{
    ...

    protected $keyType = 'uuid';

    public $incrementing = true; // will automatically cast your primary key to keyType

    // OR

    protected $keyType = 'uuid';

    public $incrementing = false;

    protected $casts = [
        'id' => 'uuid',
    ];
    ...
}

Query Builder

The database driver plugs right into the original query builder. When using cassandra connections, you will be able to build fluent queries to perform database operations.

$users = DB::table('users')->get();

$user = DB::table('users')->where('name', 'John')->first();

If you did not change your default database connection, you will need to specify it when querying.

$user = DB::connection('cassandra')->table('users')->get();

Default use of get method of query builder will call chunked fetch from database. Chunk size can be configured on config file ( 'page_size' => env('DB_PAGE_SIZE', 5000)) or with additional query builder`s method setPageSize.

$comments = Comments::setPageSize(500)->get(); // will return all comments, not 500

WARNING: Not recomended to use get if there are a lot of data in table. Use getPage instead.

Get single page of resuts

$comments = Comments::setPageSize(500)->getPage(); // will return collection with 500 results

There is an ability to set next page token what allows to get next chunk of results

$comments = Comments::setPaginationStateToken($token)->getPage();

Get next page:

$comments = $comments->nextPage();

Get next page token:

$comments = $comments->getNextPageToken();

Append collection with next page`s result:

$comments->appendNextPage();

Check if it is last page:

$comments->isLastPage();

Get raw cassandra response for current page (\Cassandra\Rows):

$rows = $commants->getRows();

Read more about the query builder on http://laravel.com/docs/queries

Examples

  • store users data to csv
$users = User::setPageSize(1000)->getPage();
while(!$users->isLastPage()) {
    foreach($users as $user) {
        // here you can write a lines to csv file
    }

    $users = $users->nextPage();
}
  • Simple api to make Load more as paggination on page
public function getComments(Request $request) {
    ...

    $comments = Comment::setPageSize(50)
        ->setPaginationStateToken($request->get('nextPageToken', null)
        ->getPage();

    ...

    return response()->json([
        ...
        'comments' => $comments,
        'nextPageToken' => !$comments->isLastPage() ? $comments->getNextPageToken() : null,
        ...
    ]);
}
  • If you use cassandra materialized views you can easily use it with eloquent models
$users = User::from('users_by_country_view')->where('country', 'USA')->get();

TODO:

[ ] full support of composite primary key [ ] full test coverage [ ] fix diff between \Cassandra\Date with Carbon [ ] add schema queries support [ ] add ability to use async queries

Docker:

There is docker-compose setup stored in package root. It can be used for local development and test running. Works well with PHPStorm testing tools + coverage.

Run command below inside of the "main" container to run tests and generate coverage file:

vendor/bin/phpunit --coverage-clover clover.xml

a-h-abid/eloquent-cassandra 适用场景与选型建议

a-h-abid/eloquent-cassandra 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 431 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「model」 「laravel」 「cassandra」 「eloquent」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 a-h-abid/eloquent-cassandra 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 a-h-abid/eloquent-cassandra 我们能提供哪些服务?
定制开发 / 二次开发

基于 a-h-abid/eloquent-cassandra 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 431
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-05