Skip to content

Latest commit

 

History

History
116 lines (93 loc) · 4.27 KB

README.md

File metadata and controls

116 lines (93 loc) · 4.27 KB

Dart Driver for Apache Cassandra

Build Status Coverage Status

Dart driver for Apache Cassandra that supports Cassandra Query Language version 3.0+ (CQL3).

The driver has a small dependency tree and implements Cassandra binary protocol (versions 2 and 3) for communicating with Cassandra servers. The protocol and CQL versions to be used are both configurable by the user.

Installation

add the following dependency to install this plugin:\

dependency:
  dart_cassandra_cql:
    git: https://github.com/Sparks1998/dart_cassandra_cql

Features

  • Asynchronous API based on Future and Streams
  • Connection management via connection pools
  • Connection load-balancing and failover
  • Server event handling (node/topology/schema change events)
  • Query multiplexing on each connection
  • Batch and prepared queries with either positional or named placeholders
  • Query result streaming
  • Support for all Cassandra types including user defined types (UDT), tuples and custom types (via user-defined Codecs)

Quick start

import "dart:async";
import 'package:dart_cassandra_cql/dart_cassandra_cql.dart' as cql;

void main() {
  // Create a client for connecting to our cluster using native
  // protocol V3 and sensible defaults. The client will setup
  // a connection pool for you and connect automatically when
  // you execute a query.
  cql.Client client = cql.Client.fromHostList([
      "10.0.0.1:9042"
      , "10.0.0.2:9042"
  ]);

  // Perform a select with positional bindings
  client.query(
      cql.Query("SELECT * from test.type_test WHERE id=?", bindings : [123])
  ).then((Iterable<Map<String, Object>> rows) {
    // ...
  });

  // Perform an prepared insert with named bindings, a time-based uuid and tuneable consistency
  client.execute(
      cql.Query("INSERT INTO test.type_test (id, uuid_value) VALUES (:id, :uuid)", bindings : {
          "id" : 1
          , "uuid" : cql.Uuid.timeBased()
      }, consistency : cql.Consistency.LOCAL_QUORUM
       , prepared : true)
  ).then((cql.ResultMessage res) {
    // ...
  });

  // Perform a batch insert query
  client.execute(
      cql.BatchQuery()
        ..add(
          cql.Query("INSERT INTO test.type_test (id, uuid_value) VALUES (:id, :uuid)", bindings : {
              "id" : 1
              , "uuid" : cql.Uuid.timeBased()
          })
      )
        ..add(
          cql.Query("INSERT INTO test.type_test (id, uuid_value) VALUES (:id, :uuid)", bindings : {
              "id" : 2
              , "uuid" : cql.Uuid.timeBased()
          })
      )
        ..consistency = cql.Consistency.TWO
  ).then((cql.ResultMessage res) {
    // ...
  }).catchError((e) {
    // Handle errors
  });

  // Stream (paginated) query
  StreamSubscription sub;
  sub = client.stream(
      cql.Query("SELECT * from test.type_test")
      , pageSize : 200
  ).listen((Map<String, Object> row) {
    // Handle incoming row
    print("Next row: ${row}");
    // ... or manipulate stream
    sub.cancel();
  });

}

Api

See the Api documentation.

Contributing

See the Contributing Guide.

Acknowledgements

License

dart_cassandra_cql is distributed under the MIT license.