好程序員大數據學習路線分享ELK技術

好程序員大數據學習路線分享ELK技術,bin存放elasticSearch 運行命令

         config 存放配置文件

         lib 存放elasticSearch運行依賴jar

         modules 存放elasticSearch 模塊

         plugins 存放插件 

 

1.1. ElasticsearchMysql對比

Elasticsearch 集羣可以包含多個索引(Index),每個索引可以包含多個類型(Type),每個類型可以包含多個文檔(Document),每個文檔可以包含多個字段(Field)。以下是 MySQL 和 Elasticsearch 的術語類比圖,幫助理解:


就像使用 MySQL 必須指定 Database 一樣,要使用 Elasticsearch 首先需要創建 Index:

client.indices.create({index : 'blog'});

這樣就創建了一個名爲 blog的 Index。Type 不用單獨創建,在創建 Mapping 時指定就可以。Mapping 用來定義 Document 中每個字段的類型,即所使用的 analyzer、是否索引等屬性,非常關鍵等。

 

索引對象(blob): 存儲數據的表結構 ,任何搜索數據,存放在索引對象上 。

         映射(mapping): 數據如何存放到索引對象上,需要有一個映射配置, 包括:數據類型、是否存儲、是否分詞 … 等。

         文檔(document): 一條數據記錄, 存在索引對象上

         文檔類型(type): 一個索引對象 存放多種類型數據,數據用文檔類型進行標識 

【後續編程】: 

第一步:建立索引對象

第二步:建立映射

第三步:存儲數據【文檔】

第四步:指定文檔類型進行搜索數據【文檔】

 

1.1. 創建一個索引

    Elasticsearch 命令的一般格式是:REST VERBHOST:9200/index/doc-type— 其中 REST VERB 是 PUT、GET 或DELETE。(使用 curlL -X 動詞前綴來明確指定 HTTP 方法。)

要創建一個索引,可在你的 shell 中運行以下命令:

curl -XPUT "http://localhost:9200/blog01/"

查看

 

1.1. 插入一個文檔

要在 /blog01 索引下創建一個類型,可插入一個文檔。

要將包含 “Deck the Halls” 的文檔插入索引中,可運行以下命令(將該命令和本教程的其他 CURL 命令都鍵入到一行中):

curl -XPUT "http://localhost:9200/blog01/article/1" -d  "{"""id""": """1""", """title""": """Whatiselasticsearch"""}"


前面的命令使用 PUT 動詞將一個文檔添加到 /article文檔類型,併爲該文檔分配 ID 爲1。URL 路徑顯示爲index/doctype/ID(索引/文檔類型/ID)。

1.2. 查看文檔

要查看該文檔,可使用簡單的 GET 命令:

curl -XGET "http://localhost:9200/blog01/article/1"


Elasticsearch 使用你之前 PUT 進索引中的 JSON 內容作爲響應:

1.3. 更新文檔

如果你認識到title字段寫錯了,並想將它更改爲 Whatislucene 怎麼辦?可運行以下命令來更新文檔:

curl -XPUT "http://localhost:9200/blog01/article/1" -d "{"""id""": """1""", """title""": """Whatislucene"""}"


因爲此命令使用了相同的唯一 ID爲1,所以該文檔會被更新。

1.4. 搜索文檔

是時候運行一次基本查詢了,此查詢比你運行來查找 “Get the Halls” 文檔的簡單 GET 要複雜一些。文檔 URL 有一個內置的 _search 端點用於此用途。在標題中找到所有包含單詞 lucene 的數據:

curl -XGET "http://localhost:9200/blog01/article/_search?q=title:'Whatislucene'"


 參數表示一個查詢。

1.5. 檢查搜索返回對象

    上圖中給出了 Elasticsearch 從前面的查詢返回的數據。

    在結果中,Elasticsearch 提供了多個 JSON 對象。第一個對象包含請求的元數據:看看該請求花了多少毫秒 (took) 和它是否超時 (timed_out)_shards 字段需要考慮 Elasticsearch 是一個集羣化服務的事實。甚至在這個單節點本地部署中,Elasticsearch 也在邏輯上被集羣化爲分片。在往後看可以觀察到 hits 對象包含:

·        total 字段,它會告訴你獲得了多少個結果

·        max_score,用於全文搜索

·        實際結果

實際結果包含 fields 屬性,因爲你將 fields 參數添加到了查詢中。否則,結果中會包含 source,而且包含完整的匹配文檔。_index_type  _id 分別表示索引、文檔類型、ID_score 指的是全文搜索命中長度。這 個字段始終會在結果中返回。

1.6. 刪除文檔

暫時不要刪除該文檔,知道如何刪除它就行了:

curl -XDELETE "http://localhost:9200/blog01/article/1"


1.7. 刪除索引

暫時不要刪除該文檔,知道如何刪除它就行了:

curl -XDELETE "http://localhost:9200/blog01"


1.要使用 Elasticsearch 首先需要創建 Index:client.indices.create({index : 'blog'});創建了一個名爲 blog的 Index

2.Type 不用單獨創建,在創建 Mapping 時指定就可以。

3.Mapping 用來定義 Document 中每個字段的類型,即所使用的 analyzer、是否索引等屬性,非常關鍵等

URL 路徑顯示爲index/doctype/ID(索引/文檔類型/ID)

創建文檔(插入一條數據),自動創建索引和映射

 

======================================================

import org.elasticsearch.action.index.IndexResponse;

import org.elasticsearch.client.Client;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.common.xcontent.XContentFactory;

import org.junit.Before;

import org.junit.Test;

 

import java.net.InetAddress;

import java.util.HashMap;

import java.util.Map;

 

public class ESTest {

    //創建連接

    private Client client;

 

    /**

     * 通過TransportClient獲取ES連接

     */

    @Before

    public void getClient() throws Exception {

        //ES服務的JavaAPI的port爲9300

        //注意:如果請求一個ES集羣,可以考慮多添加幾個節點,

        //爲了避免在一個節點出現網絡問題導致的請求失敗問題,可以自動切換另外一個節點

        client = TransportClient.builder().build()

                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

 

 

    }

    @Test

        /**

         * 1.使用json來創建文檔(插入一條數據),自動創建索引和映射

         */

 

        public void creatDocument() {

        //jason格式的數據創建文檔(插入一條數據),自動創建索引和映射

        String source = "{" +

                "\"id\":\"1\"," +

                "\"title\":\"woshishui\"," +

                "\"content\":\"wozaina\"+

                "}";

        //創建文檔:定義索引名稱,文檔類型,主鍵唯一標識id

        //execute().actionGet()==get() 代碼馬上執行

        IndexResponse indexResponse =

                client.prepareIndex("blog""article""1").setSource(source).get();

 

        //獲取響應信息

        this.loadResponse(indexResponse);

        client.close();

    }

    public void loadResponse(IndexResponse indexResponse){

        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.isCreated());

    }

        /**

         * 2.使用mao創建文檔.自動創建索引和映射

         */

        public  void creatDocument2(){

            //map類型的數據

            Map<String,Object> source = new HashMap<String, Object>();

            source.put("id","2");

            source.put("title","我是誰");

            source.put("content","我在哪");

 

            //創建文檔

            IndexResponse indexResponse =

                    client.prepareIndex("blog","article","2")

                            .setSource(source).get();

            this.loadResponse(indexResponse);

 

            client.close();

        }

 

 

    /**

     * 3.使用ES幫助類(執行類),創建文檔

     */

    @Test

    public  void creatDocument3() throws Exception{

        XContentBuilder source = XContentFactory.jsonBuilder()

 

                .startObject()

                    .field("id",3)

                    .field("title","whoami")

                    .field("content","whereami")

                .endObject();

        System.out.println(source.toString());

 

        //創建文檔

        IndexResponse indexResponse = client.prepareIndex("blog""article""3").setSource(source).get();

 

        this.loadResponse(indexResponse);

        client.close();

 

 

 

    }

}

 

======================================================================================

搜索文檔數據

1.單個索引

 

2.多個索引


更新數據

方式一:


方式二:


方式三


刪除數據


查詢

queryStringQuery:


es默認的分詞器並沒有中文進行分詞,需要我們按照一個比默認屌很多的分詞器(Ik分詞器)

 

創建映射



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章