Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ElasticSearch Cookbook - Second Edition

You're reading from   ElasticSearch Cookbook - Second Edition Over 130 advanced recipes to search, analyze, deploy, manage, and monitor data effectively with ElasticSearch

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783554836
Length 472 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alberto Paro Alberto Paro
Author Profile Icon Alberto Paro
Alberto Paro
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Downloading and Setting Up 3. Managing Mapping 4. Basic Operations 5. Search, Queries, and Filters 6. Aggregations 7. Scripting 8. Rivers 9. Cluster and Node Monitoring 10. Java Integration 11. Python Integration 12. Plugin Development Index

Using the Thrift protocol

Thrift is an interface definition language, initially developed by Facebook, used to define and create services. This protocol is now maintained by Apache Software Foundation.

Its usage is similar to HTTP, but it bypasses the limit of HTTP protocol (latency, handshake and so on) and it's faster.

Getting ready

You need a working instance of ElasticSearch cluster with the thrift plugin installed (https://github.com/elasticsearch/elasticsearch-transport-thrift/); the standard port for the Thrift protocol is 9500.

How to do it...

To use the Thrift protocol in a Java environment, perform the following steps:

  1. We must be sure that Maven loads the thrift library adding to the pom.xml file; the code lines are:
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.9.1</version>
    </dependency>
  2. In Java, creating a client is quite easy using ElasticSearch generated classes:
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TProtocol;
    import org.apache.thrift.transport.TSocket;
    import org.apache.thrift.transport.TTransport;
    import org.apache.thrift.transport.TTransportException;
    import org.elasticsearch.thrift.*;
    TTransport transport = new TSocket("127.0.0.1", 9500);
    TProtocol protocol = new TBinaryProtocol(transport);
    Rest.Client client = new Rest.Client(protocol);
    transport.open();
  3. To initialize a connection, first we need to open a socket transport. This is done with the TSocket(host, port) setting, using the ElasticSearch thrift standard port 9500.
  4. Then the socket transport protocol must be encapsulated in a binary protocol, this is done with the TBinaryProtocol(transport) parameter.
  5. Now, a client can be initialized by passing the protocol. The Rest.Client utility class and other utility classes are generated by elasticsearch.thrift. It resides in the org.elasticsearch.thrift namespace.
  6. To have a fully working client, we must open the socket (transport.open()).
  7. At the end of program, we should close the client connection (transport.close()).

There's more...

Some drivers, to connect to ElasticSearch, provide an easy-to-use API to interact with Thrift without the boulder that this protocol needs.

For advanced usage, I suggest the use of the Thrift protocol to bypass some problems related to HTTP limits, such as:

  • The number of simultaneous connections required in HTTP; Thrift transport efficiently uses resources
  • The network traffic is light weight because it is binary and is very compact

A big advantage of this protocol is that on the server side it wraps the REST entry points so that it can also be used with calls provided by external REST plugins.

See also

You have been reading a chapter from
ElasticSearch Cookbook - Second Edition - Second Edition
Published in: Jan 2015
Publisher:
ISBN-13: 9781783554836
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime