jdbc連線elasticsearch6.3.0demo展示

zjx_z發表於2018-07-13

連線方式和jdbc連線其他工具一樣,關鍵是驅動。程式碼如下:

 public void  con() {
        String driver = "org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver";
        try {
            Class.forName(driver).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String address = "jdbc:es://qs52:9200";
        Properties connectionProperties = new Properties();
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(address, connectionProperties);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet results = statement.executeQuery("SELECT * FROM twitter");
            while (results.next()) {
                System.out.println(results.getString(2));
            }
            //關閉資源
            results.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
驅動可以去我的資源頁下載。

相關文章