连接方式和jdbc连接其他工具一样,关键是驱动。代码如下:
public void con() {
String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";
try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String address = "jdbc:es://qs52:9200";
Properties connectionProperties = new Properties();
Connection connection = null;
try {
connection = DriverManager.getConnection(address, connectionProperties);
} catch (SQLException e) {
e.printStackTrace();
}
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM twitter");
while (results.next()) {
System.out.println(results.getString(2));
}
//关闭资源
results.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
驱动可以去我的资源页下载。