增加該功能,純粹是在關(guān)issue的時(shí)候看到了第一個(gè)issue,參看 Is there any plan for JDBC drivers?。
大家討論的時(shí)候,提供了兩個(gè)選擇,一個(gè)是apache calcite, 另外一個(gè)是利用 alibaba 的jdbc 連接池 druid。最后我選擇了使用druid 來(lái)完成。為啥不用第一個(gè)的原因如下:
I have tried calcite to add this feature to es-sql. It's proved to be unsuitable. calcite kind like spark datasource API which provide scanTable,filterTabe,translateTable which means you cannot access original sql(Raw SQL) but some expressions . Also, this means join ,group by action are all done in calcite which really impact performance .
PS: 該功能還只是Demo階段,很多功能還不完備。等穩(wěn)定后會(huì)合并到原項(xiàng)目中。目前經(jīng)過(guò)幾輪PR,
- https://github.com/NLPchina/elasticsearch-sql/pull/269
- https://github.com/NLPchina/elasticsearch-sql/pull/273
- https://github.com/NLPchina/elasticsearch-sql/pull/271
elasticsearch-sql 已經(jīng)支持比較復(fù)雜的SQL語(yǔ)法了。
安裝步驟
- 下載項(xiàng)目 https://github.com/allwefantasy/elasticsearch-sql/tree/jdbc-support
- build jar 包
- 添加jar包到你的項(xiàng)目即可。
代碼示例
public class JDBCTests {
@Test
public void testJDBC() throws Exception {
Properties properties = new Properties();
properties.put("url", "jdbc:elasticsearch://127.0.0.1:9300/twitter2");
DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties);
dds.setInitialSize(1);
Connection connection = dds.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT split(trim(concat_ws('dd',newtype,num_d)),'dd')[0] as nt from twitter2");
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getString("nt"));
}
ps.close();
connection.close();
dds.close();
}
}