/
serializer_cassandra.go
95 lines (82 loc) · 2.45 KB
/
serializer_cassandra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package common
import (
"fmt"
"io"
)
type SerializerCassandra struct {
}
func NewSerializerCassandra() *SerializerCassandra {
return &SerializerCassandra{}
}
// SerializeCassandra writes Point data to the given writer, conforming to the
// Cassandra query format.
//
// This function writes output that looks like:
// INSERT INTO <tablename> (series_id, ts_ns, value) VALUES (<series_id>, <timestamp_nanoseconds>, <field value>)
// where series_id looks like: <measurement>,<tagset>#<field name>#<time shard>
//
// For example:
// INSERT INTO all_series (series_id, timestamp_ns, value) VALUES ('cpu,hostname=host_01#user#2016-01-01', 12345, 42.1)\n
func (m *SerializerCassandra) SerializePoint(w io.Writer, p *Point) (err error) {
timestampNanos := p.Timestamp.UTC().UnixNano()
buf := make([]byte, 0, 4096)
buf = append(buf, []byte("INSERT INTO measurements.")...)
buf = append(buf, []byte(p.MeasurementName)...)
buf = append(buf, []byte(" (time")...)
for i := 0; i < len(p.TagKeys); i++ {
buf = append(buf, ","...)
buf = append(buf, p.TagKeys[i]...)
}
for i := 0; i < len(p.FieldKeys); i++ {
buf = append(buf, ","...)
buf = append(buf, p.FieldKeys[i]...)
}
buf = append(buf, []byte(") VALUES (")...)
buf = append(buf, []byte(fmt.Sprintf("%d", timestampNanos))...)
for i := 0; i < len(p.TagValues); i++ {
buf = append(buf, ",'"...)
buf = append(buf, p.TagValues[i]...)
buf = append(buf, byte('\''))
}
for i := 0; i < len(p.FieldValues); i++ {
buf = append(buf, ","...)
v := p.FieldValues[i]
buf = fastFormatAppendCassandra(v, buf, true)
}
buf = append(buf, []byte(");\n")...)
_, err = w.Write(buf)
if err != nil {
return err
}
return nil
}
func fastFormatAppendCassandra(v interface{}, buf []byte, singleQuotesForString bool) []byte {
switch v.(type) {
case []byte, string:
buf = append(buf, []byte("textasblob(")...)
buf = fastFormatAppend(v, buf, singleQuotesForString)
buf = append(buf, []byte(")")...)
return buf
default:
return fastFormatAppend(v, buf, singleQuotesForString)
}
}
func typeNameForCassandra(v interface{}) string {
switch v.(type) {
case int, int64:
return "bigint"
case float64:
return "double"
case float32:
return "float"
case bool:
return "boolean"
case []byte, string:
return "blob"
default:
panic(fmt.Sprintf("unknown field type for %#v", v))
}
}
func (s *SerializerCassandra) SerializeSize(w io.Writer, points int64, values int64) error {
return serializeSizeInText(w, points, values)
}