es-sql_jdbc链接查询使用
package org.nlpcn.es4sql;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ElasticSearchDruidDataSourceFactory;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
public class JDBCTests {
@Test
public void testJDBC() throws Exception {
Properties properties = new Properties();
properties.put("url", "jdbc:elasticsearch://192.168.1.101:9300/" + TestsConstants.TEST_INDEX);
DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties);
Connection connection = dds.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT * from " + TestsConstants.TEST_INDEX + " where id='46091023'");
Calendar Htime= Calendar.getInstance();
double nowtime = Htime.getTime().getTime();
ResultSet resultSet = ps.executeQuery();
Calendar Ltime= Calendar.getInstance();
double endtime = Ltime.getTime().getTime();
BigDecimal longtime = new BigDecimal((endtime-nowtime)/1000);
double second =longtime.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
List<String> result = new ArrayList<String>();
while (resultSet.next()) {
result.add(resultSet.getInt("id") + "," + resultSet.getString("age") + "," + resultSet.getString("name"));
}
System.out.println("查询结果:"+result);
System.out.println("耗时:"+second+"秒");
ps.close();
connection.close();
dds.close();
}
}
package org.nlpcn.es4sql;
public class index {
public static void main(String[] args) {
// TODO Auto-generated method stub
gotop();
}
public static void gotop() {
JDBCTests server = new JDBCTests();
try {
server.testJDBC();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341