Technology Sharing

Spring Boot Vue Graduation System Explanation 7

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

 

Data Warehouse HIVE Practice

  1. @ConfigurationProperties(prefix = "hive")
  2. @Data
  3. public class HiveDruidConfig {
  4. private String url;
  5. private String user;
  6. private String password;
  7. private String driverClassName;
  8. private int initialSize;
  9. private int minIdle;
  10. private int maxActive;
  11. private int maxWait;
  12. private int timeBetweenEvictionRunsMillis;
  13. private int minEvictableIdleTimeMillis;
  14. private String validationQuery;
  15. private boolean testWhileIdle;
  16. private boolean testOnBorrow;
  17. private boolean testOnReturn;
  18. private boolean poolPreparedStatements;
  19. private int maxPoolPreparedStatementPerConnectionSize;
  20. @Bean(name = "hiveDruidDataSource")
  21. @Qualifier("hiveDruidDataSource")
  22. public DataSource dataSource() {
  23. DruidDataSource datasource = new DruidDataSource();
  24. datasource.setUrl(url);
  25. datasource.setUsername(user);
  26. datasource.setPassword(password);
  27. datasource.setDriverClassName(driverClassName);
  28. // pool configuration
  29. datasource.setInitialSize(initialSize);
  30. datasource.setMinIdle(minIdle);
  31. datasource.setMaxActive(maxActive);
  32. datasource.setMaxWait(maxWait);
  33. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  34. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  35. datasource.setValidationQuery(validationQuery);
  36. datasource.setTestWhileIdle(testWhileIdle);
  37. datasource.setTestOnBorrow(testOnBorrow);
  38. datasource.setTestOnReturn(testOnReturn);
  39. datasource.setPoolPreparedStatements(poolPreparedStatements);
  40. datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  41. return datasource;
  42. }
  43. @Bean(name = "hiveDruidTemplate")
  44. public JdbcTemplate hiveDruidTemplate(@Qualifier("hiveDruidDataSource") DataSource dataSource) {
  45. return new JdbcTemplate(dataSource);
  46. }

This code is an example of configuring a Hive database connection using Spring Boot and Druid connection pool. It shows how to@ConfigurationPropertiesAnnotations to simplify the binding of configuration properties and how to use Spring@BeanAnnotations to create and manageDataSourceandJdbcTemplateBean. The following is a detailed explanation of this code:

1. @ConfigurationProperties(prefix = "hive")

  • effect: This annotation is used to add configuration files (such asapplication.propertiesorapplication.yml) is prefixed withhiveThe properties are bound to the current class (HiveDruidConfig) fields. This makes it very convenient and centralized to configure database connections and other related parameters.
  • example: If you areapplication.propertiesThere arehive.url=jdbc:hive2://...,SourlThe fields will be automatically assigned values.

2. @Data

  • effect: This annotation comes from the Lombok library, which automatically generates getter, setter, equals, hashCode, and toString methods for the fields of the class. This reduces boilerplate code and makes the class more concise.

3. Field Definition

  • The class defines several private fields, which correspond to the configuration parameters of the Hive database connection and the Druid connection pool.

4. dataSource()method

  • effect:This method uses@Beanannotation, indicating that it will return a Bean, which will be managed by the Spring container. In this example, it creates and configures aDruidDataSourceInstance, which is the implementation of Druid connection pool.
  • Configuration: The various properties of DruidDataSource are set by setter methods. These properties come from the values ​​of class fields, which are set by@ConfigurationPropertiesObtained from the configuration file.
  • return value: The method returns the configuredDruidDataSourceInstance, the Spring container will register it as ahiveDruidDataSourceBeans.

5. hiveDruidTemplate()method

  • effect: This method also uses@BeanNote that it creates aJdbcTemplateInstance, this instance is used to simplify database operations.
  • parameter: The method receives aDataSourceType parameters, through@Qualifier("hiveDruidDataSource")The annotation specifies the use ofhiveDruidDataSourceDataSource Bean.
  • return value: The method returns the configuredJdbcTemplateInstance, the Spring container will register it as ahiveDruidTemplateBeans.

Summarize

This code shows how to use@ConfigurationPropertiesand@BeanAnnotations are used to configure and manage database connections. By using the Druid connection pool, the performance and stability of database operations can be further improved.JdbcTemplate, developers can perform database operations more conveniently without writing a lot of JDBC code.