案例: springboot 同时集成hive和mysql

Posted 袁义锐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了案例: springboot 同时集成hive和mysql相关的知识,希望对你有一定的参考价值。

1.配置maven( pom.xml文件 ):

   1.1 集成hive:

<!-- hive -->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>1.1.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpcore</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                </exclusion>
            </exclusions>
            <version>2.7.3</version>
        </dependency>

   1.2 集成mysql

 <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
            <!--<version>5.1.30</version>-->
        </dependency>

2.配置数据源(.yml文件)

   2.1 hive 数据源:

#hive数据库配置
hive:
  datasource:
    druid:
      username: hive
      password: hive
      driverClassName: org.apache.hive.jdbc.HiveDriver
      url: jdbc:hive2://IP:端口/default
      initialSize: 10
      maxActive: 50
      removeAbandoned: true
      removeAbandonedTimeout: 60
      maxWait: 60000

 2.2 mysql 数据源:

mysql:
  datasource:
    druid:
      url: jdbc:mysql://IP:端口/数据库名称?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
      username: 用户名
      password: 密码
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20

3.编写多数据源代码:

  3.1 hive 数据源连接代码:

@Configuration
public class HiveDataSourceConfig 

    @Bean(name = "hiveDataSource")
    @ConfigurationProperties(prefix = "hive.datasource.druid")//和yml中的配置项,呼应
    public DataSource dataSource() 
        return DruidDataSourceBuilder.create().build();
    

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate getJdbcTemplate(@Qualifier("hiveDataSource") DataSource dataSource)
        return new JdbcTemplate(dataSource);
    

    /**
     * 获取hive连接
     */
    public Connection getHiveConnection() throws SQLException 
        return this.dataSource().getConnection();
    

   3.2 mysql 数据源连接代码:

@Configuration
@MapperScan(basePackages = "com.web.dao", sqlSessionTemplateRef = "mySqlSqlSessionTemplate" ,sqlSessionFactoryRef = "mySqlSqlSessionFactory")
public class MySqlDataSourceConfig 

    @Primary
    @Bean(name = "mySqlDataSource")
    @ConfigurationProperties(prefix = "mysql.datasource.druid")
    public DataSource mySqlDataSource() 
        return DruidDataSourceBuilder.create().build();
    

    @Primary
    @Bean(name = "mySqlTransactionManager")
    public DataSourceTransactionManager mySqlTransactionManager(@Qualifier("mySqlDataSource") DataSource dataSource) 
        return new DataSourceTransactionManager(dataSource);
    


    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")//需要和yml中的mybatis配置对应
    public org.apache.ibatis.session.Configuration configuration()
        return new org.apache.ibatis.session.Configuration();
    

    @Primary
    @Bean(name = "mySqlSqlSessionFactory")
    public SqlSessionFactory mySqlSqlSessionFactory(@Qualifier("mySqlDataSource") DataSource dataSource,org.apache.ibatis.session.Configuration configuration) throws Exception 
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    

    @Bean(name = "mySqlSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate mySqlSqlSessionTemplate(@Qualifier("mySqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
        return new SqlSessionTemplate(sqlSessionFactory);
    

  3.2中需要补充mybatis的扫描配置:

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.web.model
  configuration:
    log-impl: org.apache.ibatis.logging.log4j.Log4jImpl
    map-underscore-to-camel-case: true

 

以上是关于案例: springboot 同时集成hive和mysql的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot——SpringBoot集成SSMDubboRedisJSP的汇总案例

SpringBoot——SpringBoot集成SSMDubboRedisJSP的汇总案例

HBase与Hive集成

使用 CqlStorageHandler 集成 Hive 和 Cassandra

(十九)ATP应用测试平台——springboot集成RocketMQ案例实战

SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理