开发者社区> 问答> 正文

分析型数据库如何进行业务系统连接并进行查询?




8.1 业务系统连接分析型数据库并进行查询


使用jdbc odbc php python R-Mysql等连接分析型数据库的例子和注意事项介绍,以及流控、retry链接、异常处理等方法。
分析型数据库集群系统部署在阿里云环境中,用户在部署业务应用系统时,尽可能保证业务系统与阿里云环境的网络联通性,如购买阿里云ECS主机( https://www.aliyun.com/product/ecs/)作为业务系统的服务器。

JDBC直接连接分析型数据库


最简单直接的连接并访问分析型数据库的方式是通过JDBC,分析型数据库支持MySQL自带的客户端以及大部分版本的mysql-jdbc驱动。

支持的mysql jdbc驱动版本号


  • 5.0系列: 5.0.2,5.0.3,5.0.4,5.0.5,5.0.7,5.0.8
  • 5.1系列:5.1.1,5.1.2,5.1.3,5.1.4,5.1.5,5.1.6,5.1.7,5.1.8,5.1.11,5.1.12,5.1.13,5.1.14,5.1.15,5.1.16,5.1.17,5.1.18,5.1.19,5.1.20,5.1.21,5.1.22,5.1.23,5.1.24,5.1.25,5.1.26,5.1.27,5.1.28,5.1.29,5.1.31,5.1.32,5.1.33,5.1.34
  • 5.4系列
  • 5.5系列

Java程序中,将合适的mysql-jdbc驱动包(mysql-connector-java-x.x.x.jar)加入CLASSPATH中,通过以下示例程序就能连接并访问分析型数据库。通过该JDBC方式直连分析型数据库时,和直连MySQL类似,需注意在使用完连接并不准备进行复用的情况下,需要释放连接资源。用户根据具体情况,设置${user_db},${url},${my_access_key_id},${my_access_key_secret}和${query}值。

不带重试的JDBC样例程序片段

  1. Connection connection = null;
  2. Statement statement = null;
  3. ResultSet rs = null;
  4. try {
  5.     Class.forName("com.mysql.jdbc.Driver");
  6.     String url = "jdbc:mysql://mydbname.ads-hz.aliyuncs.com:5544/my_ads_db?useUnicode=true&characterEncoding=UTF-8";
  7.     Properties connectionProps = new Properties();
  8.     connectionProps.put("user", "my_access_key_id");
  9.     connectionProps.put("password", "my_access_key_secret");
  10.     connection = DriverManager.getConnection(url, connectionProps);
  11.     statement = connection.createStatement();
  12.     String query = "select count(*) from information_schema.tables";
  13.     rs = statement.executeQuery(query);
  14.     while (rs.next()) {
  15.         System.out.println(rs.getObject(1));
  16.     }
  17. } catch (ClassNotFoundException e) {
  18.     e.printStackTrace();
  19. } catch (SQLException e) {
  20.     e.printStackTrace();
  21. } catch (Exception e) {
  22.     e.printStackTrace();
  23. } finally {
  24.     if (rs != null) {
  25.         try {
  26.             rs.close();
  27.         } catch (SQLException e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.     if (statement != null) {
  32.         try {
  33.             statement.close();
  34.         } catch (SQLException e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.     if (connection != null) {
  39.         try {
  40.             connection.close();
  41.         } catch (SQLException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45. }


带重试的JDBC样例程序片段

  1. public static final int MAX_QUERY_RETRY_TIMES = 3;
  2. public static Connection conn = null;
  3. public static Statement statement = null;
  4. public static ResultSet rs = null;
  5. public static void main(String[] args) throws ClassNotFoundException {
  6.     String yourDB = "user_db";
  7.     String username = "my_access_key_id";
  8.     String password = "my_access_key_secret";
  9.     Class.forName("com.mysql.jdbc.Driver");
  10.     String url = "jdbc:mysql://mydbname.ads-hz.aliyuncs.com:5544/" + yourDB + "?useUnicode=true&characterEncoding=UTF-8";
  11.     Properties connectionProps = new Properties();
  12.     connectionProps.put("user", username);
  13.     connectionProps.put("password", password);
  14.     String query = "select id from test4dmp.test limit 10";
  15.     int retryTimes = 0;
  16.     while (retryTimes < MAX_QUERY_RETRY_TIMES) {
  17.         try {
  18.             getConn(url, connectionProps);
  19.             execQuery(query);
  20.             break; // Query execution successfully, break out.
  21.         } catch (SQLException

展开
收起
nicenelly 2017-10-31 13:44:12 1934 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
基于云原生数据仓库AnalyticDB PG的最佳实践 立即下载
新氧云原生全栈数仓最佳实践 立即下载
离线实时一体化数仓与湖仓一体—云原生大数据平台的持续演进 立即下载