homegear/laravel-cassandra

Laravel 的基于Cassandra的查询构建器。

dev-master 2018-11-28 15:18 UTC

This package is not auto-updated.

Last update: 2020-08-06 18:31:18 UTC


README

基于Jens Segers的https://github.com/jenssegers/laravel-mongodb和Shahin Sorkh的https://github.com/ShahinSorkh/laravel-cassandra

支持Cassandra的查询构建器,使用原始的Laravel API。此库扩展了原始Laravel类,因此使用完全相同的方法。

目录

  • 安装

  • 配置

  • 查询构建器

  • 模式

  • 扩展

  • 示例

安装

请确保已安装Apache Cassandra的数据Stax PHP驱动。您可以在https://github.com/datastax/php-driverhttps://github.com/datastax/php-driver/blob/master/ext/README.md找到安装说明。

注意:datastax php-driver仅与php版本5.6.*, 7.0.*和7.1.*兼容

使用composer安装

在config/app.php中添加服务提供者

# config/app.php
...
providers: [
    ...,
    ib\Cassandra\CassandraServiceProvider::class,
    ...,
],
...

配置

更改config/database.php中的默认数据库连接名称

# config/database.php
'default' => env('DB_CONNECTION', 'cassandra'),

并添加新的Cassandra连接

# config/database.php
'cassandra' => [
    'driver' => 'cassandra',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', 9042),
    'keyspace' => env('DB_DATABASE', 'cassandra_db'),
    'username' => env('DB_USERNAME', ''),
    'password' => env('DB_PASSWORD', ''),
    'page_size' => '5000',
    'consistency' => 'local_one',
    'timeout' => null,
    'connect_timeout' => 5.0,
    'request_timeout' => 12.0,
],

注意:您可以像以下这样输入所有节点:

# .env
DB_HOST=192.168.100.140,192.168.100.141,192.168.100.142

注意:您可以选择以下一致性级别之一:

anythreelocal_qourumlocal_one
oneqourumeach_qourumserial
twoalllocal_serial

查询构建器

数据库驱动程序直接集成到原始查询构建器中。使用cassandra连接时,您将能够构建流畅的查询以执行数据库操作。

$emp = DB::table('emp')->get();
$emp = DB::table('emp')->where('emp_name', 'Christy')->first();

如果您未更改默认数据库连接,则需要在每个查询中指定它。

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

基本用法

检索所有记录

$emp = DB::table('emp')->all();

索引列

CREATE INDEX在给定的表上为命名列创建一个新索引。

DB::table('users')->index(['name']);

选择列

$emp = DB::table('emp')->where('emp_no', '>', 50)->select('emp_name', 'emp_no')->get();
$emp = DB::table('emp')->where('emp_no', '>', 50)->get(['emp_name', 'emp_no']);

WHERE子句

WHERE子句指定要查询的行。在WHERE子句中,使用实际名称引用列,而不是别名。WHERE子句中的列需要满足以下要求之一

  • 分区键定义包括该列。

  • 使用CREATE INDEX索引的列。

$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();

AND语句

$emp = DB::table('emp')->where('emp_no', '>', 50)->where('emp_name', '=', 'Christy')->get();

使用数组中的WHERE IN

$emp = DB::table('emp')->whereIn('emp_no', [12, 17, 21])->get();

ORDER BY

ORDER BY子句只能选择一个列。排序可以是升序或降序,默认为升序,可以用ASC或DESC关键字指定。在ORDER BY子句中,使用实际名称引用列,而不是别名。

$emp = DB::table('emp')->where('emp_name', 'Christy')->orderBy('emp_no', 'desc')->get();

Limit

我们可以使用limit()和take()来限制查询。

$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();
$emp = DB::table('emp')->where('emp_no', '>', 50)->limit(10)->get();

Distinct

Distinct需要一个主键字段来返回不同的值。

$emp = DB::table('emp')->distinct()->get(['emp_id']);

Distinct可以与where结合使用

$emp = DB::table('emp')->where('emp_sal', 45000)->distinct()->get(['emp_name']);

Count

$number = DB::table('emp')->count();

Count可以与where结合使用

$sal = DB::table('emp')->where('emp_sal', 45000)->count();

Truncate

$sal = DB::table('emp')->truncate();

过滤集合集、列表或映射

您可以对集合列进行索引,然后在WHERE子句中使用CONTAINS条件来过滤特定值的数据。

$emp = DB::table('emp')->where('emp_name', 'contains', 'Christy')->get();

索引venues映射中的集合键后,您可以过滤映射键。

$emp = DB::table('emp')->where('todo', 'contains key', '2014-10-02 06:30:00+0000')->get();

原始查询

CQL表达式可以直接注入到查询中。

$emp = DB::raw('select * from emp');

插入、更新和删除

插入、更新和删除记录的操作与原始QB完全相同。

插入

DB::table('emp')
    ->insertCollection('set', 'phn', [123, 1234, 12345])
    ->insertCollection('map', 'friends', [['John', 'Male'], ['Eli', 'Female']])
    ->insert([
        'emp_id' => 11,
        'emp_name' => 'Christy',
        'emp_phone' => 12345676890,
        'emp_sal' => 500
    ]);

更新

要更新一个模型,您可以检索它,更改一个属性,然后使用更新方法。

DB::table('emp')
    ->where('emp_id', 11)
    ->update([
        'emp_city' => 'kochi',
        'emp_name' => 'Christy jos',
        'emp_phone' =>  123456789
    ]);

更新集合集、列表和映射

逐行更新集合。方法如下

updateCollection(collection_type, column_name, operator, value);

Collection_type可以是set、list或map中的任何一个。

Column_name是要更新的列的名称。

Operator是+或-,+用于将值添加到集合中,-用于从集合中删除值。

Value对于map类型可以是关联数组,对于list和set类型可以是字符串/数字数组。

DB::table('users')->where('id', 1)
    ->updateCollection('set', 'phn', '+', [123, 1234,12345])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('set', 'phn', '-', [123])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('list', 'hobbies', '+', ['reading', 'cooking', 'cycling'])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('set', 'phn', '+', [123, 1234,12345])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('set', 'phn', '-', [123])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('list', 'hobbies', '+', ['reading', 'cooking', 'cycling'])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('list', 'hobbies', '-', ['cooking'])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('map', 'friends', '+', [['John', 'Male'], ['Rex', 'Male']])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('map', 'friends', '-', ['John'])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('map', 'friends', '+', [['John', 'Male'], ['Rex', 'Male']])->update();

DB::table('users')->where('id', 1)
    ->updateCollection('map', 'friends', '-', ['John'])->update();

删除

要删除一个模型,只需在实例上调用delete方法。我们可以使用deleteRow方法删除表中的行

$emp = DB::table('emp')->where('emp_city', 'Kochi')->deleteRow();

我们还可以使用deleteColumn方法通过表中的列执行删除

$emp = DB::table('emp')->where('emp_id', 3)->deleteColumn();

测试

为测试,运行以下命令一次

$ php ./prepare_db.php

这将创建一个名为testing的键空间,一个名为users的表,以及两个名为users_by_usernameusers_by_email的物化视图,还有一个名为posts的表和一个名为posts_by_month的物化视图。您可以在prepare_db.php文件中查看完整的模式。

然后运行phpunit

# `pwd` = <project root>
$ ./vendor/bin/phpunit tests