The best elasticsearch highlevel java rest api-----bboss html
Elasticsearch 6.3之後的版本能夠經過jdbc操做es,該功能還在不斷的完善當中,本文介紹es jdbc使用方法。java
導入elasticsearch jdbc驅動和bboss持久層 <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>jdbc</artifactId> <version>6.3.2</version> </dependency> <dependency> <groupId>com.bbossgroups</groupId> <artifactId>bboss-persistent</artifactId> <version>5.3.6</version> </dependency> 在pom中添加elastic maven庫 <repositories> <repository> <id>elastic.co</id> <url>https://artifacts.elastic.co/maven</url> </repository> </repositories>
直接看執行各類sql功能的代碼ESJdbcTest:git
package com.frameworkset.sqlexecutor; /* * Copyright 2008 biaoping.yin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import com.frameworkset.common.poolman.SQLExecutor; import com.frameworkset.common.poolman.util.SQLUtil; import org.junit.Test; import java.sql.SQLException; import java.util.HashMap; import java.util.List; public class ESJdbcTest { public void initDBSource(){ SQLUtil.startPool("es",//ES數據源名稱 "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver",//ES jdbc驅動 "jdbc:es://http://127.0.0.1:9200/timezone=UTC&page.size=250",//es連接串 "elastic","changeme",//es x-pack帳號和口令 "SHOW tables 'dbclob%'" //數據源鏈接校驗sql ); } /** * 執行一個查詢 * @throws SQLException */ @Test public void testSelect() throws SQLException { initDBSource();//啓動數據源 //執行查詢,將結果映射爲HashMap集合 List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SELECT SCORE() as score,content as content FROM dbclobdemo"); System.out.println(data); } /** * 進行模糊搜索,Elasticsearch 的搜索能力你們都知道,強!在 SQL 裏面,能夠用 match 關鍵字來寫,以下: * @throws SQLException */ @Test public void testMatchQuery() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SELECT SCORE(), * FROM dbclobdemo WHERE match(content, '_ewebeditor_pa_src') ORDER BY documentId DESC"); System.out.println(data); /** *還能試試 SELECT 裏面的一些其餘操做,如過濾,別名,以下: */ data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SELECT SCORE() as score,title as myname FROM dbclobdemo as mytable WHERE match(content, '_ewebeditor_pa_src') and (title.keyword = 'adsf' OR title.keyword ='elastic') limit 5 "); System.out.println(data); } /** * 分組和函數計算 */ @Test public void testGroupQuery() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SELECT title.keyword,max(documentId) as max_id FROM dbclobdemo as mytable group by title.keyword limit 5"); System.out.println(data); } /** * 查看全部的索引表 * @throws SQLException */ @Test public void testShowTable() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW tables"); System.out.println(data); } /** * 如 dbclob 開頭的索引,注意通配符只支持 %和 _,分別表示多個和單個字符(什麼,不記得了,回去翻數據庫的書去!) * @throws SQLException */ @Test public void testShowTablePattern() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW tables 'dbclob_'"); System.out.println(data); data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW tables 'dbclob%'"); System.out.println(data); } /** * 查看索引的字段和元數據 * @throws SQLException */ @Test public void testDescTable() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","DESC dbclobdemo"); System.out.println(data); data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW COLUMNS IN dbclobdemo"); System.out.println(data); } /** * 不記得 ES 支持哪些函數,只須要執行下面的命令,便可獲得完整列表 * @throws SQLException */ @Test public void testShowFunctin() throws SQLException { initDBSource(); List<HashMap> data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW FUNCTIONS"); System.out.println(data); //一樣支持通配符進行過濾: data = SQLExecutor.queryListWithDBName(HashMap.class,"es","SHOW FUNCTIONS 'S__'"); System.out.println(data); } }
若是執行的時候報錯:web
能夠採用正式的license或者在elasticsearch.yml文件最後添加如下配置便可:spring
xpack.license.self_generated.type: trialsql
bboss 提供一組sql和fetchQuery API,可替代es jdbc模塊;採用bboss便可擁有bboss的客戶端自動發現和容災能力、對es、jdk、spring boot的兼容性能力,又能夠擁有es jdbc的全部功能,同時還解決了由於引入es jdbc致使項目對es版本的強依賴和兼容性問題,參考demo:shell
orm查詢
https://gitee.com/bbossgroups/eshelloword-booter/blob/master/src/test/java/org/bboss/elasticsearchtest/sql/SQLOrmTest.java
分頁查詢
https://gitee.com/bbossgroups/eshelloword-booter/blob/master/src/test/java/org/bboss/elasticsearchtest/sql/SQLPagineTest.java數據庫
elasticsearch sql官方文檔:express
https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.htmlapache
elasticsearch技術交流:166471282
elasticsearch技術交流: