ElasticSearch7-sql代码示例

介绍

es-sql是x-park插件免费功能, 可以使用命令行工具进行直接操作, 示例使用restful接口进行交互.
ES-SQL官网

es-sql命令行命令

elasticsearch-7.2.0/bin/elasticsearch-sql-cli http://192.168.10.111:9200

es-sql主要代码

    String url = "http://192.168.10.112:9200/_xpack/sql?format=json";

    @Test
    public void showTable() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("query", "show tables");
        String str = OkHttpUtil.postResponseWithParamsInJson(url, jsonObject.toJSONString());
        System.out.println(str);
    }

    @Test
    public void executeSql() {
        JSONObject jsonObject = new JSONObject();
        String endDate = "'2019-01-11'";
        jsonObject.put("query", "SELECT count(1) FROM tableName where  file1 = 'ldfsjlafs' and createTime  between " + endDate + " and  '3019-12-05' ");
        String str = OkHttpUtil.postResponseWithParamsInJson(url, jsonObject.toJSONString());
        System.out.println(str);
    }

okhttp依赖


            com.squareup.okhttp3
            okhttp
            3.8.1

OkHttp工具类

import okhttp3.*;

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

/**
 * 用于http请求的工具类
 * 使用okhttp3
 */
public class OkHttpUtil {
    /**
     * 给定特定的url,参数封装成一个map,get请求
     *
     * @param url
     * @param params
     * @return 响应字符串
     */
    private static OkHttpClient client = new OkHttpClient();
    public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

    public static String getResponseWithParams(String url, Map params) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        HttpUrl.Builder builder = httpUrl.newBuilder();
        if (params != null) {
            for (String key : params.keySet()) {
                builder.addQueryParameter(key, params.get(key));
            }
        }
        Request request = new Request.Builder().url(builder.build()).build();

        try {
            String res = client.newCall(request).execute().body().string();
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

        /* 异步处理 */
//        client.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//            }
//        });
    }

    /**
     * 向url发送get请求
     *
     * @param url
     * @return 返回的字符串
     */
    public static String getResponse(String url) {
        Request request = new Request.Builder().url(url).build();
        try {
            String res = client.newCall(request).execute().body().string();
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * post请求 参数是map格式
     *
     * @param url
     * @param params
     * @return
     */
    public static String postResponseWithParamsInMap(String url, Map params) {
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null) {
            for (String key : params.keySet()) {
                builder.add(key, params.get(key));
            }
        }
        RequestBody body = builder.build();
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            String res = client.newCall(request).execute().body().string();
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 参数是json 格式
     *
     * @param url
     * @param json
     * @return
     */
    public static String postResponseWithParamsInJson(String url, String json) {
        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            String res = client.newCall(request).execute().body().string();
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

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