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.
add the following dependency to install this plugin:\
dependency:
dart_cassandra_cql:
git: https://github.com/Sparks1998/dart_cassandra_cql
- 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)
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();
});
}
See the Api documentation.
See the Contributing Guide.
- The design and implementation of this driver borrows lots of ideas from node-cassandra-cql.
- The varint and decimal type decoders have been ported from the DataStax cpp driver.
dart_cassandra_cql is distributed under the MIT license.