DBCP 与 DBUtils 结合 配置
pheonix_jdbc.properties 文件
driverClassName=org.apache.phoenix.jdbc.PhoenixDriver url=jdbc:phoenix:10.1.20.129,10.1.20.128,10.1.20.44:2181 username=user password=pass initialSize=10 maxIdle=20 minIdle=5 maxActive=50 logAbandoned=true removeAbandoned=true removeAbandonedTimeout=180 maxWait=1000
PhoenixQueryUtils:
public class PhoenixQueryUtils { private static BasicDataSource dataSource = null; private static QueryRunner runner; private PhoenixQueryUtils() { } private synchronized static void init() throws SQLException { if (dataSource != null) { try { dataSource.close(); } catch (SQLException ex) { DbUtils.printStackTrace(ex); } } dataSource = null; InputStream in = Object.class.getResourceAsStream("/pheonix_jdbc.properties"); Properties props = new Properties(); try { props.load(in); dataSource = BasicDataSourceFactory.createDataSource(props); runner = new QueryRunner(dataSource); } catch (IOException ex) { throw new SQLException(ex); } catch (Exception ex) { throw new SQLException(ex); } } public static List<Object[]> getAllResults(String sql, Object[] params) throws SQLException { if (runner == null) { init(); } List<Object[]> ojbs = runner.query(sql, new ArrayListHandler(), params); return ojbs; } public static Object[] getFirstResult(String sql, Object[] params) throws SQLException { if (runner == null) { init(); } Object[] ojbs = runner.query(sql, new ArrayHandler(), params); return ojbs; } public static Map<String, Object> getFirstResultMap(String sql, Object[] params) throws SQLException { if (runner == null) { init(); } Map<String, Object> map = runner.query(sql, new MapHandler(), params); return map; } public static Object getResultMaps(String sql, Object[] params) throws SQLException { if (runner == null) { init(); } List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), params); return list; } public static synchronized Connection getConnection() throws SQLException, IOException, Exception { Connection conn = null; if (dataSource == null) { init(); } if (dataSource != null) { conn = dataSource.getConnection(); } return conn; } }
public static void main(String[] args) { try { List<Object[]> list = PhoenixQueryUtils.getAllResults("select id, a, b , c , d from test ", new Object[]{}); System.out.println("id \t a \t b \t c \t d"); for (Object[] objs : list) { for (Object obj : objs) { System.out.print(obj + "\t"); } System.out.println(); } } catch (SQLException ex) { DbUtils.printStackTrace(ex); } System.exit(0); }
输出:
id a b c d 5555 55 55 55 55555555 1000010 20 30 50 1415248098439 1000011 22 33 55 1415248098440 1000012 24 36 60 1415248098441 1000013 26 39 65 1415248098441 1000014 28 42 70 1415248098442 1000015 30 45 75 1415248098443 1000016 32 48 80 1415248098443 1000017 34 51 85 1415248098444 1000018 36 54 90 1415248098444 1000019 38 57 95 1415248098445 1000020 40 60 100 1415248098446 1000021 42 63 105 1415248098446 1000022 44 66 110 1415248098447 1000023 46 69 115 1415248098447 1000024 48 72 120 1415248098448 1000025 50 75 125 1415248098449 1000026 52 78 130 1415248098449 1000027 54 81 135 1415248098450 1000028 56 84 140 1415248098450