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

Fix data scheme so that cassandra stores channel and thing id as text. Support sorting by time per channel partition. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
47 lines
981 B
Go
47 lines
981 B
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package cassandra
|
|
|
|
import "github.com/gocql/gocql"
|
|
|
|
const table = `CREATE TABLE IF NOT EXISTS messages (
|
|
id uuid,
|
|
channel text,
|
|
publisher text,
|
|
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,
|
|
PRIMARY KEY (channel, time, id)
|
|
) WITH CLUSTERING ORDER BY (time DESC)`
|
|
|
|
// 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
|
|
}
|
|
|
|
if err := session.Query(table).Exec(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return session, nil
|
|
}
|