最近公司要做后端接口调用,数据存储在es,我的打算是用sql查es,用springboot提供接口调用,es6.3版本开始支持sql查询,好了,废话不多bb,开始咯。
首先先创建一个java项目,pom文件中添加如下依赖:
<repositories>
<repository>
<id>elastic.co</id>
<url>https://artifacts.elastic.co/maven</url>
</repository>
</repositories>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>jdbc</artifactId>
<version>6.3.0</version>
</dependency>
接下来 上代码
import java.sql.*;
import java.util.Properties;
public class EsTest {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";
Class.forName(driver).newInstance();
String address = "jdbc:es://127.0.0.1:9200";
Properties connectionProperties = new Properties();
Connection connection = null;
try {
connection = DriverManager.getConnection(address, connectionProperties);
} catch (SQLException e) {
e.printStackTrace();
}
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM flower");
while (results.next()<