溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

es-sql_jdbc鏈接查詢使用

發(fā)布時(shí)間:2020-10-09 12:58:05 來(lái)源:網(wǎng)絡(luò) 閱讀:1169 作者:王上京小伙 欄目:數(shù)據(jù)庫(kù)

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

        while (resultSet.next()) {

            result.add(resultSet.getInt("id") + "," + resultSet.getString("age") + "," + resultSet.getString("name"));

        }

        System.out.println("查詢結(jié)果:"+result);

        System.out.println("耗時(shí):"+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();

}

}


}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI