PHPackages                             intermaterium/cassandra-native - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. intermaterium/cassandra-native

ActiveLibrary[Database &amp; ORM](/categories/database)

intermaterium/cassandra-native
==============================

A native Apache Cassandra and ScyllaDB connector for PHP based on the CQL binary protocol with support for persistent connections

3.1.1(1y ago)2961↑150%[1 PRs](https://github.com/chrisBirmingham/Cassandra-Native/pulls)MITPHPPHP &gt;=8.0

Since Dec 16Pushed 1w agoCompare

[ Source](https://github.com/chrisBirmingham/Cassandra-Native)[ Packagist](https://packagist.org/packages/intermaterium/cassandra-native)[ RSS](/packages/intermaterium-cassandra-native/feed)WikiDiscussions main Synced yesterday

READMEChangelog (5)DependenciesVersions (17)Used By (0)

PHP CQL
=======

[](#php-cql)

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Native [Apache Cassandra](https://cassandra.apache.org) and [ScyllaDB](https://www.scylladb.com) connector for PHP applications using the CQL binary protocol (v4), without the need for an external extension.

Requires [PHP](https://www.php.net/) version &gt;=8, Cassandra &gt;1.2, and any ScyllaDB version.

Much of the API is built to emulate the [Datastax PHP Driver](https://docs.datastax.com/en/developer/php-driver/1.3/index.html). Original work by Uri Hartmann

Installation
------------

[](#installation)

```
$ composer require intermaterium/cassandra-native
```

Features
--------

[](#features)

- Simple and Prepared Statements
- SSL Encryption
- Persistent Connections
- Compression via LZ4.
- Authentication

Missing Features
----------------

[](#missing-features)

- Batch Statements
- Async queries
- Result Paging
- Tuples and User Defined Types

Usage
-----

[](#usage)

### Cluster

[](#cluster)

A Cassandra cluster can be built via the `ClusterBuilder` class. By default, the Cluster will try to connect to localhost.

```
$clusterBuilder = new \CassandraNative\Cluster\ClusterBuilder();
$cassandra = $clusterBuilder->build();
```

You can specify a set of IP/hostnames to connect to using the `withContactPoints` method.

The client will attempt to connect to one of the contact points at random. If the connection fails it will try another host until all contact points have been attempted or max connection attempts, configured with the `withMaxConnectionAttempts` method, has been reached, default is 3 attempts. If the client cannot connect to any of the provided hosts an `NoHostsAvailableException` is thrown.

```
$clusterBuilder = new \CassandraNative\Cluster\ClusterBuilder();
$clusterBuilder->withContactPoints(['1.0.0.0', '2.0.0.0']);
$cassandra = $clusterBuilder->build();
```

When connecting, the created Cassandra instance doesn't connect to a specific keyspace. Calling `connect` on the created Cassandra instance is the same as performing a `USE $keyspace` query against the connection.

```
$cassandra->connect('system');
```

### SSL

[](#ssl)

You can turn on SSL Encryption via the `SSLBuilder` class and pass the result of a call to the `build` method to the `withSSL`method of a cluster builder instance.

```
$sslBuilder = new \CassandraNative\SSL\SSLBuilder();
$sslBuilder
    ->withClientCert(__DIR__ . '/certs/localhost.cer')
    ->withPrivateKey(__DIR__ . '/certs/localhost.key.pem');
    ->withTrustedCerts(__DIR__ . '/certs/localhost.cer.pem');

$clusterBuilder->withSSL($sslBuilder->build());
```

### Compression

[](#compression)

Compression can be enabled by calling the `withCompression` method on the cluster builder.

```
$clusterBuilder->withCompression(true);
```

When enabled, the client checks to see if the LZ4 extension is loaded by PHP. If the extension is not loaded, an exception is thrown.

Originally on creation, the client would send an OPTIONS request and choose which compression algorithm to use based from the response. This uncovered an issue when using persistent connections. Unless we were using a cache, there wasn't a way to tell if the persistent connection had compression enabled unless we queried the cluster again, and Cassandra would return a compressed OPTIONS response before we had set the compressor.

To make things simpler, the client now assumes that the Cassandra cluster supports LZ4 compression when compression is requested and Snappy compression has been removed.

### Authentication

[](#authentication)

Authentication can be enabled by providing an Authentication Provider to the cluster build via the `withCredentials` method. With this library is the `PasswordAuthenticator` provider which accepts a plaintext username and password.

```
$authProvider = new \CassandraNative\Auth\PasswordAuthenticator('cassandra', 'cassandra');
$clusterBuilder->withCredentials($authProvider);
```

For other SASL based authentication methods you'll need to provide/use your own implementation. This can be done by creating a class which implements the `AuthProviderInterface`.

```
