ES学习路程(三)-JAVA代码操作ES

1、首先创建一个maven项目,把这依赖搞进去

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

            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.8.0</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.8.2</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.9</version>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>

2、插入数据到es当中

package rpc.es;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.dubbo.common.json.JSON;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.RequestBuilder;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

/**
 * @author nsy
 * @date 2022/12/28
 *
 * 插入数据到es当中
 */
public class esInsert {
    public static void main(String[] args) throws Exception {

        //获取ES客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost",9200,"http")
        ));
        //创建索引
        IndexRequest indexRequest = new IndexRequest();
        //向索引中插入数据
        indexRequest.index("city1").id("1001");
        //准备数据因为再ES中都是json格式的数据所以我们前期准备的依赖也准备fastjson依赖
        Preson preSon = new Preson();
        preSon.setName("王老齐");
        preSon.setSex("男");
        preSon.setTel("18736125945");

        //把对象转成json
        ObjectMapper objectMapper = new ObjectMapper();

        String s = objectMapper.writeValueAsString(preSon);

        //然后将json数据放进request中的数据源
        indexRequest.source(s, XContentType.JSON);

        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        //查看结果
        DocWriteResponse.Result result = indexResponse.getResult();


        System.out.println(indexRequest);
        //关闭客户端
        restHighLevelClient.close();

    }
}

3、删除ES索引数据

package rpc.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @author nsy
 * @date 2022/12/28
 */
public class esTestDelRequest {
    public static void main(String[] args) throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        //删除索引库
        DeleteIndexRequest request = new DeleteIndexRequest("city2");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 查看查询结果
        boolean acknowledged = response.isAcknowledged();
        System.out.println("删除索引库成功"+acknowledged);
        // 关闭客户端

        client.close();
    }

}

https://blog.csdn.net/m0_60579941/article/details/122864365

你可能感兴趣的:(elasticsearch,java,大数据)