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 native protocol

ElasticSearch provides a native protocol, used mainly for low-level communication between nodes, but very useful for fast importing of huge data blocks. This protocol is available only for Java Virtual Machine (JVM) languages and commonly is used in Java, Groovy, and Scala.

Getting ready

You need a working instance of the ElasticSearch cluster; the standard port number for native protocol is 9300.

How to do it...

The following are the steps required to use the native protocol in a Java environment (we'll discuss this in depth in Chapter 10, Java Integration):

  1. Before starting, we must be sure that Maven loads the Elasticsearch.jar file by adding the following code to the pom.xml file:
    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>1.4.1</version>
    </dependency>
  2. Depending on the Elasticsearch.jar file, creating a Java client is quite easy:
    import org.elasticsearch.common.settings.ImmutableSettings;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.transport.TransportClient;
    …
    Settings settings = ImmutableSettings.settingsBuilder()
    .put("client.transport.sniff", true).build();
      // we define a new settings
      // using sniff transport allows to autodetect other nodes
    Client client = new TransportClient(settings)
      .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
      // a client is created with the settings

How it works...

To initialize a native client, a settings object is required, which contains some configuration parameters. The most important ones are:

  • cluster.name: This is the name of the cluster
  • client.transport.sniff: This allows you to sniff out the rest of the cluster and add them into its list of machines to use

With the settings object, it's possible to initialize a new client by giving an IP address and port a number (default 9300).

There's more...

The native protocol is the internal one used in ElasticSearch. It's the fastest protocol that is available to communicate with ElasticSearch.

The native protocol is an optimized binary and works only for JVM languages; to use this protocol, you need to include the elasticsearch.jar in your JVM project. Because it depends on ElasticSearch implementation, it must be the same version of ElasticSearch cluster.

For this reason, every time you update ElasticSearch, you need to update the elasticsearch.jar file on which it depends and if there are internal API changes, you need to update your code.

To use this protocol, you need to study the internals of ElasticSearch, so it's not as easy to use as HTTP and Thrift protocol.

Native protocol is useful for massive data import. But as ElasticSearch is mainly thought as a REST HTTP server to communicate with, it lacks support for everything that is not standard in the ElasticSearch core, such as the plugin's entry points. So using this protocol, you are unable to call entry points made by external plugins.

Note

The native protocol seems the most easy to integrate in a Java/JVM project. However, due to its nature that follows the fast release cycles of ElasticSearch, it changes very often. Also, for minor release upgrades, your code is more likely to be broken. Thus, ElasticSearch developers wisely tries to fix them in the latest releases.

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 €18.99/month. Cancel anytime