项目中使用es(二):使用RestHighLevelClient操作elasticsearch

写在前面

之前写了有关elasticsearch的搭建使用springboot操作elasticsearch,这次主要简单说下使用RestHighLevelClient工具包操作es。

搭建环境和选择合适的版本

环境还是以springboot2.7.12为基础搭建的,不过这不重要,因为这次想说的是RestHighLevelClient操作elasticsearch,RestHighLevelClient版本使用的是7.6.2,还需要引入elasticsearch客户端maven配置,版本也是7.6.2.
主要maven配置:

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.32</version>
        </dependency>

具体代码实现

1.首先是通过springboot实例化RestHighLevelClient对象

package com.es.demo.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ElasticsearchConfig {

    @Value("${spring.elasticsearch.uris}")
    private String urls;
    
    @Bean
    public RestHighLevelClient geClient() {
        RestHighLevelClient client = null;
        String[] split = urls.split(":");
        HttpHost httpHost = new HttpHost(split[0], Integer.parseInt(split[1]), "http");
        client = new RestHighLevelClient(RestClient.builder(httpHost));
        return client;
    }
}

2.RestHighLevelClient的简单例子,这里我就没有抽出来公共方法,只是简单的写了一下如何使用。
首先是接口

public interface ProductHighService {

    Boolean save(ProductInfo... productInfo);

    Boolean delete(Integer id);

    ProductInfo getById(Integer id);

    List<ProductInfo> getAll();

    List<ProductInfo> query(String keyword);
}

然后是实现

@Service
public class ProductHighServiceImpl implements ProductHighService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public Boolean save(ProductInfo... productInfo) {
//        IndexRequest request = new IndexRequest();
//        for (ProductInfo info : productInfo) {
//            request.index("product-info").id(String.valueOf(info.getId()));
//            request.source(XContentType.JSON, info);
//            try {
//                IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//                // 打印结果信息
//                System.out.println("_index: " + response.getIndex());
//                System.out.println("id: " + response.getId());
//                System.out.println("_result: " + response.getResult());
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
        //循环插入/更新
//        UpdateRequest updateRequest = new UpdateRequest();
//        for (ProductInfo info : productInfo) {
//            updateRequest.index("product-info").id(String.valueOf(info.getId()));
//            updateRequest.doc(XContentType.JSON, info);
//            try {
//                UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
//                // 打印结果信息
//                System.out.println("_index: " + response.getIndex());
//                System.out.println("id: " + response.getId());
//                System.out.println("_result: " + response.getResult());
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }

        //批量操作estHighLevelClient.bulk()可以包含新增,修改,删除
        BulkRequest request = new BulkRequest();
        Arrays.stream(productInfo).forEach(info -> {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.index("product-info").id(String.valueOf(info.getId()));
            try {
                //通过ObjectMapper将对象转成字符串再保存,如果不转的话存入的格式不是正常的json格式数据,不能转成对象
                ObjectMapper objectMapper = new ObjectMapper();
                String productJson = objectMapper.writeValueAsString(info);
                indexRequest.source(productJson, XContentType.JSON);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            request.add(indexRequest);
        });
        try {
            BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
            System.out.println(response.status().getStatus());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public Boolean delete(Integer id) {
        DeleteRequest deleteRequest = new DeleteRequest().index("product-info").id(String.valueOf(id));
        try {
            DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
            // 打印结果信息
            System.out.println("_index: " + response.getIndex());
            System.out.println("_id: " + response.getId());
            System.out.println("result: " + response.getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public ProductInfo getById(Integer id) {
        GetRequest getRequest = new GetRequest().index("product-info").id(String.valueOf(id));
        try {
            GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            // 打印结果信息
            System.out.println("_index: " + response.getIndex());
            System.out.println("_type: " + response.getType());
            System.out.println("_id: " + response.getId());
            System.out.println("source: " + response.getSourceAsString());
            if (StringUtils.hasLength(response.getSourceAsString())) {
                return JSONObject.parseObject(response.getSourceAsString(), ProductInfo.class);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<ProductInfo> getAll() {
        SearchRequest request = new SearchRequest();
        request.indices("product-info");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //restHighLevelClient在执行默认查询时返回条数都是10条,查询所有的时候要设置上size
        //lasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [1000000]
        //size不能设置查过10000条,
        searchSourceBuilder.size(1000);
        request.source(searchSourceBuilder);
        List<ProductInfo> result = new ArrayList<>();
        try {
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            response.getHits().forEach(e -> {
                ProductInfo productInfo = JSONObject.parseObject(e.getSourceAsString(), ProductInfo.class);
                result.add(productInfo);
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public List<ProductInfo> query(String keyword) {
        //https://blog.csdn.net/qq_38826019/article/details/121344376
        SearchRequest request = new SearchRequest();
        request.indices("product-info");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("productName", keyword));
        searchSourceBuilder.query(QueryBuilders.matchQuery("description", keyword));
        //分页
//        searchSourceBuilder.from();
//        searchSourceBuilder.size()
        request.source(searchSourceBuilder);
        List<ProductInfo> result = new ArrayList<>();
        try {
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            response.getHits().forEach(e -> {
                ProductInfo productInfo = JSONObject.parseObject(e.getSourceAsString(), ProductInfo.class);
                result.add(productInfo);
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}

这里注意一点:restHighLevelClient在执行查询的时候默认返回10条,所以如果查询数量大于10条的话需要手动设置sizesearchSourceBuilder.size(1000),但是size的大小不能查过10000条,es有限制查询记录数不能超过10000,如果设置过大就会报错:elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [1000000]

最后

别的的话好像没有遇到什么问题,因为我就是写了一个简单的demo测试一下
demo地址:https://gitee.com/wdvc/es-demo.git,有兴趣可以看下。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用elasticsearch-sql框架,可以通过以下步骤将SQL数据插入ES: 1. 创建一个ES索引,定义字段映射 2. 使用ES-SQL框架连接ES,执行SQL查询获取数据 3. 遍历查询结果,使用ES-SQL框架提供的API将数据插入ES 下面是一个示例代码,演示如何使用ES-SQL框架将SQL数据插入ES: ```java import io.github.iamazy.elasticsearch.dsl.sql.parser.SqlParser; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.QueryAction; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import java.io.IOException; import java.sql.*; public class SqlToEs { // 定义ES索引名称 private static final String INDEX_NAME = "my_index"; // 定义ES连接客户端 private RestHighLevelClient client; // 定义SQL查询语句 private String sql = "SELECT * FROM my_table WHERE id > 100"; public SqlToEs() { // 初始化ES连接客户端 client = new RestHighLevelClient(); } public void insert() throws SQLException, IOException, SqlParseException { // 解析SQL查询语句 SqlParser sqlParser = new SqlParser(); QueryAction queryAction = sqlParser.parseSelect(sql); // 构建ES查询请求 SqlElasticRequestBuilder requestBuilder = queryAction.explain(); SearchSourceBuilder sourceBuilder = requestBuilder.getSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); String query = sourceBuilder.toString(); // 执行SQL查询获取数据 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_database", "user", "password"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); // 遍历查询结果,将数据插入ES BulkRequest request = new BulkRequest(); while (rs.next()) { // 创建一个ES文档 Map<String, Object> document = new HashMap<>(); document.put("id", rs.getInt("id")); document.put("name", rs.getString("name")); document.put("age", rs.getInt("age")); // 添加到批量请求 request.add(new IndexRequest(INDEX_NAME).source(document)); } // 执行批量请求 BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); if (bulkResponse.status() == RestStatus.OK) { System.out.println("数据插入成功!"); } } } ``` 这里使用ES-SQL框架解析SQL查询语句,并构建ES查询请求。然后执行SQL查询获取数据,并遍历查询结果,将数据插入ES。最后使用ES客户端执行批量请求,将数据插入ES

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值