chat-分别用java/python写一段代码实现es数据和接口返回数据对比

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ESDataComparison {
    public static void main(String[] args) throws IOException {
        // 连接到Elasticsearch
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")));

        // 从ES中获取数据
        SearchRequest esRequest = new SearchRequest("your_index_name");
        SearchSourceBuilder esSourceBuilder = new SearchSourceBuilder();
        esSourceBuilder.query(QueryBuilders.matchAllQuery());
        esSourceBuilder.size(100);
        esRequest.source(esSourceBuilder);
        SearchResponse esResponse = esClient.search(esRequest, RequestOptions.DEFAULT);
        SearchHit[] esHits = esResponse.getHits().getHits();

        // 从接口获取数据
        OkHttpClient httpClient = new OkHttpClient();
        Request apiRequest = new Request.Builder()
                .url("http://your_api_url")
                .build();
        Response apiResponse = httpClient.newCall(apiRequest).execute();
        String apiResponseBody = apiResponse.body().string();
        List> apiResults = Arrays.asList(new Gson().fromJson(apiResponseBody, Map[].class));

        // 对比数据
        for (int i = 0; i < Math.min(esHits.length, apiResults.size()); i++) {
            Map esData = esHits[i].getSourceAsMap();
            Map apiData = apiResults.get(i);

            // 比较数据字段
            for (String field : esData.keySet()) {
                if (apiData.containsKey(field)) {
                    if (!esData.get(field).equals(apiData.get(field))) {
                        System.out.println("Field '" + field + "' is different: ES - " + esData.get(field) +
                                ", API - " + apiData.get(field));
                    }
                } else {
                    System.out.println("Field '" + field + "' is missing in API data");
                }
            }

            // 检查API数据中是否有额外字段
            for (String field : apiData.keySet()) {
                if (!esData.containsKey(field)) {
                    System.out.println("Field '" + field + "' is missing in ES data");
                }
            }
        }

        // 关闭连接
        esClient.close();
    }
}

2、python实现

import requests
from elasticsearch import Elasticsearch

# 连接到Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

# 从ES中获取数据
es_query = {
    "query": {
        "match_all": {}
    }
}
es_result = es.search(index='your_index_name', body=es_query)

# 从接口获取数据
api_url = 'http://your_api_url'
api_result = requests.get(api_url).json()

# 对比数据
for es_hit, api_data in zip(es_result['hits']['hits'], api_result):
    es_data = es_hit['_source']
    
    # 比较数据字段
    for field in es_data:
        if field in api_data:
            if es_data[field] != api_data[field]:
                print(f"Field '{field}' is different: ES - {es_data[field]}, API - {api_data[field]}")
        else:
            print(f"Field '{field}' is missing in API data")
    
    # 检查API数据中是否有额外字段
    for field in api_data:
        if field not in es_data:
            print(f"Field '{field}' is missing in ES data")

你可能感兴趣的:(python)