Developed with love by KnpLabs Hire us for your project!
50

CassandraBundle

by M6Web

Symfony bundle on top of the datastax cassandra component

CassandraBundle

Build Status

The CassandraBundle provide a Cassandra client as a Symfony service.

Installation

NOTE : You need to install the offical datastax php driver extension

Use composer:

composer require m6web/cassandra-bundle

Then make sure the bundle is registered in your application:

// config/bundles.php

return [
    \M6Web\Bundle\CassandraBundle\M6WebCassandraBundle::class => ['all' => true],
];

Usage

Add the m6web_cassandra section in your configuration file. Here is the minimal configuration required.

m6web_cassandra:
    clients:
        myclient:
            contact_endpoints:
                - 127.0.0.1
                - 127.0.0.2
                - 127.0.0.3

Then you can ask container for your client :

$cassandra = $this->get('m6web_cassandra.client.myclient');

$prepared = $cassandra->prepare("INSERT INTO test (id, title) VALUES(?, ?)");

$batch     = new Cassandra\BatchStatement(Cassandra::BATCH_LOGGED);
$batch->add($prepared, ['id' => 1, 'title' => 'my title']);
$batch->add($prepared, ['id' => 2, 'title' => 'my title 2']);

$cassandra->execute($batch);

$statement = new Cassandra\SimpleStatement('SELECT * FROM test');
$result = $cassandra->execute($statement);

foreach ($result as $row) {
    // do something with $row
}

$statement = new Cassandra\SimpleStatement('SELECT * FROM test');
$result = $cassandra->executeAsync($statement);

// do something while cassandra query running

foreach($result->get() as $row) {
    // do something with row
}

Bundle provide a util class for extracting Datetime from a timeuuid string.

use M6Web\Bundle\CassandraBundle\Cassandra\Type as TypeUtils;

$datetime = TypeUtils::getDateTimeFromTimeuuidString('513a5340-6da0-11e5-815e-93ec150e89fd');

if (is_null($datetime)) {
    // something is wrong with supplied uuid
} else {
    echo $datetime->format(\DateTime::W3C); // 2015-10-08 11:38:22+02:00
}

DataCollector

Datacollector is available when the symfony profiler is enabled. The collector allows you to see the following Cassandra data :

  • keyspace
  • command name
  • command arguments
  • execution time
  • execution options override (consistency, serial consistency, page size and timeout)

NOTE : The time reported in the data collector may not be the real execution time in case you use the async calls : executeAsync and prepareAsync

Configuration reference

m6web_cassandra:
    dispatch_events: true                 # By default event are triggered on each cassandra command
    clients:
        client_name:
            persistent_sessions: true     # persistent session connection 
            keyspace: "mykeyspace"        # default is null, optional keyspace to connect
            load_balancing: "round-robin" # round-robin or dc-aware-round-robin
            dc_options:                   # required if load balancing is set to dc-aware-round-robin
                local_dc_name: "testdc"
                host_per_remote_dc: 3
                remote_dc_for_local_consistency: false
            default_consistency: "one"    # 'one', 'any', 'two', 'three', 'quorum', 'all', 'local_quorum', 'each_quorum', 'serial', 'local_serial', 'local_one'
            default_pagesize: 10000       # ~ to disable pagination
            contact_endpoints:            # required list of ip to contact
                - 127.0.0.1
            contact_whitelist:            # if specified cassandra will connect only to this datacenters/hosts
                dc:
                    - "testdc"
                host:
                    - 172.0.0.1
            contact_blacklist:            # if specified cassandra will not connect to this datacenters/hosts
                dc:
                    - "blacklisted_testdc"
                host:
                    - 6.6.6.6
            port_endpoint: 9042           # cassandra port
            token_aware_routing: true     # Enable or disable token aware routing
            credentials:                  # cassandra authentication
                username: ""              # username for authentication
                password: ""              # password for authentication
            ssl: false                    # set up ssl context
            default_timeout: null         # default is null, must be an integer if set
            timeout:
                connect: 5 
                request: 5 
            retries:
                sync_requests: 0          # Number of retries for synchronous requests. Default is 0, must be an integer if set

        client_name:
            ...

Contributing

First of all, thank you for contributing !

Here are few rules to follow for a easier code review before the maintainers accept and merge your request.

  • you MUST follow the Symfony2 coding standard : you can use ./bin/coke to validate
  • you MUST run the test
  • you MUST write or update tests
  • you MUST write or update documentation

Running the test

Install the composer dev dependencies

$ composer install --dev

Then run the test with atoum unit test framework

./bin/atoum
The MIT License (MIT)

Copyright (c) 2014 M6Web

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • Symfony BC (#44)
    By web-flow, 4 months ago
  • Merge pull request #45 from M6Web/fix-ci
    By web-flow, 4 months ago
  • fix(cs): fix and add bin/coke script to Travis build
    By Oliver Thébault, 4 months ago
  • chore(ci): fix Travis build
    By Oliver Thébault, 4 months ago
  • Merge pull request #42 from M6Web/feature/req-php7
    By web-flow, 1 year ago
  • update php requirements
    By omansour, 1 year ago
  • Merge pull request #36 from benoitblin/master
    By web-flow, 2 years ago
  • Merge pull request #38 from liacmedia/master
    By web-flow, 2 years ago
  • Dependency Injection, setPublic for SF4
    By web-flow, 2 years ago
  • Patch for Symfony4 error on CassandraDataCollector template naming
    By bblin, 2 years ago
  • Merge pull request #35 from M6Web/fix/compat-sf4
    By web-flow, 2 years ago
  • Add CassandraDataCollector::reset() method required for Symfony 4
    By web-flow, 2 years ago
  • Merge pull request #33 from metricso/master
    By web-flow, 3 years ago
  • improved config readability
    By therpr, 3 years ago
  • adds whitelist & blacklist config properties
    By therpr, 3 years ago
  • Merge pull request #32 from M6Web/fix/compatible-driver-1.3.0
    By web-flow, 3 years ago
  • Fix deprecated ExecutionOptions
    By g-millet, 3 years ago
  • Merge pull request #30 from M6Web/fix/compatible-driver-1.3.0
    By web-flow, 3 years ago
  • Fix errors with driver 1.3.0
    By Gael Millet, 3 years ago
  • Merge pull request #29 from jubianchi/update-atoum
    By web-flow, 3 years ago
  • Update to newer atoum versions
    By jubianchi, 3 years ago
  • Merge pull request #28 from M6Web/feature/doc-allow-null-keyspace
    By web-flow, 3 years ago
  • Documentation : Allow keyspace to be null
    By n_saby, 3 years ago
  • Merge pull request #27 from M6Web/feature/keyspace-and-client-management
    By web-flow, 3 years ago
  • Allow keyspace to be null
    By n_saby, 3 years ago
  • Merge pull request #25 from Coservit/master
    By web-flow, 3 years ago
  • Modifier la valeur à saisir pour desactiver la pagination
    By Jimmy KLEIN, 3 years ago
  • Merge pull request #24 from M6Web/feature/test-multi-sf-versions
    By web-flow, 4 years ago
  • Run tests against sf 2.3/2.7/3.0
    By Oliboy50, 4 years ago
  • Merge pull request #23 from GregoireHebert/master
    By web-flow, 4 years ago