- ElasticSearch Cookbook
- Alberto Paro
- 358字
- 2021-04-02 10:09:56
Using the HTTP protocol
This recipe shows a sample of using the HTTP protocol.
Getting ready
You need a working ElasticSearch cluster. Using default configuration the 9200 port is open in your server to communicate with.
How to do it…
The standard RESTful protocol, it's easy to integrate.
Now, I'll show how to easily fetch the ElasticSearch greeting API on a running server at 9200 port using several ways and programming languages.
For every language sample, the answer will be the same:
{ "ok" : true, "status" : 200, "name" : "Payge, Reeva", "version" : { "number" : "0.90.5", "snapshot_build" : false }, "tagline" : "You Know, for Search" }
In BASH:
curl –XGET http://127.0.0.1:9200
In Python:
import urllib result = urllib.open("http://127.0.0.1:9200")
In Java:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; … try { // get URL content URL url = new URL("http://127.0.0.1:9200"); URLConnection conn = url.openConnection();// open the stream and put it into BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = br.readLine()) != null){ System.out.println(inputLine); } br.close(); System.out.println("Done"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
In Scala:
scala.io.Source.fromURL("http://127.0.0.1:9200","utf-8").getLines.mkString("\n")
How it works…
Every client creates a connection to the server and fetches the answer. The answer is a valid JSON object. You can call ElasticSearch server from any language that you like.
The main advantages of this protocol are as follows:
- Portability: It uses web standards so it can be integrated in different languages (Erlang, JavaScript, Python, Ruby, and so on) or called from command-line applications such as curl.
- Durability: The REST APIs don't often change. They don't break for minor release changes as Native protocol does.
- Simple to use: It speaks JSON to JSON.
- More supported than other protocols: Every plugin typically supports a REST endpoint on HTTP.
In this book a lot of examples are used calling the HTTP API via command-line cURL program. This approach is very fast and allows you to test functionalities very quickly.
There's more…
Every language provides drivers to best integrate ElasticSearch or RESTful web services.
ElasticSearch community provides official drivers that support the various services.
- Linux網(wǎng)絡管理與配置(第2版)
- Red Hat Enterprise Linux 8系統(tǒng)管理實戰(zhàn)
- Mastering KVM Virtualization
- 嵌入式操作系統(tǒng)(Linux篇)(微課版)
- 高性能Linux服務器構(gòu)建實戰(zhàn):系統(tǒng)安全、故障排查、自動化運維與集群架構(gòu)
- Windows Phone 8 Application Development Essentials
- Windows Server 2019 Administration Fundamentals
- Windows 7中文版從入門到精通(修訂版)
- Linux內(nèi)核觀測技術(shù)BPF
- Windows 7應用入門與技巧
- Linux內(nèi)核設計的藝術(shù):圖解Linux操作系統(tǒng)架構(gòu)設計與實現(xiàn)原理
- Linux系統(tǒng)最佳實踐工具:命令行技術(shù)
- Linux 從入門到項目實踐(超值版)
- Mastering Eclipse Plug-in Development
- Xamarin Mobile Application Development for Android