前提:1.连接hadoop需要本地安装 winutils.exe 并在程序指定,不然程序会报错  IOException: HADOOP_HOME or hadoop.home.dir are not set. 但不指定程序可能启动。

2.要在springboot容器启动后再初始化页面,否则使用时会取不到bean

整体思路:程序启动时,扫描所有包的@Configation,有使用到自定义的配置可以用@Value映射变量

HBASE读取配置:因为涉及到的配置较多,有两种方式可以考虑,最终注入,反转来调用的只有两个变量  admin、connetion来实现查询和持久化功能

第一种方式:通过xml配置文件,命名一定要hbase-site.xml(还有一个hbase-defualt.xml),这样在装载 org.apache.hadoop.conf.Configuration时程序就会自动读取resource下的xml文件封装。

第二种方式:通过yml配置信息,但不用所有变量都取出来;写一个工具类,IntegrateHBaseProperties指定hbase配置的父路径,并写一个config的set方法,定义一个map变量;在读取配置类里启动这个类                                  @EnableConfigurationProperties(IntegrateHBaseProperties.class),数据就自动封装到map变量里,再用set方法读取放在org.apache.hadoop.conf.Configuration里;

ORACLE以及IMPALA配置:这两者是一样的,这里都用读取yml中的配置,拿到dataSource和jdbcTemplate,而jdbcTemplate这个类里就有我们所有想要的操作了;

下面代码只贴一些主要的,源码是我用swing写的一个多配置连接数据库工具,已上传到百度网盘上

  1. 启动类:HbaseApplication
  1. @SpringBootApplication
  2. public class HbaseApplication {
  3.  
  4. public static void main(String[] args) throws IOException {
  5. File workaround = new File(".");
  6. System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath());
  7. new File("./bin").mkdirs();
  8. new File("./bin/winutils.exe").createNewFile();
  9. System. setProperty("java.awt.headless", "false");
  10. SpringApplication app = new SpringApplication(HbaseApplication.class);
  11. app.addListeners(new ApplicationStartup());
  12. app.run(args);
  13. }
  14.  
  15. }

配置文件:application.yml

  1. hbase:
  2. local:
  3. config:
  4. hbase.zookeeper.quorum: ddatanode17222.ifoton.com.cn,dnamenode17226.ifoton.com.cn,ddatanode17224.ifoton.com.cn,dnamenode17225.ifoton.com.cn,ddatanode17223.ifoton.com.cn
  5. hbase.zookeeper.property.clientPort: 2181
  6. hbase.rest.ssl.enabled: false
  7. hbase.rpc.protection: authentication
  8. hbase.security.authentication: simple
  9. hbase.snapshot.master.timeout.millis: 60000
  10. hbase.snapshot.region.timeout: 60000
  11. hbase.snapshot.master.timeoutMillis: 60000
  12. hbase.snapshot.enabled: true
  13. hbase.rpc.timeout: 60000
  14. hbase.thrift.support.proxyuser: false
  15. hbase.regionserver.thrift.http: false
  16. hbase.coprocessor.region.classes: org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint
  17. hbase.client.scanner.timeout.period: 60000
  18. hbase.fs.tmp.dir: /user/${user.name}/hbase-staging
  19. hbase.client.primaryCallTimeout.multiget: 10
  20. hbase.client.primaryCallTimeout.get: 10
  21. hbase.ipc.client.allowsInterrupt: true
  22. hbase.client.keyvalue.maxsize: 10485760
  23. hbase.client.scanner.caching: 100
  24. hbase.client.retries.number: 35
  25. hbase.client.pause: 100
  26. hbase.client.write.buffer: 2097152
  27. hbase.rootdir: hdfs://nameservice226/hbase
  28. zookeeper.znode.rootserver: root-region-server
  29. zookeeper.session.timeout: 60000
  30. integrate:
  31. config:
  32. hbase.zookeeper.quorum: pdatanode66.ifoton.com.cn,pdatanode64.ifoton.com.cn,pdatanode63.ifoton.com.cn,pdatanode65.ifoton.com.cn,pnamenode62.ifoton.com.cn
  33. hbase.zookeeper.property.clientPort: 2181
  34. hbase.rest.ssl.enabled: false
  35. hbase.rpc.protection: authentication
  36. hbase.security.authentication: simple
  37. hbase.snapshot.master.timeout.millis: 60000
  38. hbase.snapshot.region.timeout: 60000
  39. hbase.snapshot.master.timeoutMillis: 60000
  40. hbase.snapshot.enabled: true
  41. hbase.rpc.timeout: 60000
  42. hbase.thrift.support.proxyuser: false
  43. hbase.regionserver.thrift.http: false
  44. hbase.coprocessor.region.classes: org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint
  45. hbase.client.scanner.timeout.period: 60000
  46. hbase.client.primaryCallTimeout.multiget: 10
  47. hbase.client.primaryCallTimeout.get: 10
  48. hbase.ipc.client.allowsInterrupt: true
  49. hbase.client.keyvalue.maxsize: 10485760
  50. hbase.client.scanner.caching: 100
  51. hbase.client.retries.number: 35
  52. hbase.client.pause: 100
  53. hbase.client.write.buffer: 2097152
  54. hbase.rootdir: hdfs://nameservice1/hbase
  55. zookeeper.znode.rootserver: root-region-server
  56. zookeeper.session.timeout: 60000
  57.  
  58. impala:
  59. url: jdbc:impala://172.24.17.158:21050/default;AuthMech=0;SocketTimeout=0
  60. port: 21050
  61. schema: default
  62. user: impala
  63. password: impala
  64. driverClass: com.cloudera.impala.jdbc41.Driver
  65. url2: jdbc:impala://10.100.62.61:21050/default;AuthMech=0;SocketTimeout=0
  66.  
  67. spring:
  68. freemarker:
  69. cache: false
  70. charset: utf-8
  71. enabled: true
  72. datasource:
  73. type: com.alibaba.druid.pool.DruidDataSource
  74. max-idle: 10
  75. max-wait: 10000
  76. min-idle: 5
  77. initial-size: 5
  78. integrate:
  79. driver-class-name: oracle.jdbc.OracleDriver
  80. username: foton
  81. password: 3UI%die)e2_2urYUCN
  82. jdbc-url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.100.58.218)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=iovdb)))
  83. type: ORACLE
  84. local:
  85. driver-class-name: oracle.jdbc.OracleDriver
  86. username: foton
  87. password: 7Ca30Rt
  88. jdbc-url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=172.24.7.189)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=iovdb)))
  89. type: ORACLE

配置类:ImpalaConfig:

  1. @Configuration
  2. public class ImpalaConfig {
  3. @Value("${impala.url}")
  4. private String impalaUrl;
  5. @Value("${impala.url2}")
  6. private String impalaUrl2;
  7. @Value("${impala.port}")
  8. private String impalaPort;
  9. @Value("${impala.schema}")
  10. private String impalaScheme;
  11. @Value("${impala.user}")
  12. private String impalaUser;
  13. @Value("${impala.password}")
  14. private String impalaPassword;
  15.  
  16. @Bean(name = {"impalaJdbcTemplate"})
  17. @Qualifier("impalaJdbcTemplate")
  18. public JdbcTemplate getImpalaJdbcTemplate() {
  19. String connUrl = this.impalaUrl + ":" + this.impalaPort + "/" + this.impalaScheme + ";auth=noSasl";
  20. DataSource dataSource = new DataSource();
  21. dataSource.setURL(connUrl);
  22. return new JdbcTemplate(dataSource);
  23. }
  24.  
  25. @Bean(name = {"impalaJdbcTemplate2"})
  26. @Qualifier("impalaJdbcTemplate2")
  27. public JdbcTemplate getImpalaJdbcTemplate2() {
  28. String connUrl = this.impalaUrl2 + ":" + this.impalaPort + "/" + this.impalaScheme + ";auth=noSasl";
  29. DataSource dataSource = new DataSource();
  30. dataSource.setURL(connUrl);
  31. return new JdbcTemplate(dataSource);
  32. }
  1. 配置类2IntegrateHBaseConfig
  1. @Configuration
  2. @EnableConfigurationProperties(IntegrateHBaseProperties.class)
  3. public class IntegrateHBaseConfig {
  4.  
  5. private final IntegrateHBaseProperties properties;
  6.  
  7. public IntegrateHBaseConfig(IntegrateHBaseProperties properties) {
  8. this.properties = properties;
  9. }
  10.  
  11. @Bean(name = "integrateHBaseService")
  12. @Qualifier("integrateHBaseService")
  13. @Primary
  14. public HBaseService integrateHBaseService() {
  15. org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
  16. Map<String, String> config = properties.getConfig();
  17. Set<String> keySet = config.keySet();
  18. for (String key : keySet) {
  19. conf.set(key, config.get(key));
  20. }
  21. return new HBaseService(conf);
  22. }
  23.  
  24. }

配置类3:LocalHBaseConfig

  1. @Configuration
  2. @EnableConfigurationProperties(LocalHBaseProperties.class)
  3. public class LocalHBaseConfig {
  4.  
  5. private final LocalHBaseProperties properties;
  6.  
  7. public LocalHBaseConfig(LocalHBaseProperties properties) {
  8. this.properties = properties;
  9. }
  10.  
  11. @Bean(name = "localHBaseService")
  12. @Qualifier("localHBaseService")
  13. @Primary
  14. public HBaseService localHBaseService() {
  15. org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
  16. Map<String, String> config = properties.getConfig();
  17. Set<String> keySet = config.keySet();
  18. for (String key : keySet) {
  19. conf.set(key, config.get(key));
  20. }
  21. return new HBaseService(conf);
  22. }
  23.  
  24. }

配置类4:OracleConfig

  1. @Configuration
  2. public class OracleConfig {
  3. //@Primary注解在哪个ds,默认使用那个ds
  4.  
  5. @Bean(name = "integrateDataSource")
  6. @Qualifier("integrateDataSource")
  7. @ConfigurationProperties(prefix="spring.datasource.integrate")
  8. public DataSource integrateDataSource() {
  9. return DataSourceBuilder.create().build();
  10. }
  11.  
  12. @Bean(name = "localDataSource")
  13. @Qualifier("localDataSource")
  14. @Primary
  15. @ConfigurationProperties(prefix="spring.datasource.local")
  16. public DataSource localDataSource() {
  17. return DataSourceBuilder.create().build();
  18. }
  19.  
  20. @Bean(name = "integrateJdbcTemplate")
  21. public JdbcTemplate integrateJdbcTemplate(
  22. @Qualifier("integrateDataSource") DataSource dataSource) {
  23. return new JdbcTemplate(dataSource);
  24. }
  25.  
  26. @Bean(name = "localJdbcTemplate")
  27. public JdbcTemplate localJdbcTemplate(
  28. @Qualifier("localDataSource") DataSource dataSource) {
  29. return new JdbcTemplate(dataSource);
  30. }
  31.  
  32. }

业务类:DBService

  1. @Service
  2. public class DBService {
  3.  
  4. @Autowired
  5. @Qualifier("localHBaseService")
  6. HBaseService localHBaseService;
  7. @Autowired
  8. @Qualifier("integrateHBaseService")
  9. HBaseService integrateHBaseService;
  10. @Autowired
  11. @Qualifier("localJdbcTemplate")
  12. JdbcTemplate localJdbcTemplate;
  13. @Autowired
  14. @Qualifier("impalaJdbcTemplate")
  15. JdbcTemplate impalaJdbcTemplate;
  16. @Autowired
  17. @Qualifier("impalaJdbcTemplate2")
  18. JdbcTemplate impalaJdbcTemplate2;
  19. @Autowired
  20. @Qualifier("integrateJdbcTemplate")
  21. JdbcTemplate integrateJdbcTemplate;
  22.  
  23. 。。。
  24. }
  1.  

SpringBoot连接多数据源(HBASE,KUDU,ORACLE集成和开发库)的更多相关文章

  1. SpringBoot添加多数据源mysql和oracle

    项目结构 多数据源配置文件 MultiDataSourceConfig.java SqlSessionTemplate1.java SqlSessionTemplate2.java package c ...

  2. Oracle连接odbc数据源

    Oracle连接odbc数据源 说明 oracle连接ODBC数据源有两种方式,hsodbc和dg4odbc,简单说dg4odbc是hsodbc的升级.两种连接方法大致一样,现将连接步骤说明如下: 检 ...

  3. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  4. Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源

      本文链接:https://blog.csdn.net/wjy511295494/article/details/78825890 Springboot+Mybatis+Pagehelper+Aop ...

  5. Superset连接Impala数据源

    公司最近在superset上面做二次开发,目前对接了mysql和oracle数据源,对这两个源的SQL操作查询做了完善和兼容.目前有新的需求就是要对接大数据部门的HBASE和HIVE数据源,由于sup ...

  6. springboot+ibatis 多数据源配置

    这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...

  7. SpringBoot配置多数据源时遇到的问题

    SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...

  8. 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化

    第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...

  9. SpringBoot 的多数据源配置

    最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...

随机推荐

  1. kali一些基础工具

    目录 netcat netcat https://blog.csdn.net/fageweiketang/article/details/82833193 网络工具当中的瑞士军刀 -nc指令,nc可以 ...

  2. Linux初步了解

    文件系统类型:swap分区:用于内存扩充,用磁盘兑换,磁盘是内存的两倍,便于替换 / 分区:容纳根文件系统 文件名:由字母,数字,下划线,圆点组成 文件类型:显示在每一行的左边第一个字符 普通文件   ...

  3. 破晓行动----带你总结JVM的知识大全(一)

    JVM线程 + JVM内存区域

  4. iNeuOS工业互联平台,机床&PLC硬件网关与平台无缝对接,进行数据交互

    目       录 1.      概述... 2 2.      平台演示... 2 3.      硬件网关的基本操作... 3 3.1           数据采集... 3 3.2       ...

  5. mysql-2-where

    #进阶2:条件查询 /* 语法: SELECT 查询列表 FROM 表名 WHERE 筛选条件 分类: 1.按条件表达式筛选:> < = != <> >= <= 2 ...

  6. 080 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 05 单一职责原则

    080 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 05 单一职责原则 本文知识点:单一职责原则 说明:因为时间紧张,本人写博客过程中只是 ...

  7. 033 01 Android 零基础入门 01 Java基础语法 03 Java运算符 13 运算符和表达式知识点总结

    033 01 Android 零基础入门 01 Java基础语法 03 Java运算符 13 运算符和表达式知识点总结 本文知识点:运算符和表达式知识点总结 前面学习的几篇文都是运算符和表达式相关的知 ...

  8. helm包管理工具

    K8S正常部署应用是如下方式 kubectl create deployment web --image=nginx --dru-run=client -o yaml > web.yaml ku ...

  9. 在Windows7系统中设置虚拟内存大小

    当我们的电脑物理内存空间不够用时,操作系统就会自动从硬盘空间上分出一块空间来当内存使用,这就是虚拟内存.可以说虚拟内存是物理内存的补充,是备用的物理内存.一般来说,如果电脑里的程序不多,占用内存资源不 ...

  10. MeteoInfoLab脚本示例:格点数据散点图

    绘制格点数据的散点图,用scaterm函数. 脚本程序: f = addfile('D:/Temp/GrADS/model.ctl') ps = f['PS'][0,(10,60),(60,140)] ...