2016-02-03 99 views
0

我们正在使用Azure和Phoenix写入HBase群集。有两个驱动程序:一个大的和一个瘦客户端。瘦客户机使用JSON over HTTP与数据库进行交互。“聚合”Phoenix Query Server连接的正确方法是什么

当我们创建一个新的连接每次保存时,我们可以保存在每次保存约150ms。

this.conn = DriverManager.getConnection("jdbc:phoenix:thin:url=http://1.1.1.1"); 

当我们重新使用连接时,我们可以在每次保存70ms时完成:显着的加速。文档有点模糊,在厚厚的客户和思想客户之间转移。

那么汇集瘦客户机连接的最佳做法是什么?

重要更改!

我们开始有一些连接问题,所以我想回到我们的代码并做了一些修改。我设置了一些定时器,发现上面的代码工作在0ms。我不知道我上面做错了什么。

因此,正确的方式来将凤凰池汇集在一起​​,不要将凤凰池。有几个帖子证实了开发团队的这一点。

拥有巨大的SQL Oracle/DB2/SqlServer背景可能是我的失败。使用Redis或Phoenix或者任何新的No-sql数据库都与SQL非常不同。我的建议是“读取你正在使用的产品的指示”,并做他们告诉你做的事!

回答

0

根据the FAQ from the Phoenix offical site,不需要汇聚Phoenix JDBC连接。

由于基础HBase连接,Phoenix的Connection对象与大多数其他JDBC连接不同。菲尼克斯连接对象被设计成一个成本低廉的薄物件。如果重新使用Phoenix连接,则以前的用户可能不会始终将底层HBase连接保持在健康状态。最好创建新的Phoenix连接以确保避免任何潜在问题。

凤凰池实现可以简单地通过创建一个实例从池中取出时,新凤凰城连接,然后关闭它返回到池中时,连接一个委托连接来完成(见PHOENIX-2388)。

我认为粗体上面的内容是集中瘦客户端连接的最佳实践。

您可以尝试在客户端设置下面的配置以调整性能,请在Configuration and Tuning查看更多详细信息。 enter image description here


更新:我下面简单实现对凤凰连接池

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Queue; 
import java.util.concurrent.ConcurrentLinkedQueue; 

public class DataSource { 

    private String driverClass; 
    private String jdbcUrl; 
    private String user; 
    private String password; 
    private int min; // The number of connections of initial pool 
    private int max; 
    private int used; // The number of connections of polled and not released from poll 
    private Queue<Connection> pool = null; 

    public DataSource(String driverClass, String jdbcUrl, String user, String password, int min, int max) { 
     this.driverClass = driverClass; 
     this.jdbcUrl = jdbcUrl; 
     this.user = user; 
     this.password = password; 
     this.min = min; 
     this.max = max; 
     this.used = 0; 
     this.pool = new ConcurrentLinkedQueue<Connection>(); 
     init(); 
    } 

    private void init() { 
     try { 
      Class.forName(driverClass); 
      for (int i = 0; i < min; i++) { 
       Connection conn = DriverManager.getConnection(jdbcUrl, user, password); 
       pool.add(conn); 
      } 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Connection getConnection() { 
     Connection conn = null; 
     if (pool.size() > 0) { 
      conn = pool.poll(); 
      Thread connGen = new Thread(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         Connection conn = DriverManager.getConnection(jdbcUrl, user, password); 
         pool.add(conn); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      connGen.start(); 
      used ++; 
     } else if(used < max) { 
      try { 
       conn = DriverManager.getConnection(jdbcUrl, user, password); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
     return conn; 
    } 

    public void releaseConnection(Connection conn) { 
     if (conn != null) { 
      try { 
       conn.close(); 
       used--; 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 
+0

是的,我看到了这一点,但是创造了“新凤凰连接”走的是一条很长的时间。我们可以重复使用连接的一半时间。任何想法是什么“代表连接”?我会喜欢一些代码... – markthegrea

+1

@markthegrea我只是为凤凰实现一个池。没有测试,只是参考。 –

相关问题