elasticsearch-sql sql查询 结合java代码查询

1.本例子是使用elasticearch-sql插件的jar包结合代码来使用;

2.使用的es版本为5.6.8,elasticsearch-sql 的版本在maven官网库找不到,暂且使用5.2.1.0,可以到git 上去下去,然后自己编译https://github.com/NLPchina/elasticsearch-sql/tree/elastic5.6.8;

3.在pom.xml文件中加入:

elasticsearch-sql 

    org.nlpcn
    elasticsearch-sql
    5.1.2.0


elasticsearch

    org.elasticsearch
    elasticsearch
    5.6.8

阿里的json格式化工具

    com.alibaba
    fastjson
    1.2.31

4.使用sql查询时,几乎是遵循mysql的语法查询,支持的sql范例如下:

  • SQL Select
  • SQL Delete
  • SQL Where
  • SQL Order By
  • SQL Group By
  • SQL AND & OR
  • SQL Like
  • SQL COUNT distinct
  • SQL In
  • SQL Between
  • SQL Aliases
  • SQL Not Null
  • SQL(ES) Date
  • SQL avg()
  • SQL count()
  • SQL last()
  • SQL max()
  • SQL min()
  • SQL sum()
  • SQL Nulls
  • SQL isnull()
  • SQL now()
  • SQL floor
  • SQL split
  • SQL trim
  • SQL log
  • SQL log10
  • SQL substring
  • SQL round
  • SQL sqrt
  • SQL concat_ws
  • SQL union and minus

5.下面为使用sql查询的java代码,提供了两种不同的客户端查询方式

    第一种是集群安装了xpack并设置了密码

    第二种是集群为普通模式,没有安装xpack也没有密码

package test;

import com.alibaba.fastjson.JSON;
import com.common.log.Log;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.nlpcn.es4sql.SearchDao;
import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder;

import java.net.InetAddress;
import java.util.Map;

public class SqlTest {
    /**
     * 使用xpack客户端查询
     * 体现在es集群安装了xpack,并有密码的情况下
     */
    public static Map sqlQueryXpack() {
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", "cluster_name")
                    .put("xpack.security.user", "elastic:elastic_password") //x-pack的用户名及密码
                    .build();
            TransportClient client = new PreBuiltXPackTransportClient(settings);
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
            //创建sql查询对象
            SearchDao searchDao = new SearchDao(client);
            //执行sql查询
            SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao
.explain("select * from test_index where flag = 316")
.explain();
            //使用阿里巴巴的json格式化工具,resultMap的值就是查询返回的值
            Map resultMap = (Map) JSON.parse((select.get()).toString());
            return resultMap;
        } catch (Exception ex) {
            Log.low.error("查询异常", ex);
        }

    }

    /**
     * 集群中没有安装xpack及未设置密码
     */
    public static Map sqlQuery() {
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", "cluster_name")
                    .build();
            TransportClient client = new PreBuiltTransportClient(settings);
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
            //创建sql查询对象
            SearchDao searchDao = new SearchDao(client);
            //执行sql查询
            SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao
.explain("select * from test_index where flag = 316")
.explain();
            //使用阿里巴巴的json格式化工具,resultMap的值就是查询返回的值
            Map resultMap = (Map) JSON.parse((select.get()).toString());
            return resultMap;
        } catch (Exception ex) {
            Log.low.error("查询异常", ex);
        }
    }
}

 

你可能感兴趣的:(java,mysql,ElasticSearch)