使用应用程序(Java/Python)访问MaxCompute Lightning进行数据开发
生活随笔
收集整理的這篇文章主要介紹了
使用应用程序(Java/Python)访问MaxCompute Lightning进行数据开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MaxCompute Lightning是MaxCompute產品的交互式查詢服務,支持以PostgreSQL協議及語法連接訪問Maxcompute項目,讓您使用熟悉的工具以標準 SQL查詢分析MaxCompute項目中的數據,快速獲取查詢結果。
很多開發者希望利用Lightning的特性來開發數據應用,本文將結合示例來介紹Java和Python如何連接訪問Lightning進行應用開發(參考時需要替換為您項目所在region的Endpoint及用戶認證信息)。
一、Java使用JDBC訪問Lightning
示例如下:
二、Java使用druid訪問Lightning
1.pom依賴
2.spring配置
<bean id="LightningDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="jdbc:postgresql://lightning.cn-shanghai.maxcompute.aliyun.com:443/project_name?prepareThreshold=0&sslmode=require”/> <!--替換成自己project所在region的Endpoint—><property name="username" value=“訪問用戶的Access Key ID"/><property name="password" value="訪問用戶的Access Key Secret"/><property name="driverClassName" value="org.postgresql.Driver"/><property name="dbType" value="postgresql"/><property name="initialSize" value="1" /> <property name="minIdle" value="1" /><property name="maxActive" value="5" /> <!—Lightning服務每個project的連接數限制20,所以不要配置過大,按需配置,否則容易出現query_wait_timeout錯誤 --><!--以下兩個配置,檢測連接有效性,修復偶爾出現create connection holder error錯誤 --><property name="testWhileIdle" value="true" /><property name="validationQuery" value="SELECT 1" /></bean><bean class="com.xxx.xxx.LightningProvider"><property name="druidDataSource" ref="LightningDataSource"/></bean>復制代碼3.代碼訪問
public class LightningProvider {DruidDataSource druidDataSource;/*** 執行sql* @param sql* @return* @throws Exception*/public void execute(String sql) throws SQLException {DruidPooledConnection connection = null ;Statement st = null;try{connection = druidDataSource.getConnection();st = connection.createStatement();ResultSet resultSet = st.executeQuery(sql);//對返回值的解析和處理的代碼//按行處理,每行的數據放到一個map中ResultSetMetaData metaData = resultSet.getMetaData();int columnCount = metaData.getColumnCount();List<LinkedHashMap> rows = Lists.newArrayList();while(resultSet.next()){LinkedHashMap map = Maps.newLinkedHashMap();for(int i=1;i<=columnCount;i++){String label = resultSet.getMetaData().getColumnLabel(i);map.put(label,resultSet.getString(i));}rows.add(map);} }catch (Exception e){e.printStackTrace();}finally {try {if(st!=null) {st.close();}} catch (SQLException e) {e.printStackTrace();}try {if(connection!=null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}} }復制代碼三、Python使用pyscopg2訪問Lightning
示例如下:
四、Python使用ODBC訪問Lightning
您需要現在電腦上安裝并和配置odbc驅動。代碼示例如下:
由于Lightning提供了PostgreSQL兼容的接口,您可以像開發PostgreSQL的應用一樣開發Lightning應用程序。
總結
以上是生活随笔為你收集整理的使用应用程序(Java/Python)访问MaxCompute Lightning进行数据开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何利用 gulp 压缩混淆 “上古”时
- 下一篇: MaxCompute安全管理指南-基础篇