1. 程式人生 > >elasticsearch的單節點和分散式的安裝及其操作(使用命令和使用程式碼)

elasticsearch的單節點和分散式的安裝及其操作(使用命令和使用程式碼)

關於安裝是在另一個部落格上面寫的 不知道怎麼同步 有興趣的可以點選連結去看下

在文章開始之前、先明確三個概念 1、索引 2、型別 3、文件 對比於資料庫中,索引就是一個數據庫、型別就是資料庫中的某張表、文件也就是表中具體的記錄。抽象點來看,索引抽象成一個人、人又分為男人和女人(就是型別)、然後男人有姓名、年齡、身高等(就是文件)。

使用postman進行操作

    向es(elasticsearch,下面全部用es簡稱)傳送操作的請求是一個RestFui風格的,類似下面這種:
        http://<ip>:<port>/<索引>/<型別>/<文件id>
下面使用post向es傳送請求進行索引的建立

請求url:127.0.0.1:9200/people 請求入參:

{
    "settings": { 
        "index.number_of_shards":3,
        "index.number_of_replicas":1
    },
    "mappings": {
        "man": { 
            "properties":{
                "name":{
                    "type":"text"
                    
                },
                "height":{
                    "type":"text"
                    
                },
                "age":{
                    "type":"integer"
                    
                }
            }
        }
        
    }
}
上述的過程就是建立了一個索引為people、型別為man、型別中的properties有name、height、age這個欄位

響應引數:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

插入資料有兩種方式: 第一種 指定id來插入: 請求url:127.0.0.1:9200/people/man/1 (1就是我們指定的id) 請求入參json串:

{
    "name":"shuaige",
    "age" : 18,
    "height": "188cm"
}

響應:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

自動生成id: 請求url:127.0.0.1:9200/people/man 請求入參:

{
    "name":"laoshiren",
    "age" : 30,
    "height": "166cm"
}

響應引數:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWXxFS1S66ULpPmE4hFv",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

此時就建立了一個id為AWXxFS1S66ULpPmE4hFv的資料,其它的操作同理,都是像es發請求,只不過入參改改就可以

使用java程式碼進行操作

1、首先、構建一個springboot'工程、引入es的依賴
    
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

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

新建一個配置檔案的類,初始化一個es的客戶端

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public TransportClient client() throws Exception {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp埠是9300

        Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }
}

最後在一個controller裡面對es進行操作

首先注入初始化es客戶端:

    @Autowired
    private TransportClient client;

插入資料:

public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){

        try {
            XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                    .field("name",name)
                    .field("height",height)
                    .field("age",age)
                    .endObject();
            IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get();
            log.info("插入資料成功={}",response);
            return new ResponseEntity(response.getId(),HttpStatus.OK);

        }catch (Exception e){
            log.error("插入資料異常={}", e);
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

最後為方法配置路由去訪問就可以,其它的操作同理的。 程式碼的地址:程式碼下載連線

---恢復內容結束---

在文章開始之前、先明確三個概念 1、索引 2、型別 3、文件 對比於資料庫中,索引就是一個數據庫、型別就是資料庫中的某張表、文件也就是表中具體的記錄。抽象點來看,索引抽象成一個人、人又分為男人和女人(就是型別)、然後男人有姓名、年齡、身高等(就是文件)。

使用postman進行操作

    向es(elasticsearch,下面全部用es簡稱)傳送操作的請求是一個RestFui風格的,類似下面這種:
        http://<ip>:<port>/<索引>/<型別>/<文件id>
下面使用post向es傳送請求進行索引的建立

請求url:127.0.0.1:9200/people 請求入參:

{
    "settings": { 
        "index.number_of_shards":3,
        "index.number_of_replicas":1
    },
    "mappings": {
        "man": { 
            "properties":{
                "name":{
                    "type":"text"
                    
                },
                "height":{
                    "type":"text"
                    
                },
                "age":{
                    "type":"integer"
                    
                }
            }
        }
        
    }
}
上述的過程就是建立了一個索引為people、型別為man、型別中的properties有name、height、age這個欄位

響應引數:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

插入資料有兩種方式: 第一種 指定id來插入: 請求url:127.0.0.1:9200/people/man/1 (1就是我們指定的id) 請求入參json串:

{
    "name":"shuaige",
    "age" : 18,
    "height": "188cm"
}

響應:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

自動生成id: 請求url:127.0.0.1:9200/people/man 請求入參:

{
    "name":"laoshiren",
    "age" : 30,
    "height": "166cm"
}

響應引數:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWXxFS1S66ULpPmE4hFv",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

此時就建立了一個id為AWXxFS1S66ULpPmE4hFv的資料,其它的操作同理,都是像es發請求,只不過入參改改就可以

使用java程式碼進行操作

1、首先、構建一個springboot'工程、引入es的依賴
    
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

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

新建一個配置檔案的類,初始化一個es的客戶端

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public TransportClient client() throws Exception {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp埠是9300

        Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }
}

最後在一個controller裡面對es進行操作

首先注入初始化es客戶端:

    @Autowired
    private TransportClient client;

插入資料:

public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){

        try {
            XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                    .field("name",name)
                    .field("height",height)
                    .field("age",age)
                    .endObject();
            IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get();
            log.info("插入資料成功={}",response);
            return new ResponseEntity(response.getId(),HttpStatus.OK);

        }catch (Exception e){
            log.error("插入資料異常={}", e);
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

最後為方法配置路由去訪問就可以,其它的操作同理的。 程式碼的地址:程式碼下載連線