无论才能、知识多么卓著,如果缺乏热情,则无异纸上画饼充饥,无补于事。

JDBC with ESQL

灌水区 | 作者 hill | 发布于2018年12月14日 | | 阅读数:2066


elsh.png

 
 https://github.com/unimassystem/elasticsearch-jdbc
	BasicDataSource basicDataSource = new BasicDataSource();
// 创建连接池 一次性创建多个连接池

// 连接池 创建连接 ---需要四个参数
basicDataSource.setDriverClassName("com.elasticsearch.jdbc.ElasticsearchDriver");
basicDataSource.setUrl("jdbc:elasticsearch://127.0.0.1:5000");

// 从连接池中获取连接
Connection conn = basicDataSource.getConnection();
String sql = "select SRC_IP,SRC_PORT from \"my_test-*\"";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("SRC_IP"));
}
basicDataSource.close();


String sql = "select SRC_IP,SRC_PORT from my_test* where SRC_PORT between 10 and 100 limit 1000";
String url = "jdbc:elasticsearch://127.0.0.1:5000";
Connection connection = DriverManager.getConnection(url, "test", null);
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
ResultSetMetaData meta = rs.getMetaData();
String columns = "|";
for (int i = 0; i < meta.getColumnCount(); i++) {
columns += meta.getColumnLabel(i) + " | ";
}
System.out.println(columns);
while (rs.next()) {
String row = "|";
for (int i = 0; i < meta.getColumnCount(); i++) {
row += rs.getString(i) + " | ";
}
System.out.println(row);
}

[尊重社区原创,转载请保留或注明出处]
本文地址:http://elasticsearch.cn/article/6198


0 个评论

要回复文章请先登录注册