|
| 1 | +package org.datayoo.moql.querier.es; |
| 2 | + |
| 3 | +import junit.framework.TestCase; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.http.HttpHost; |
| 6 | +import org.apache.http.auth.AuthScope; |
| 7 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 8 | +import org.apache.http.client.CredentialsProvider; |
| 9 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 10 | +import org.datayoo.moql.RecordSet; |
| 11 | +import org.elasticsearch.client.RestClient; |
| 12 | +import org.elasticsearch.client.RestClientBuilder; |
| 13 | +import org.elasticsearch.client.RestHighLevelClient; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +public class EsQueryTest extends TestCase { |
| 19 | + protected RestHighLevelClient restHighLevelClient; |
| 20 | + |
| 21 | + public void test01() throws IOException { |
| 22 | + open(); |
| 23 | + String sql = "select titleWords.index,titleWords.term from zz_news_entity-202205"; |
| 24 | + EsDataQuerier esDataQuerier = new EsDataQuerier(); |
| 25 | + esDataQuerier.bind(restHighLevelClient.getLowLevelClient()); |
| 26 | + |
| 27 | + String[] array = sql.split("(?i)from"); |
| 28 | + String tableNames = array[array.length - 1].trim().split(" ")[0]; |
| 29 | + |
| 30 | + sql = sql.replace(tableNames, "table"); |
| 31 | + Properties properties = new Properties(); |
| 32 | + Properties indexNameMappings = new Properties(); |
| 33 | + indexNameMappings.put("table", tableNames); |
| 34 | + properties.put(EsDataQuerier.INDEX_NAME_MAPPINGS, indexNameMappings); |
| 35 | + RecordSet recordSet = esDataQuerier.query(sql, properties); |
| 36 | + |
| 37 | + System.out.println(recordSet.getRecords().size()); |
| 38 | + close(); |
| 39 | + } |
| 40 | + |
| 41 | + public void open() throws IOException { |
| 42 | + if (restHighLevelClient != null) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + String url = "172.30.30.4:19200"; |
| 47 | + |
| 48 | + List<Map<String, Object>> address = new LinkedList<>(); |
| 49 | + if (StringUtils.isNotEmpty(url)) { |
| 50 | + String[] addressArray = StringUtils.split(url, ","); |
| 51 | + for (String addr : addressArray) { |
| 52 | + String host = addr.split(":")[0]; |
| 53 | + String port = addr.split(":")[1]; |
| 54 | + Map<String, Object> connectionInfo = new HashMap<>(); |
| 55 | + connectionInfo.put("host", host); |
| 56 | + connectionInfo.put("port", Integer.valueOf(port)); |
| 57 | + address.add(connectionInfo); |
| 58 | + } |
| 59 | + } else { |
| 60 | + Map<String, Object> connectionInfo = new HashMap<>(); |
| 61 | + connectionInfo.put("host", "172.30.30.4"); |
| 62 | + connectionInfo.put("port", 9200); |
| 63 | + address.add(connectionInfo); |
| 64 | + } |
| 65 | + restHighLevelClient = restHighLevelClient(address, "", ""); |
| 66 | + } |
| 67 | + |
| 68 | + public static RestHighLevelClient restHighLevelClient( |
| 69 | + List<Map<String, Object>> address, String username, String password) { |
| 70 | + // 拆分地址 |
| 71 | + List<HttpHost> hostLists = new ArrayList<>(); |
| 72 | + for (Map<String, Object> hostInfo : address) { |
| 73 | + String host = (String) hostInfo.get("host"); |
| 74 | + int port = (int) hostInfo.get("port"); |
| 75 | + hostLists.add(new HttpHost(host, port, "http")); |
| 76 | + } |
| 77 | + // 转换成 HttpHost 数组 |
| 78 | + HttpHost[] httpHost = hostLists.toArray(new HttpHost[] {}); |
| 79 | + // 构建连接对象 |
| 80 | + RestClientBuilder builder = RestClient.builder(httpHost); |
| 81 | + if (StringUtils.isNotEmpty(username)) { |
| 82 | + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
| 83 | + credentialsProvider.setCredentials(AuthScope.ANY, |
| 84 | + new UsernamePasswordCredentials(username, password)); |
| 85 | + builder.setHttpClientConfigCallback( |
| 86 | + f -> f.setDefaultCredentialsProvider(credentialsProvider)); |
| 87 | + } |
| 88 | + |
| 89 | + // socket通信超时时间 |
| 90 | + // 异步连接延时配置 |
| 91 | + builder.setRequestConfigCallback(requestConfigBuilder -> { |
| 92 | + requestConfigBuilder.setConnectTimeout(300 * 1000); |
| 93 | + requestConfigBuilder.setSocketTimeout(300 * 1000); |
| 94 | + requestConfigBuilder.setConnectionRequestTimeout(300 * 1000); |
| 95 | + return requestConfigBuilder; |
| 96 | + }); |
| 97 | + |
| 98 | + return new RestHighLevelClient(builder); |
| 99 | + } |
| 100 | + |
| 101 | + public void close() throws IOException { |
| 102 | + if (restHighLevelClient != null) { |
| 103 | + restHighLevelClient.close(); |
| 104 | + restHighLevelClient = null; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments