mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Add Cassandra writer implementation Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add cassandra service with version and metrics endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test for cassandra writer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Exclude api.go files from code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add readme file for cassandra writer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add docker compose configuration for cassandra writer Add README file. Add docker compose configuration. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add gocql as project dependency Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix init script for cassandra in docker-compose Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add testifies require subpackage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
40 lines
836 B
Go
40 lines
836 B
Go
package cassandra
|
|
|
|
import "github.com/gocql/gocql"
|
|
|
|
const table = `CREATE TABLE IF NOT EXISTS messages (
|
|
id uuid PRIMARY KEY,
|
|
channel bigint,
|
|
publisher bigint,
|
|
protocol text,
|
|
name text,
|
|
unit text,
|
|
value double,
|
|
string_value text,
|
|
bool_value boolean,
|
|
data_value text,
|
|
value_sum double,
|
|
time double,
|
|
update_time double,
|
|
link text,
|
|
)`
|
|
|
|
// Connect establishes connection to the Cassandra cluster.
|
|
func Connect(hosts []string, keyspace string) (*gocql.Session, error) {
|
|
cluster := gocql.NewCluster(hosts...)
|
|
cluster.Keyspace = keyspace
|
|
cluster.Consistency = gocql.Quorum
|
|
|
|
session, err := cluster.CreateSession()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = session.Query(table).Exec()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return session, nil
|
|
}
|