DataStax Developer Blog

PHP Driver 1.0 GA

By Bulat Shakirzyanov -  September 14, 2015 | 0 Comments

Hello PHPeople!

Today marks the first stable release of the DataStax PHP Driver for Apache Cassandra and DataStax Enterprise. Refer to the PHP Driver documentation for compatibility information.

With the very first beta coming out merely four months ago, the driver has come a long way with the help and numerous contributions from the community.

This release of the DataStax PHP Driver for Apache Cassandra and DataStax Enterprise includes two new features in addition to everything introduced in the 1.0 release candidate release:

Schema Metadata

The DataStax PHP Driver allows inspecting schema of the Cassandra cluster that it is connected to. Call Cassandra\Session::schema() to get ahold of Cassandra\Schema. From there you can access all or individual keyspace metadata using Cassandra\Schema::keyspaces() or Cassandra\Schema::keyspace() accordingly.

The example below gives an adequate taste of what’s possible:


$schema = $session->schema();

foreach ($schema->keyspaces() as $keyspace) {
    printf("Keyspace: %s\n", $keyspace->name());
    printf("    Replication Strategy: %s\n", $keyspace->replicationClassName());
    printf("    Replication Options:\n");
    $options = $keyspace->replicationOptions();
    $keys    = $options->keys();
    $values  = $options->values();
    foreach (array_combine($keys, $values) as $key => $value) {
        printf("        %s: %s\n", $key, $value);
    printf("    Durable Writes:       %s\n", $keyspace->hasDurableWrites() ? 'true' : 'false');

    foreach ($keyspace->tables() as $table) {
        printf("    Table: %s\n", $table->name());
        printf("        Comment:                    %s\n", $table->comment());
        printf("        Read Repair Chance:         %f\n", $table->readRepairChance());
        printf("        Local Read Repair Chance:   %f\n", $table->localReadRepairChance());
        printf("        GC Grace Seconds:           %d\n", $table->gcGraceSeconds());
        printf("        Caching:                    %s\n", $table->caching());
        printf("        Bloom Filter FP Chance:     %f\n", $table->bloomFilterFPChance());
        printf("        Memtable Flush Period Ms:   %d\n", $table->memtableFlushPeriodMs());
        printf("        Default Time To Live:       %d\n", $table->defaultTTL());
        printf("        Speculative Retry:          %s\n", $table->speculativeRetry());
        printf("        Index Interval:             %d\n", $table->indexInterval());
        printf("        Compaction Strategy:        %s\n", $table->compactionStrategyClassName());
        printf("        Populate IO Cache On Flush: %s\n", $table->populateIOCacheOnFlush() ? 'yes' : 'no');
        printf("        Replicate On Write:         %s\n", $table->replicateOnWrite() ? 'yes' : 'no');
        printf("        Max Index Interval:         %d\n", $table->maxIndexInterval());
        printf("        Min Index Interval:         %d\n", $table->minIndexInterval());

        foreach ($table->columns() as $column) {
            printf("        Column: %s\n", $column->name());
            printf("            Type:          %s\n", $column->type());
            printf("            Order:         %s\n", $column->isReversed() ? 'desc' : 'asc');
            printf("            Frozen:        %s\n", $column->isFrozen() ? 'yes' : 'no');
            printf("            Static:        %s\n", $column->isStatic() ? 'yes' : 'no');

            if ($column->indexName()) {
                printf("            Index:         %s\n", $column->indexName());
                printf("            Index Options: %s\n", $column->indexOptions());

Types API

With the introduction of Schema Metadata it is necessary to let users inspect the type of columns defined in a given keyspace.

The newly introduced Cassandra\Type interface defines the contract for the methods that all types must support as well as declares a number of static methods for fluent type definition and creation.

Using the Type API you can define and create Maps, Sets and Collections as well as validate and coerce data into all supported scalar types.

A picture is worth a thousand words:


// define and instantiate a map<varchar, int>
$map = Cassandra\Type::map(Cassandra\Type::varchar(), Cassandra\Type::int())
                     ->create('a', 1, 'b', 2, 'c', 3, 'd', 4);

var_dump(array_combine($map->keys(), $map->values()));

// validate the data
$value = Cassandra\Type::int()->create('this will throw an exception');

Install using PECL

The DataStax PHP Driver for Apache Cassandra and DataStax Enterprise is now available via PECL, install it by following installation instructions.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>