Laravel Cassandra
Content
Integrate Cassandra into your Laravel app, using Laravel's own API and methods., (*1)
Table of contents
Installation
Make sure you have the Cassandra PHP driver installed. https://github.com/datastax/php-driver, (*2)
Installation using composer:, (*3)
composer require sonvq/cassandra
Laravel version Compatibility
Laravel |
Package |
5.2.x |
1.0.x |
Add Cassandra service provider in config/app.php
:, (*4)
sonvq\Cassandra\CassandraServiceProvider::class,
For usage with Lumen, add the service provider in bootstrap/app.php
. In this file, you will also need to enable Eloquent. You must however ensure that your call to $app->withEloquent();
is below where you have registered the CassandraServiceProvider
:, (*5)
$app->register('sonvq\Cassandra\CassandraServiceProvider');
$app->withEloquent();
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., (*6)
Configuration
Change your default database connection name in app/config/database.php
:, (*7)
'default' => env('DB_CONNECTION', 'cassandra'),
And add a new cassandra connection:, (*8)
'cassandra' => [
'driver' => 'cassandra',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'options' => [
'db' => 'admin' // sets the authentication database required by cassandra 3
]
],
You can connect to multiple servers or replica sets with the following configuration:, (*9)
'cassandra' => [
'driver' => 'cassandra',
'host' => ['server1', 'server2'],
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'options' => ['replicaSet' => 'replicaSetName']
],
Eloquent
This package includes a Cassandra enabled Eloquent class that you can use to define models for corresponding collections., (*10)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {}
Note that we did not tell Eloquent which collection to use for the User
model. Just like the original Eloquent, the lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. You may specify a custom collection (alias for table) by defining a collection
property on your model:, (*11)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {
protected $collection = 'users_collection';
}
NOTE: Eloquent will also assume that each collection has a primary key column named id. You may define a primaryKey
property to override this convention. Likewise, you may define a connection
property to override the name of the database connection that should be used when utilizing the model., (*12)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class MyModel extends Eloquent {
protected $connection = 'cassandra';
}
Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent, (*13)
Optional: Alias
You may also register an alias for the Cassandra model by adding the following to the alias array in app/config/app.php
:, (*14)
'Moloquent' => 'sonvq\Cassandra\Eloquent\Model',
This will allow you to use the registered alias like:, (*15)
class MyModel extends Moloquent {}
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. For your convenience, there is a collection
alias for table
as well as some additional cassandra specific operators/operations., (*16)
$users = DB::collection('users')->get();
$user = DB::collection('users')->where('name', 'John')->first();
If you did not change your default database connection, you will need to specify it when querying., (*17)
$user = DB::connection('cassandra')->collection('users')->get();
Read more about the query builder on http://laravel.com/docs/queries, (*18)
Schema
The database driver also has (limited) schema builder support. You can easily manipulate collections and set indexes:, (*19)
Schema::create('users', function($collection)
{
$collection->index('name');
$collection->unique('email');
});
Supported operations are:, (*20)
- create and drop
- collection
- hasCollection
- index and dropIndex (compound indexes supported as well)
- unique
- background, sparse, expire (Cassandra specific)
All other (unsupported) operations are implemented as dummy pass-through methods, because Cassandra does not use a predefined schema. Read more about the schema builder on http://laravel.com/docs/schema, (*21)
Extensions
Auth
If you want to use Laravel's native Auth functionality, register this included service provider:, (*22)
'sonvq\Cassandra\Auth\PasswordResetServiceProvider',
This service provider will slightly modify the internal DatabaseReminderRepository to add support for Cassandra based password reminders. If you don't use password reminders, you don't have to register this service provider and everything else should work just fine., (*23)
Queues
If you want to use Cassandra as your database backend, change the the driver in config/queue.php
:, (*24)
'connections' => [
'database' => [
'driver' => 'cassandra',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
Examples
Basic Usage
Retrieving All Models, (*25)
$users = User::all();
Retrieving A Record By Primary Key, (*26)
$user = User::find('517c43667db388101e00000f');
Wheres, (*27)
$users = User::where('votes', '>', 100)->take(10)->get();
Or Statements, (*28)
$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
And Statements, (*29)
$users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();
Using Where In With An Array, (*30)
$users = User::whereIn('age', [16, 18, 20])->get();
When using whereNotIn
objects will be returned if the field is non existent. Combine with whereNotNull('age')
to leave out those documents., (*31)
Using Where Between, (*32)
$users = User::whereBetween('votes', [1, 100])->get();
Where null, (*33)
$users = User::whereNull('updated_at')->get();
Order By, (*34)
$users = User::orderBy('name', 'desc')->get();
Offset & Limit, (*35)
$users = User::skip(10)->take(5)->get();
Distinct, (*36)
Distinct requires a field for which to return the distinct values., (*37)
$users = User::distinct()->get(['name']);
// or
$users = User::distinct('name')->get();
Distinct can be combined with where:, (*38)
$users = User::where('active', true)->distinct('name')->get();
Advanced Wheres, (*39)
$users = User::where('name', '=', 'John')->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
Group By, (*40)
Selected columns that are not grouped will be aggregated with the $last function., (*41)
$users = Users::groupBy('title')->get(['title', 'name']);
Aggregation, (*42)
Aggregations are only available for Cassandra versions greater than 2.2., (*43)
$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');
Aggregations can be combined with where:, (*44)
$sold = Orders::where('sold', true)->sum('price');
Like, (*45)
$user = Comment::where('body', 'like', '%spam%')->get();
Incrementing or decrementing a value of a column, (*46)
Perform increments or decrements (default 1) on specified attributes:, (*47)
User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);
The number of updated objects is returned:, (*48)
$count = User->increment('age');
You may also specify additional columns to update:, (*49)
User::where('age', '29')->increment('age', 1, ['group' => 'thirty something']);
User::where('bmi', 30)->decrement('bmi', 1, ['category' => 'overweight']);
Soft deleting, (*50)
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, apply the SoftDeletingTrait to the model:, (*51)
use sonvq\Cassandra\Eloquent\SoftDeletes;
class User extends Eloquent {
use SoftDeletes;
protected $dates = ['deleted_at'];
}
For more information check http://laravel.com/docs/eloquent#soft-deleting, (*52)
Cassandra specific operators
Exists, (*53)
Matches documents that have the specified field., (*54)
User::where('age', 'exists', true)->get();
All, (*55)
Matches arrays that contain all elements specified in the query., (*56)
User::where('roles', 'all', ['moderator', 'author'])->get();
Size, (*57)
Selects documents if the array field is a specified size., (*58)
User::where('tags', 'size', 3)->get();
Regex, (*59)
Selects documents where values match a specified regular expression., (*60)
User::where('name', 'regex', new CassandraRegex("/.*doe/i"))->get();
NOTE: you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a CassandraRegex object., (*61)
User::where('name', 'regexp', '/.*doe/i'))->get();
And the inverse:, (*62)
User::where('name', 'not regexp', '/.*doe/i'))->get();
Type, (*63)
Selects documents if a field is of the specified type. For more information check: http://docs.cassandra.org/manual/reference/operator/query/type/#op._S_type, (*64)
User::where('age', 'type', 2)->get();
Mod, (*65)
Performs a modulo operation on the value of a field and selects documents with a specified result., (*66)
User::where('age', 'mod', [10, 0])->get();
Where, (*67)
Matches documents that satisfy a JavaScript expression. For more information check http://docs.cassandra.org/manual/reference/operator/query/where/#op._S_where, (*68)
Inserts, updates and deletes
Inserting, updating and deleting records works just like the original Eloquent., (*69)
Saving a new model, (*70)
$user = new User;
$user->name = 'John';
$user->save();
You may also use the create method to save a new model in a single line:, (*71)
User::create(['name' => 'John']);
Updating a model, (*72)
To update a model, you may retrieve it, change an attribute, and use the save method., (*73)
$user = User::first();
$user->email = 'john@foo.com';
$user->save();
There is also support for upsert operations, check https://github.com/sonvq/laravel-cassandra#cassandra-specific-operations, (*74)
Deleting a model, (*75)
To delete a model, simply call the delete method on the instance:, (*76)
$user = User::first();
$user->delete();
Or deleting a model by its key:, (*77)
User::destroy('517c43667db388101e00000f');
For more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete, (*78)
Dates
Eloquent allows you to work with Carbon/DateTime objects instead of CassandraDate objects. Internally, these dates will be converted to CassandraDate objects when saved to the database. If you wish to use this functionality on non-default date fields you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators, (*79)
Example:, (*80)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {
protected $dates = ['birthday'];
}
Which allows you to execute queries like:, (*81)
$users = User::where('birthday', '>', new DateTime('-18 years'))->get();
Relations
Supported relations are:, (*82)
- hasOne
- hasMany
- belongsTo
- belongsToMany
- embedsOne
- embedsMany
Example:, (*83)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {
public function items()
{
return $this->hasMany('Item');
}
}
And the inverse relation:, (*84)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class Item extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
The belongsToMany relation will not use a pivot "table", but will push id's to a related_ids attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to null
:, (*85)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
}
}
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://laravel.com/docs/eloquent#relationships, (*86)
EmbedsMany Relations
If you want to embed models, rather than referencing them, you can use the embedsMany
relation. This relation is similar to the hasMany
relation, but embeds the models inside the parent object., (*87)
REMEMBER: these relations return Eloquent collections, they don't return query builder objects!, (*88)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class User extends Eloquent {
public function books()
{
return $this->embedsMany('Book');
}
}
You access the embedded models through the dynamic property:, (*89)
$books = User::first()->books;
The inverse relation is automagically available, you don't need to define this reverse relation., (*90)
$user = $book->user;
Inserting and updating embedded models works similar to the hasMany
relation:, (*91)
$book = new Book(['title' => 'A Game of Thrones']);
$user = User::first();
$book = $user->books()->save($book);
// or
$book = $user->books()->create(['title' => 'A Game of Thrones'])
You can update embedded models using their save
method (available since release 2.0.0):, (*92)
$book = $user->books()->first();
$book->title = 'A Game of Thrones';
$book->save();
You can remove an embedded model by using the destroy
method on the relation, or the delete
method on the model (available since release 2.0.0):, (*93)
$book = $user->books()->first();
$book->delete();
// or
$user->books()->destroy($book);
If you want to add or remove an embedded model, without touching the database, you can use the associate
and dissociate
methods. To eventually write the changes to the database, save the parent object:, (*94)
$user->books()->associate($book);
$user->save();
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:, (*95)
return $this->embedsMany('Book', 'local_key');
Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections, (*96)
EmbedsOne Relations
The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model., (*97)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class Book extends Eloquent {
public function author()
{
return $this->embedsOne('Author');
}
}
You access the embedded models through the dynamic property:, (*98)
$author = Book::first()->author;
Inserting and updating embedded models works similar to the hasOne
relation:, (*99)
$author = new Author(['name' => 'John Doe']);
$book = Books::first();
$author = $book->author()->save($author);
// or
$author = $book->author()->create(['name' => 'John Doe']);
You can update the embedded model using the save
method (available since release 2.0.0):, (*100)
$author = $book->author;
$author->name = 'Jane Doe';
$author->save();
You can replace the embedded model with a new model like this:, (*101)
$newAuthor = new Author(['name' => 'Jane Doe']);
$book->author()->save($newAuthor);
MySQL Relations
If you're using a hybrid Cassandra and SQL setup, you're in luck! The model will automatically return a Cassandra- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-models will need use the sonvq\Cassandra\Eloquent\HybridRelations
trait. Note that this functionality only works for hasOne, hasMany and belongsTo relations., (*102)
Example SQL-based User model:, (*103)
use sonvq\Cassandra\Eloquent\HybridRelations;
class User extends Eloquent {
use HybridRelations;
protected $connection = 'mysql';
public function messages()
{
return $this->hasMany('Message');
}
}
And the Cassandra-based Message model:, (*104)
use sonvq\Cassandra\Eloquent\Model as Eloquent;
class Message extends Eloquent {
protected $connection = 'cassandra';
public function user()
{
return $this->belongsTo('User');
}
}
Raw Expressions
These expressions will be injected directly into the query., (*105)
User::whereRaw(['age' => array('$gt' => 30, '$lt' => 40]))->get();
You can also perform raw expressions on the internal CassandraCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response., (*106)
// Returns a collection of User models.
$models = User::raw(function($collection)
{
return $collection->find();
});
// Returns the original CassandraCursor.
$cursor = DB::collection('users')->raw(function($collection)
{
return $collection->find();
});
Optional: if you don't pass a closure to the raw method, the internal CassandraCollection object will be accessible:, (*107)
$model = User::raw()->findOne(['age' => array('$lt' => 18]));
The internal CassandraClient and Cassandra objects can be accessed like this:, (*108)
$client = DB::getCassandraClient();
$db = DB::getCassandra();
Cassandra specific operations
Cursor timeout, (*109)
To prevent CassandraCursorTimeout exceptions, you can manually set a timeout value that will be applied to the cursor:, (*110)
DB::collection('users')->timeout(-1)->get();
Upsert, (*111)
Update or insert a document. Additional options for the update method are passed directly to the native update method., (*112)
DB::collection('users')->where('name', 'John')
->update($data, ['upsert' => true]);
Projections, (*113)
You can apply projections to your queries using the project
method., (*114)
DB::collection('items')->project(['tags' => array('$slice' => 1]))->get();
Projections with Pagination, (*115)
$limit = 25;
$projections = ['id', 'name'];
DB::collection('items')->paginate($limit, $projections);
Push, (*116)
Add an items to an array., (*117)
DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('messages', ['from' => 'Jane Doe', 'message' => 'Hi John']);
If you don't want duplicate items, set the third parameter to true
:, (*118)
DB::collection('users')->where('name', 'John')->push('items', 'boots', true);
Pull, (*119)
Remove an item from an array., (*120)
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('messages', ['from' => 'Jane Doe', 'message' => 'Hi John']);
Unset, (*121)
Remove one or more fields from a document., (*122)
DB::collection('users')->where('name', 'John')->unset('note');
You can also perform an unset on a model., (*123)
$user = User::where('name', 'John')->first();
$user->unset('note');
Query Caching
You may easily cache the results of a query using the remember method:, (*124)
$users = User::remember(10)->get();
From: http://laravel.com/docs/queries#caching-queries, (*125)
Query Logging
By default, Laravel keeps a log in memory of all queries that have been run for the current request. However, in some cases, such as when inserting a large number of rows, this can cause the application to use excess memory. To disable the log, you may use the disableQueryLog
method:, (*126)
DB::connection()->disableQueryLog();
From: http://laravel.com/docs/database#query-logging, (*127)