-
Notifications
You must be signed in to change notification settings - Fork 111
/
toElasticsearch.go
79 lines (71 loc) · 2.11 KB
/
toElasticsearch.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
package library
import (
"github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core"
"github.com/nytlabs/streamtools/st/blocks" // blocks
"github.com/nytlabs/streamtools/st/util"
)
// specify those channels we're going to use to communicate with streamtools
type ToElasticsearch struct {
blocks.Block
queryrule chan chan interface{}
inrule chan interface{}
in chan interface{}
out chan interface{}
quit chan interface{}
host string
port string
index string
indextype string
}
// we need to build a simple factory so that streamtools can make new blocks of this kind
func NewToElasticsearch() blocks.BlockInterface {
return &ToElasticsearch{}
}
// Setup is called once before running the block. We build up the channels and specify what kind of block this is.
func (b *ToElasticsearch) Setup() {
b.Kind = "ToElasticsearch"
b.Desc = "sends messages as JSON to a specified index and type in Elasticsearch"
b.in = b.InRoute("in")
b.inrule = b.InRoute("rule")
b.queryrule = b.QueryRoute("rule")
b.quit = b.Quit()
b.out = b.Broadcast()
}
// Run is the block's main loop. Here we listen on the different channels we set up.
// This block posts a message to a specified Elasticsearch index with the given type.
func (b *ToElasticsearch) Run() {
for {
select {
case msgI := <-b.inrule:
host, _ := util.ParseString(msgI, "Host")
port, _ := util.ParseString(msgI, "Port")
index, _ := util.ParseString(msgI, "Index")
indextype, _ := util.ParseString(msgI, "IndexType")
// Set the Elasticsearch Host/Port to Connect to
api.Domain = host
api.Port = port
b.host = host
b.port = port
b.index = index
b.indextype = indextype
case respChan := <-b.queryrule:
// deal with a query request
respChan <- map[string]interface{}{
"Host": b.host,
"Port": b.port,
"Index": b.index,
"IndexType": b.indextype,
}
case <-b.quit:
// quit the block
return
case msg := <-b.in:
var args map[string]interface{}
_, err := core.Index(b.index, b.indextype, "", args, msg)
if err != nil {
b.Error(err)
}
}
}
}