使用java如何操作elasticsearch?簡單樣本。

來源:互聯網
上載者:User

標籤:htm   class   content   引入   tran   注意   系統   rtc   cep   

線上API:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/transport-client.html
教程:
http://blog.java1234.com/blog/articles/345.html
注意:
不同版本的ES API差別較大,引入jar包版本一定要和生產保持一致。工具類及使用方法可以參考備件系統項目:源碼見GitHub
工具類及使用方法可以參考備件系統項目:源碼見GitHub

 

引入jar包:

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>

寫測試類別:

 

package com.sxt.es.test;import java.net.InetAddress;import java.util.Date;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentFactory;public class Testes {        private static String host="192.168.136.131"; // 伺服器位址    private static int port=9300; // 連接埠        public static void main(String[] args) throws Exception {                TransportClient client = TransportClient.builder().build()                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Testes.host), Testes.port));        IndexResponse response =client.prepareIndex("twitter", "tweet", "1")                .setSource(XContentFactory.jsonBuilder()                        .startObject()                        .field("user", "kimchy")                        .field("postDate", new Date())                        .field("message", "trying out Elasticsearch")                    .endObject()                        )                .get();            System.out.println("索引名稱:"+response.getIndex());            System.out.println("類型:"+response.getType());            System.out.println("文檔ID:"+response.getId()); // 第一次使用是1        client.close();    }}

 

使用java如何操作elasticsearch?簡單樣本。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.