Skip to content

Instantly share code, notes, and snippets.

@stuart-warren
Created December 7, 2014 22:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuart-warren/bcd8fd4dd78c1e960412 to your computer and use it in GitHub Desktop.
Save stuart-warren/bcd8fd4dd78c1e960412 to your computer and use it in GitHub Desktop.
Example golang code to access Elasticsearch using Thrift
package main
/*
* Example golang code to access Elasticsearch using Thrift
* gen-go directory generated with:
* ``thrift -gen go elasticsearch.thrift``
*/
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/stuart-warren/es-thrift/gen-go/elasticsearch" // generated code
"net"
)
const BUFFER_SIZE = 1024
var request = elasticsearch.RestRequest{
Method: elasticsearch.Method_GET,
Uri: "/_cluster/health",
Parameters: map[string]string{"pretty":"true"},
}
func Connect(host string, thriftPort string) (*elasticsearch.RestClient, error) {
binaryProtocol := thrift.NewTBinaryProtocolFactoryDefault()
socket, err := thrift.NewTSocket(net.JoinHostPort(host, thriftPort))
if err != nil {
return nil, err
}
bufferedTransport := thrift.NewTBufferedTransport(socket, BUFFER_SIZE)
client := elasticsearch.NewRestClientFactory(bufferedTransport, binaryProtocol)
if err := bufferedTransport.Open(); err != nil {
return nil, err
}
return client, nil
}
func main() {
client, err := Connect("localhost", "9500")
if err != nil {
// err
}
result, err2 := client.Execute(&request)
if err2 != nil {
// err2
}
fmt.Println(string(result.GetBody()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment