将ElasticSearchjdbc驱动程序与hibernate/spring引导集成

lp0sw83n  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(3)|浏览(468)

我正在尝试集成来自的弹性驱动程序“org.elasticsearch.xpack.sql.jdbc.esdriver”

<dependency>
  <groupId>org.elasticsearch.plugin</groupId>
  <artifactId>x-pack-sql-jdbc</artifactId>
  <version>7.10.0</version>
</dependency>

进入我的 Spring 启动应用程序使用休眠。
在我的spring配置bean中,我有以下内容:

@Bean
    @ConfigurationProperties(prefix = "db.elastic")
    @Qualifier("elasticDataSource")
    @Primary
    public DataSource elasticDataSource() {
        return DataSourceBuilder.create()
                .build();
    }

    public LocalContainerEntityManagerFactoryBean elasticEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        Map<String, Object> properties = new HashMap<>();
        properties.put(AvailableSettings.HBM2DDL_AUTO, "none");
        properties.put(AvailableSettings.HBM2DLL_CREATE_SCHEMAS, "false");
        properties.put(AvailableSettings.DIALECT, org.elasticsearch.xpack.sql.jdbc.EsDriver.class.getName());
        return builder
                .dataSource(elasticDataSource())
                .packages(Issuer.class)
                .persistenceUnit("elastic")
                .properties(properties)
                .build();
    }

但是,当我运行此代码时,出现以下异常:

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
    ... 16 common frames omitted
Caused by: org.hibernate.HibernateException: Unable to construct requested dialect [org.elasticsearch.xpack.sql.jdbc.EsDriver]
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:84)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
    ... 33 common frames omitted
Caused by: java.lang.ClassCastException: org.elasticsearch.xpack.sql.jdbc.EsDriver cannot be cast to org.hibernate.dialect.Dialect
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74)
    ... 38 common frames omitted

我想这是因为驱动程序与hibernate不兼容。我是正确的还是有一些其他的配置,必须做的工作来解决这个问题?
这里还有一个商用的jdbc驱动程序:https://www.cdata.com/drivers/elasticsearch/jdbc/
有没有人有过使用这个驱动程序的经验以及它与hibernate的兼容性?

lrpiutwd

lrpiutwd1#

所以我发现org.hibernate.dialect.h2dialect或org.hibernate.dialect.postgresql9dialect是有效的,但仅在一定程度上有效。
首先,实体名称中还必须包含双引号:

@Data
@Table(name = "\"mytable\"")
@Entity
public class MyTable {
}

此外,我发现设置maxrows不起作用。由于某些原因,此值在生成的sql中不受限制:

18:14:29.320 [http-nio-8081-exec-1] DEBUG org.hibernate.SQL - 
    select
        mytabl_.NAME as col_0_0_ 
    from
        "mytable" mytabl_
    order by
        mytabl_.NAME asc limit ?
18:14:29.651 [http-nio-8081-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: null
18:14:29.651 [http-nio-8081-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - line 1:108: mismatched input '?' expecting {'ALL', INTEGER_VALUE}
18:14:29.658 [http-nio-8081-exec-1] WARN  g.e.SimpleDataFetcherExceptionHandler - Exception while fetching data (/Issuers) : line 1:108: mismatched input '?' expecting {'ALL', INTEGER_VALUE}
org.hibernate.JDBCException: line 1:108: mismatched input '?' expecting {'ALL', INTEGER_VALUE}

有人知道为什么会这样吗?

a0x5cqrl

a0x5cqrl2#

ElasticSearchSQL方言没有方言,但您可以尝试使用 org.hibernate.dialect.SQLServerDialect 就像在https://www.cdata.com/kb/tech/elasticsearch-jdbc-hibernate.rst
最后,您可能需要重写方言中的一些配置,以适应es支持的sql方言。

xqkwcwgp

xqkwcwgp3#

我不知道elasticsearch驱动程序,但是我会告诉你为什么会出现错误。
您的错误是:由于:java.lang.classcastexception:org.elasticsearch.xpack.sql.jdbc.esdriver无法转换为org.hibernate.dialect.dialect
这是因为您的属性有问题:
properties.put(availablesettings.dialect,org.elasticsearch.xpack.sql.jdbc.esdriver.class.getname());
esdrver.class.getname()不是hibernate方言。
hibernate方言的一个例子是:org.hibernate.dialect.mysql5dialect
敬请阅读https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-jdbc.html
看到您使用的是springboot,您甚至不必手动配置datasource/entitymanager。只需添加依赖项就可以自动配置它。

相关问题