mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00
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
|
||
|
}
|