首页 > 代码库 > drill java && spring jdbc 连接使用

drill java && spring jdbc 连接使用

 原生 jdbc 连接
 
1. maven 包
  1. <dependency>
  2. <groupId>org.apache.drill.exec</groupId>
  3. <artifactId>drill-jdbc-all</artifactId>
  4. <version>1.10.0</version>
  5. </dependency>
2. 代码
  1. Class.forName("org.apache.drill.jdbc.Driver");
  2. Connection connection =DriverManager.getConnection("jdbc:drill:zk=10.10.5.18:2181/drill/demo");
  3. Statement st = connection.createStatement();
  4. ResultSet rs = st.executeQuery("select * from dfs.`/root/drill/drill/sample-data/user.json`");
  5. while(rs.next()){
  6. System.out.println(rs.getString(1));
  7. }
3. 查询结果
 
技术分享
 
使用spring boot  jdbc
1. spring boot maven 
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.drill.exec</groupId>
  7. <artifactId>drill-jdbc-all</artifactId>
  8. <version>1.10.0</version>
  9. </dependency>
2. 代码
  1. a.DataSourceBean定义
  2. @Bean(name="dataSource2")
  3. publicDataSource dataSource2(){
  4. BasicDataSource dataSource =newBasicDataSource();
  5. dataSource.setUrl("jdbc:drill:zk=10.10.5.18:2181/drill/demo");
  6. dataSource.setDriverClassName("org.apache.drill.jdbc.Driver");
  7. dataSource.setInitialSize(2);
  8. dataSource.setMaxActive(20);
  9. dataSource.setMinIdle(0);
  10. dataSource.setPoolPreparedStatements(true);
  11. dataSource.setMaxWait(60000);
  12. dataSource.setTestOnBorrow(false);
  13. dataSource.setTestWhileIdle(true);
  14. return dataSource;
  15. }
  16. b. jdbctempalte
  17. @Bean
  18. publicJdbcTemplate jdbcTemplate2(@Qualifier("dataSource2")DataSource dataSource2){
  19. JdbcTemplate oracle =newJdbcTemplate();
  20. oracle.setDataSource(dataSource2);
  21. return oracle;
  22. }
  23. c.查询使用
  24. @RestController
  25. publicclassDrillController{
  26. @Autowired
  27. privateJdbcTemplate jdbcTemplate2;
  28. @RequestMapping(value="/drill",method=RequestMethod.GET)
  29. publicObject getUser3(){
  30. return jdbcTemplate2.queryForList("select * from dfs.`/root/drill/drill/sample-data/user.json`");
  31. }
  32. }
3. 查询结果
  技术分享

 
 



null



drill java && spring jdbc 连接使用