Skip to content

Instantly share code, notes, and snippets.

@ionutvilie
Last active January 12, 2018 11:06
Show Gist options
  • Save ionutvilie/e98e0391e1c6f83b0720ac743c0ae6b5 to your computer and use it in GitHub Desktop.
Save ionutvilie/e98e0391e1c6f83b0720ac743c0ae6b5 to your computer and use it in GitHub Desktop.
cassandra-golang-play
--datasource1
CREATE KEYSPACE datasource1 with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
CREATE TABLE datasource1.tweet (
id uuid,
event_time timestamp,
description text,
summary text,
PRIMARY KEY (id, event_time)
) WITH CLUSTERING ORDER BY (EventTime DESC);
create index on datasource1.tweet(EventTime);
INSERT INTO datasource1.tweet (id, eventTime, description, summary )
VALUES (now(), dateof(now()) ,'Foo', 'Summary-test') USING TTL 600;
/*
http://localhost:8080/test
POST:
{
"Description": "(http_requests_total > 30) in the last minute",
"Summary": "Stage: pp Vertical: testvertical",
"annotations": {
"description": "(http_requests_total > 30) in the last minute",
"summary": "Stage: pp \n Vertical: testvertical"
}
}
*/
package main
import (
"encoding/json"
"fmt"
"github.com/gocql/gocql"
"io/ioutil"
"log"
"net/http"
)
type Annotations struct {
Description string `json:"Description"`
Summary string `json:"Summary"`
}
type Response struct {
Id gocql.UUID
Description string
Summary string
}
type test_struct struct {
Description string `json:"Description"`
Summary string `json:"Summary"`
Annotations Annotations
}
func post_in_cassandra(rw http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
//panic(err)
http.Error(rw, err.Error(), http.StatusBadRequest)
}
//log.Println(string(body))
var t test_struct
err = json.Unmarshal(body, &t)
if err != nil {
//panic(err)
http.Error(rw, err.Error(), http.StatusBadRequest)
}
cluster := gocql.NewCluster("localhost")
cluster.Keyspace = "datasource1"
cluster.Consistency = gocql.One
session, _ := cluster.CreateSession()
defer session.Close()
// insert a tweet
//INSERT INTO tweet (id, event_time, Description,Summary )
//values (now(), dateof(now()) ,'Description', 'Summary') USING TTL 600;
if err := session.Query(`INSERT INTO tweet (id, event_time, Description,Summary) VALUES (?, dateof(now()), ?, ?) USING TTL 20`,
//gocql.TimeUUID() ,t.Description, t.Summary).Exec(); err != nil {
"87b13cea-fa70-11e6-af52-4c327591d293", t.Description, t.Summary).Exec(); err != nil {
log.Fatal(err)
}
fmt.Printf("\n%s \n%s \n%s", t.Description, t.Summary, t.Annotations.Summary)
}
func get_from_cassandra(rw http.ResponseWriter, req *http.Request) {
cluster := gocql.NewCluster("localhost")
cluster.Keyspace = "datasource1"
cluster.Consistency = gocql.One
session, _ := cluster.CreateSession()
defer session.Close()
var id gocql.UUID
var description string
var summary string
responses := []Response{}
iter := session.Query(`SELECT id, Description, Summary FROM tweet where id = ?`,
"87b13cea-fa70-11e6-af52-4c327591d293").Iter()
for iter.Scan(&id, &description, &summary) {
//fmt.Println(id, description, summary)
m := Response{id, description, summary}
responses = append(responses, m)
}
b, err := json.Marshal(responses)
if err != nil {
log.Fatal(err)
}
fmt.Fprint(rw, string(b))
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
func rootHandler(writer http.ResponseWriter, request *http.Request) {
// The "/" pattern matches everything, so we need to check that we're at the root here.
if request.URL.Path != "/" {
http.NotFound(writer, request)
return
}
fmt.Fprintf(writer, "use /post for posting a json or /get to view the message")
}
func main() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/post", post_in_cassandra)
http.HandleFunc("/get", get_from_cassandra)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment