Java查询es用sql_es-sql_jdbc链接查询使用

这篇博客展示了如何使用Java通过sql_es-sql_jdbc连接Elasticsearch进行查询操作。示例代码中,创建了DruidDataSource,配置Elasticsearch连接URL,并执行了一个查询语句,获取了id为'46091023'的记录,同时计算了查询的耗时。
摘要由CSDN通过智能技术生成

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;

/**

* Created by allwefantasy on 8/26/16.

*/

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 result = new ArrayList();

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值