1. 程式人生 > >ESAPI(一)索引的操作以及資料插入

ESAPI(一)索引的操作以及資料插入

JDK1.8環境下,maven倉庫依賴

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

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>5.2.2</version>
</dependency>

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.9.0</version>
</dependency>

Elasticsearch兩種操作模式可以使用

該應用程式可在Elasticsearch叢集中扮演更加主動或更加被動的角色。在更加主動的情況下(稱為Node Client),應用程式例項將從叢集接收請求,確定哪個節點應處理該請求,就像正常節點所做的一樣。(應用程式甚至可以託管索引和處理請求。)另一種模式稱為Transport Client,它將所有請求都轉發到另一個Elasticsearch節點,由後者來確定最終目標。

獲取Transport Client

(1)ElasticSearch服務預設埠9300。

(2)Web管理平臺埠9200。

(3) 外掛使用的埠 9100

@Test
public void getClient() throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //獲取客戶端物件 PreBuiltTransportClient client = new PreBuiltTransportClient(settings); //9100 外掛埠 9200web埠 9300 客戶端訪問埠 client.addTransportAddress(new InetSocketTransportAddress
(InetAddress.getByName("testnote01"),9300)); System.out.println(client.toString()); }

結果

[email protected]311

建立索引和刪除索引

package com.zyd;

import static org.junit.Assert.assertTrue;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Unit test for simple App.
 */
public class AppTest {

    TransportClient client;
    @SuppressWarnings("unchecked")
    @Before
    public void getClient() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();
        //獲取客戶端物件
        client = new PreBuiltTransportClient(settings);
        //9100 外掛埠 9200web埠 9300 客戶端訪問埠
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("testnote01"),9300));
        System.out.println(client.toString());
    }

    //建立索引
    @Test
    public void createIndex(){
        //建立索引
        client.admin().indices().prepareCreate("blog").get();
        //關閉資源
        client.close();
    }

/**
瀏覽器訪問 9100埠 顯示資訊
*/
    //刪除索引
    @Test
    public void deleteIndex(){
        //刪除索引
        client.admin().indices().prepareDelete("blog").get();
        //關閉資源
        client.close();
    }
    /**
瀏覽器訪問 9100埠 資訊被刪除
*/
}

新建文件(源資料JSON串)

當直接在ElasticSearch建立文件物件時,如果索引不存在,預設會自動建立,對映採用預設方式

//建立文件以json形式
@Test
public void createIndexByJson(){
    //1. 文件準備
    String json = "{" + "\"id\":\"1\"," + "\"title\":\"基於Lucene的搜尋伺服器\","
            + "\"content\":\"它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面\"" + "}";

    //建立
    IndexResponse indexResponse = client.prepareIndex("blog", "article", "1").setSource(json).execute().actionGet();

    //列印返回值
    System.out.println("索引:"+indexResponse.getIndex());
    System.out.println("型別:"+indexResponse.getType());
    System.out.println("id:"+indexResponse.getId());
    System.out.println("版本號:"+indexResponse.getVersion());
    System.out.println("結果:"+indexResponse.getResult());

    client.close();
}

結果

[email protected]311
索引:blog
型別:article
id:1
版本號:1
結果:CREATED

通過外掛頁面的資料瀏覽可以看到

建立文件以hashmap

//建立文件以hashmap
@Test
public void createIndexByMap(){
    HashMap<String, Object> map = new HashMap<>();
    map.put("id","2");
    map.put("title","大資料");
    map.put("name","zyd");

    IndexResponse reponse = client.prepareIndex("blog", "article", "2").setSource(map).execute().actionGet();

    //列印返回值
    System.out.println("索引:"+reponse.getIndex());
    System.out.println("型別:"+reponse.getType());
    System.out.println("id:"+reponse.getId());
    System.out.println("版本號:"+reponse.getVersion());
    System.out.println("結果:"+reponse.getResult());

    client.close();
}

結果

[email protected]989
索引:blog
型別:article
id:2
版本號:1
結果:CREATED

通過外掛頁面的資料瀏覽可以看到

建立文件以builder方式

//建立文件以builder方式
@Test
public void createIndexByBuilder() throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder()
            .startObject()
            .field("id", "5").field("title", "雲端計算")
            .field("content", "未來").endObject();

    IndexResponse response = client.prepareIndex("blog", "article", "3").setSource(builder).execute().actionGet();

    //列印返回值
    System.out.println("索引:"+response.getIndex());
    System.out.println("型別:"+response.getType());
    System.out.println("id:"+response.getId());
    System.out.println("版本號:"+response.getVersion());
    System.out.println("結果:"+response.getResult());

    client.close();
}

通過外掛頁面的資料瀏覽可以看到