NLPchina / elasticsearch-sql

Use SQL to query Elasticsearch
Apache License 2.0
7k stars 1.54k forks source link

使用jdbc连接es都需要哪些依赖呢? 版本6.3.0 #894

Open ZQbd opened 5 years ago

ZQbd commented 5 years ago

我用JDBCTest里边的代码跑不通,6.3版本好像有问题

WilliamShen2019 commented 5 years ago

没试过6.3版本,我集群里是6.1.3,引入项目里的依赖,和zip里的jar,可以使用文末的代码跑起来

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<groupId>com.swc</groupId>
<artifactId>elasticsearch_sql</artifactId>
<version>6.1.3.0</version>

<developers>
    <developer>
        <id>ansj</id>
        <name>ansj</name>
        <email>ansj-sun@163.com</email>
    </developer>
    <developer>
        <id>omershelef</id>
        <name>Omer shelef</name>
        <email>shlaflaf@gmail.com</email>
    </developer>
    <developer>
        <id>eliranmoyal</id>
        <name>Eliran Moyal</name>
        <email>eliran.moyal1@gmail.com</email>
    </developer>
</developers>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <runSuite>**/MainTestSuite.class</runSuite>
    <elasticsearch.plugin.name>sql</elasticsearch.plugin.name>
    <elasticsearch.plugin.site>true</elasticsearch.plugin.site>
    <elasticsearch.plugin.jvm>true</elasticsearch.plugin.jvm>
    <elasticsearch.version>6.1.3</elasticsearch.version>
    <elasticsearch.plugin.classname>org.elasticsearch.plugin.nlpcn.SqlPlug</elasticsearch.plugin.classname>
</properties>

<dependencies>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.41</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.15</version>
    </dependency>

    <dependency>
        <groupId>org.locationtech.spatial4j</groupId>
        <artifactId>spatial4j</artifactId>
        <version>0.6</version>
    </dependency>

    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts</artifactId>
        <version>1.13</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>15.0</version>
    </dependency>

</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>es-plugin.properties</include>
                <include>plugin-descriptor.properties</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.3</version>
            <configuration>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.elasticsearch.plugin</groupId>
                                <artifactId>parent-join-client</artifactId>
                                <version>${elasticsearch.version}</version>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                            </artifactItem>
                            <artifactItem>
                                <groupId>org.elasticsearch.plugin</groupId>
                                <artifactId>reindex-client</artifactId>
                                <version>${elasticsearch.version}</version>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.alibaba</groupId>
                                <artifactId>druid</artifactId>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>druid.jar</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.google.guava</groupId>
                                <artifactId>guava</artifactId>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>guava.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>plugin-descriptor.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <appendAssemblyId>all</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptor>src/assembly/zip.xml</descriptor>
                <finalName>elasticsearch</finalName>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>${runSuite}</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

<distributionManagement>
    <snapshotRepository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus snapshot repository</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>

    <repository>
        <id>sonatype-nexus-staging</id>
        <name>Sonatype Nexus release repository</name>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
    </repository>
</distributionManagement>

然后再引入zip包里的几个项目jar包,就可以测试了: package ela;

import java.sql.*; import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.ElasticSearchDruidDataSourceFactory;

public class ElaJDBC { public static void main(String[] args) throws Exception { Properties properties = new Properties(); properties.put("url", "jdbc:elasticsearch://192...***:9200/index_name"); DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory .createDataSource(properties); dds.setInitialSize(1); Connection connection = dds.getConnection(); String sql2 = "SELECT count(0) as count FROM index_name limit 10"; PreparedStatement ps = connection.prepareStatement(sql2); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { //sql对应输出 System.out.println(resultSet.getString("count") );

    }
    ps.close();
    connection.close();
    dds.close();
}

}

shi-yuan commented 5 years ago

elasticsearch-sql,由于中央仓库没有更新,需要自己install

<dependency>
    <groupId>org.nlpcn</groupId>
    <artifactId>elasticsearch-sql</artifactId>
    <version>${essql.version}</version>
</dependency>

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>
ZQbd commented 5 years ago

@shi-yuan 非常感谢您,我还有一个问题,我看到JDBCSupport 用的是transport client,9300端口。可以提供一个基于rest client的吗

WilliamShen2019 commented 5 years ago

elasticsearch-sql,由于中央仓库没有更新,需要自己install

<dependency>
  <groupId>org.nlpcn</groupId>
  <artifactId>elasticsearch-sql</artifactId>
  <version>${essql.version}</version>
</dependency>

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>x-pack-transport</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>
WilliamShen2019 commented 5 years ago

@shi-yuan

org.elasticsearch.client x-pack-transport ${elasticsearch.version}

这个依赖中央仓库没有6.1以上的,怎么搞

ZQbd commented 5 years ago

@WilliamShen2019 我已经跑通了

`

    <repository>
        <id>elasticsearch-releases</id>
        <url>https://artifacts.elastic.co/maven</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>`
WilliamShen2019 commented 5 years ago

@shi-yuan 6.1.3版本是不是还要依赖druid.jar

shi-yuan commented 5 years ago

恩恩,需要依赖,druid