Lacassa
A Query builder with support for Cassandra, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods., (*1)
Table of contents
-
Installation, (*2)
-
Configuration, (*3)
-
Query Builder, (*4)
-
Schema, (*5)
-
Extensions, (*6)
-
Examples, (*7)
Installation
Make sure you have the DataStax PHP Driver for Apache Cassandra installed. You can find installation instructions at https://github.com/datastax/php-driver
or
https://github.com/datastax/php-driver/blob/master/ext/README.md, (*8)
datastax php-driver requires php version 5.6+, (*9)
Installation using composer:, (*10)
composer require cubettech/lacassa
And add the service provider in config/app.php:, (*11)
Cubettech\Lacassa\CassandraServiceProvider::class,
Configuration
Change your default database connection name in config/database.php:, (*12)
'default' => env('DB_CONNECTION', 'cassandra'),
And add a new cassandra connection:, (*13)
'cassandra' => [
'driver' => 'Cassandra',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 7199),
'keyspace' => env('DB_DATABASE', 'cassandra_db'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
],
Auth
You can use Laravel's native Auth functionality for cassandra, make sure your config/auth.php looks like, (*14)
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 'users',
],
],
Schema
The database driver also has (limited) schema builder support. You can easily manipulate tables and set indexes:, (*15)
Schema::create(
'users', function ($table) {
$table->int('id');
$table->text('name');
$table->text('email');
$table->text('password');
$table->text('remember_token');
$table->setCollection('phn', 'bigint');
$table->listCollection('hobbies', 'text');
$table->mapCollection('friends', 'text', 'text');
$table->primary(['id']);
});
DROP table, (*16)
Schema::drop('users');
CQL data types supported
text('a'), (*17)
bigint('b'), (*18)
blob('c'), (*19)
boolean('d'), (*20)
counter('e'), (*21)
decimal('f'), (*22)
double('g'), (*23)
float('h'), (*24)
frozen('i'), (*25)
inet('j'), (*26)
nt('k'), (*27)
listCollection('l', 'text'), (*28)
mapCollection('m', 'timestamp', 'text'), (*29)
setCollection('n', 'int'), (*30)
timestamp('o'), (*31)
timeuuid('p'), (*32)
tuple('q', 'int', 'text', 'timestamp'), (*33)
uuid('r'), (*34)
varchar('s'), (*35)
varint('t'), (*36)
ascii('u'), (*37)
Primary Key, (*38)
primary(['a', 'b']), (*39)
Query Builder, (*40)
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., (*41)
$emp = DB::table('emp')->get();
$emp = DB::table('emp')->where('emp_name', 'Christy')->first();
If you did not change your default database connection, you will need to specify it when querying., (*42)
$emp = DB::connection('cassandra')->table('emp')->get();
Examples, (*43)
Basic Usage
Retrieving All Records, (*44)
$emp = DB::table('emp')->all();
Indexing columns, (*45)
CREATE INDEX creates a new index on the given table for the named column., (*46)
DB::table('users')->index(['name']);
Selecting columns, (*47)
$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']);
Wheres, (*48)
The WHERE clause specifies which rows to query. In the WHERE clause, refer to a column using the actual name, not an alias. Columns in the WHERE clause need to meet one of these requirements:, (*49)
-
The partition key definition includes the column., (*50)
-
A column that is indexed using CREATE INDEX., (*51)
$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();
And Statements, (*52)
$emp = DB::table('emp')->where('emp_no', '>', 50)->where('emp_name', '=', 'Christy')->get();
Using Where In With An Array, (*53)
$emp = DB::table('emp')->whereIn('emp_no', [12, 17, 21])->get();
Order By, (*54)
ORDER BY clauses can select a single column only. Ordering can be done in ascending or descending order, default ascending, and specified with the ASC or DESC keywords. In the ORDER BY clause, refer to a column using the actual name, not the aliases., (*55)
$emp = DB::table('emp')->where('emp_name','Christy')->orderBy('emp_no', 'desc')->get();
Limit, (*56)
We can use limit() and take() for limiting the query., (*57)
$emp = DB::table('emp')->where('emp_no', '>', 50)->take(10)->get();
$emp = DB::table('emp')->where('emp_no', '>', 50)->limit(10)->get();
Distinct, (*58)
Distinct requires a field for which to return the distinct values., (*59)
$emp = DB::table('emp')->distinct()->get(['emp_id']);
Distinct can be combined with where:, (*60)
$emp = DB::table('emp')->where('emp_sal', 45000)->distinct()->get(['emp_name']);
Count, (*61)
$number = DB::table('emp')->count();
Count can be combined with where:, (*62)
$sal = DB::table('emp')->where('emp_sal', 45000)->count();
Truncate, (*63)
$sal = DB::table('emp')->truncate();
Filtering a collection set, list, or map
You can index the collection column, and then use the CONTAINS condition in the WHERE clause to filter the data for a particular value in the collection., (*64)
$emp = DB::table('emp')->where('emp_name','contains', 'Christy')->get();
After indexing the collection keys in the venues map, you can filter on map keys., (*65)
$emp = DB::table(emp')->where('todo','contains key', '2014-10-02 06:30:00+0000')->get();
Raw Query, (*66)
The CQL expressions can be injected directly into the query., (*67)
$emp = DB::raw('select * from emp');
Inserts, updates and deletes, (*68)
Inserting, updating and deleting records works just like the original QB., (*69)
Insert, (*70)
DB::table('emp')->insert(['emp_id' => 11, 'emp_city' => '{"kochi", "tvm", "kollam"}', 'emp_name' => 'Christy', 'emp_phone' => 12345676890, 'emp_sal' => 500]);
Updating, (*71)
To update a model, you may retrieve it, change an attribute, and use the update method., (*72)
DB::table('emp')->where('emp_id', 11)->update(['emp_city' => 'kochi', 'emp_name' => 'Christy jos', 'emp_phone' => 1234567890]);
Updating a collection set, list, and map
Update collections in a row. The method will be like, (*73)
updateCollection(collection_type, column_name, operator, value);
Collection_type is any of set, list or map., (*74)
Column_name is the name of column to be updated., (*75)
Operator is + or -, + for adding the values to collection and - to remove the value from collection., (*76)
Value can be associative array for map type and array of string/number for list and set types., (*77)
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();
Deleting, (*78)
To delete a model, simply call the delete method on the instance. We can delete the rows in a table by using deleteRow method:, (*79)
$emp = DB::table('emp')->where('emp_city', 'Kochi')->deleteRow();
We can also perform delete by the column in a table using deleteColumn method:, (*80)
$emp = DB::table('emp')->where('emp_id', 3)->deleteColumn();