前提: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寫的一個多配置連接數據庫工具,已上傳到百度網盤上
啟動類:HbaseApplication
@SpringBootApplication public class HbaseApplication { public static void main(String[] args) throws IOException { File workaround = new File("."); System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath()); new File("./bin").mkdirs(); new File("./bin/winutils.exe").createNewFile(); System. setProperty("java.awt.headless", "false"); SpringApplication app = new SpringApplication(HbaseApplication.class); app.addListeners(new ApplicationStartup()); app.run(args); } }
監聽:
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } new DBManager().initUI(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } } }); } }
配置文件:application.yml
hbase:
local:
config:
hbase.zookeeper.quorum: ddatanode17222.ifoton.com.cn,dnamenode17226.ifoton.com.cn,ddatanode17224.ifoton.com.cn,dnamenode17225.ifoton.com.cn,ddatanode17223.ifoton.com.cn
hbase.zookeeper.property.clientPort: 2181
hbase.rest.ssl.enabled: false
hbase.rpc.protection: authentication
hbase.security.authentication: simple
hbase.snapshot.master.timeout.millis: 60000
hbase.snapshot.region.timeout: 60000
hbase.snapshot.master.timeoutMillis: 60000
hbase.snapshot.enabled: true
hbase.rpc.timeout: 60000
hbase.thrift.support.proxyuser: false
hbase.regionserver.thrift.http: false
hbase.coprocessor.region.classes: org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint
hbase.client.scanner.timeout.period: 60000
hbase.fs.tmp.dir: /user/${user.name}/hbase-staging
hbase.client.primaryCallTimeout.multiget: 10
hbase.client.primaryCallTimeout.get: 10
hbase.ipc.client.allowsInterrupt: true
hbase.client.keyvalue.maxsize: 10485760
hbase.client.scanner.caching: 100
hbase.client.retries.number: 35
hbase.client.pause: 100
hbase.client.write.buffer: 2097152
hbase.rootdir: hdfs://nameservice226/hbase
zookeeper.znode.rootserver: root-region-server
zookeeper.session.timeout: 60000
integrate:
config:
hbase.zookeeper.quorum: pdatanode66.ifoton.com.cn,pdatanode64.ifoton.com.cn,pdatanode63.ifoton.com.cn,pdatanode65.ifoton.com.cn,pnamenode62.ifoton.com.cn
hbase.zookeeper.property.clientPort: 2181
hbase.rest.ssl.enabled: false
hbase.rpc.protection: authentication
hbase.security.authentication: simple
hbase.snapshot.master.timeout.millis: 60000
hbase.snapshot.region.timeout: 60000
hbase.snapshot.master.timeoutMillis: 60000
hbase.snapshot.enabled: true
hbase.rpc.timeout: 60000
hbase.thrift.support.proxyuser: false
hbase.regionserver.thrift.http: false
hbase.coprocessor.region.classes: org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint
hbase.client.scanner.timeout.period: 60000
hbase.client.primaryCallTimeout.multiget: 10
hbase.client.primaryCallTimeout.get: 10
hbase.ipc.client.allowsInterrupt: true
hbase.client.keyvalue.maxsize: 10485760
hbase.client.scanner.caching: 100
hbase.client.retries.number: 35
hbase.client.pause: 100
hbase.client.write.buffer: 2097152
hbase.rootdir: hdfs://nameservice1/hbase
zookeeper.znode.rootserver: root-region-server
zookeeper.session.timeout: 60000
impala:
url: jdbc:impala://172.24.17.158:21050/default;AuthMech=0;SocketTimeout=0
port: 21050
schema: default
user: impala
password: impala
driverClass: com.cloudera.impala.jdbc41.Driver
url2: jdbc:impala://10.100.62.61:21050/default;AuthMech=0;SocketTimeout=0
spring:
freemarker:
cache: false
charset: utf-8
enabled: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
max-idle: 10
max-wait: 10000
min-idle: 5
initial-size: 5
integrate:
driver-class-name: oracle.jdbc.OracleDriver
username: foton
password: 3UI%die)e2_2urYUCN
jdbc-url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.100.58.218)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=iovdb)))
type: ORACLE
local:
driver-class-name: oracle.jdbc.OracleDriver
username: foton
password: 7Ca30Rt
jdbc-url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=172.24.7.189)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=iovdb)))
type: ORACLE
配置類:ImpalaConfig:
@Configuration public class ImpalaConfig { @Value("${impala.url}") private String impalaUrl; @Value("${impala.url2}") private String impalaUrl2; @Value("${impala.port}") private String impalaPort; @Value("${impala.schema}") private String impalaScheme; @Value("${impala.user}") private String impalaUser; @Value("${impala.password}") private String impalaPassword; @Bean(name = {"impalaJdbcTemplate"}) @Qualifier("impalaJdbcTemplate") public JdbcTemplate getImpalaJdbcTemplate() { String connUrl = this.impalaUrl + ":" + this.impalaPort + "/" + this.impalaScheme + ";auth=noSasl"; DataSource dataSource = new DataSource(); dataSource.setURL(connUrl); return new JdbcTemplate(dataSource); } @Bean(name = {"impalaJdbcTemplate2"}) @Qualifier("impalaJdbcTemplate2") public JdbcTemplate getImpalaJdbcTemplate2() { String connUrl = this.impalaUrl2 + ":" + this.impalaPort + "/" + this.impalaScheme + ";auth=noSasl"; DataSource dataSource = new DataSource(); dataSource.setURL(connUrl); return new JdbcTemplate(dataSource); }
配置類2:IntegrateHBaseConfig:
@Configuration @EnableConfigurationProperties(IntegrateHBaseProperties.class) public class IntegrateHBaseConfig { private final IntegrateHBaseProperties properties; public IntegrateHBaseConfig(IntegrateHBaseProperties properties) { this.properties = properties; } @Bean(name = "integrateHBaseService") @Qualifier("integrateHBaseService") @Primary public HBaseService integrateHBaseService() { org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create(); Map<String, String> config = properties.getConfig(); Set<String> keySet = config.keySet(); for (String key : keySet) { conf.set(key, config.get(key)); } return new HBaseService(conf); } }
配置類3:LocalHBaseConfig
@Configuration @EnableConfigurationProperties(LocalHBaseProperties.class) public class LocalHBaseConfig { private final LocalHBaseProperties properties; public LocalHBaseConfig(LocalHBaseProperties properties) { this.properties = properties; } @Bean(name = "localHBaseService") @Qualifier("localHBaseService") @Primary public HBaseService localHBaseService() { org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create(); Map<String, String> config = properties.getConfig(); Set<String> keySet = config.keySet(); for (String key : keySet) { conf.set(key, config.get(key)); } return new HBaseService(conf); } }
配置類4:OracleConfig
@Configuration public class OracleConfig { //@Primary注解在哪個ds,默認使用那個ds @Bean(name = "integrateDataSource") @Qualifier("integrateDataSource") @ConfigurationProperties(prefix="spring.datasource.integrate") public DataSource integrateDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "localDataSource") @Qualifier("localDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.local") public DataSource localDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "integrateJdbcTemplate") public JdbcTemplate integrateJdbcTemplate( @Qualifier("integrateDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "localJdbcTemplate") public JdbcTemplate localJdbcTemplate( @Qualifier("localDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
業務類:DBService
@Service public class DBService { @Autowired @Qualifier("localHBaseService") HBaseService localHBaseService; @Autowired @Qualifier("integrateHBaseService") HBaseService integrateHBaseService; @Autowired @Qualifier("localJdbcTemplate") JdbcTemplate localJdbcTemplate; @Autowired @Qualifier("impalaJdbcTemplate") JdbcTemplate impalaJdbcTemplate; @Autowired @Qualifier("impalaJdbcTemplate2") JdbcTemplate impalaJdbcTemplate2; @Autowired @Qualifier("integrateJdbcTemplate") JdbcTemplate integrateJdbcTemplate; 。。。 }