简体   繁体   English

如何使用SpringBoot从~100个客户数据库中查询和检索结果?

[英]How to query and retrieve results from ~100 customer databases using SpringBoot?

I have the scenario where I have to run 10 different queries on 100 customer databases with similar structure and then push the results to an ElasticSearch cluster for analysis. 我有这样的场景,我必须在具有类似结构的100个客户数据库上运行10个不同的查询,然后将结果推送到ElasticSearch集群进行分析。 All the database connections are configured inside my applications.properties file. 所有数据库连接都在我的applications.properties文件中配置。 I decided to use Spring-Boot for the project and Java High Level Rest Client as the ElasticSearch API. 我决定将Spring-Boot用于项目,将Java High Level Rest Client用作ElasticSearch API。 However I found that in Spring-Boot I have to create an entity class for every entity and create a separate class and methods for each database connection. 但是我发现在Spring-Boot中我必须为每个实体创建一个实体类,并为每个数据库连接创建一个单独的类和方法。 I am new to Spring Boot and am also not understanding the concepts of entitymanager or rowmapper. 我是Spring Boot的新手,也不了解entitymanager或rowmapper的概念。 It is quite different from Java connection-statement-query-resultset format. 它与Java connection-statement-query-resultset格式完全不同。 Kindly help me 请帮助我

I have tried creating this Database configuration class where I tried to configure a single database reading from the properties file. 我尝试创建此数据库配置类,我尝试从属性文件配置单个数据库读取。 I have created the basic datasource() and jdbctemplate() methods 我已经创建了基本的datasource()和jdbctemplate()方法

package elasticsearch;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactory",
    basePackages = { "elasticsearch" }
)
public class DatabaseConfig {


@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "primary.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}


@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource         dataSource){
    return new JdbcTemplate(dataSource);
}


@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("dataSource") DataSource dataSource) {
    return builder
            .dataSource(dataSource)
            .packages("elasticsearch")
            .persistenceUnit("elasticsearch")
            .build();
}


@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
        @Qualifier("entityManagerFactory") EntityManagerFactory
                entityManagerFactory
) {
    return new JpaTransactionManager(entityManagerFactory);
}

} }

I do not want to create an entity class for every object because the queries and also the results can vary. 我不想为每个对象创建一个实体类,因为查询和结果可能会有所不同。 Also I do not want to create rowmappers because I have already written Json mappers for the rows retrieved to push it to elasticsearch 此外,我不想创建行映射器,因为我已经为检索到的行编写了Json映射器以将其推送到elasticsearch

I am new to Spring Boot and am also not understanding the concepts of entitymanager or rowmapper. 我是Spring Boot的新手,也不了解entitymanager或rowmapper的概念。

You don't show any of the details of your schema, but I don't think you need both JPA and JdbcTemplate . 您没有显示架构的任何细节,但我认为您不需要JPA和JdbcTemplate I'd recommend one or the other. 我推荐一个或另一个。

My preference would be JdbcTemplate . 我的偏好是JdbcTemplate

JPA/Hibernate is overkill and complexity that you don't need. JPA / Hibernate是你不需要的过度杀伤和复杂性。 JdbcTemplate will be fine if you are comfortable with writing SQL SELECTs. 如果您习惯编写SQL SELECT, JdbcTemplate会很好。

It is quite different from Java connection-statement-query-resultset format. 它与Java connection-statement-query-resultset格式完全不同。

Not really. 并不是的。 JdbcTemplate helps you with the boilerplate, but it's still JDBC underneath. JdbcTemplate可以帮助您使用样板文件,但它仍然是JDBC。

100 client databases will require 100 sets of URL and credentials, one for each. 100个客户端数据库将需要100组URL和凭证,每个都有一个。 That is a lot of configuration. 这是很多配置。 You can't get around that. 你无法解决这个问题。

The problem is intractable if the schemas are not identical for all customers. 如果所有客户的模式不相同,则问题是难以解决的。

I would separate the two problems: querying for customer data and pushing to Elastic Search. 我将这两个问题分开:查询客户数据并推送到弹性搜索。

You only need a single RowMapper per query if the schema and query are identical for all customers. 如果架构和查询对所有客户都相同,则每个查询只需要一个RowMapper

I think a single repository/data access object can be used. 我认为可以使用单个存储库/数据访问对象。 You only need write and test it once, but you need to instantiate a new instance at runtime for each database connection. 您只需要编写并测试一次,但是您需要在运行时为每个数据库连接实例化一个新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Twitter4j检索超过100个结果 - How to retrieve more than 100 results using Twitter4j 如何在单个查询中从多个数据库中检索数据? - how can i retrieve data from multiple databases in a single query? 如何使用SpringBoot从Cassandra检索空集 - How to retrieve empty set from cassandra using SpringBoot 使用 springboot 从 MySql 数据库中检索电子邮件和密码 - retrieve of email and password from MySql databse using springboot 将JCR与很大的查询结果>> 100k匹配一起使用。 可扩展到多远? - Using JCR with very large query results of >> 100k hits. How far does it scale? 如何从Hibernate Envers中的客户RevisionEntity查询? - how to query from customer RevisionEntity in Hibernate Envers? 如何在 SOAP header 中检索 From/Address 值(在 Java/SpringBoot 中) - How to retrieve From/Address value in SOAP header (in Java/SpringBoot) 在springboot上使用elasticsearch查询 - using elasticsearch query on springboot 如何使用mongodb在springboot中使用多个数据库配置多个登录页面进行身份验证 - How to configure multiple login pages using multiple databases for authentication in springboot using mongodb 如何在springboot中编写查询? - How to write query in springboot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM