怎样使用应用程序访问MaxCompute Lightning进行数据开发
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章将为大家详细讲解有关怎样使用应用程序访问MaxCompute Lightning进行数据开发,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Ma
千家信息网最后更新 2025年02月02日怎样使用应用程序访问MaxCompute Lightning进行数据开发
这篇文章将为大家详细讲解有关怎样使用应用程序访问MaxCompute Lightning进行数据开发,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
MaxCompute Lightning是MaxCompute产品的交互式查询服务,支持以PostgreSQL协议及语法连接访问Maxcompute项目,让您使用熟悉的工具以标准 SQL查询分析MaxCompute项目中的数据,快速获取查询结果。
很多开发者希望利用Lightning的特性来开发数据应用,小编将结合示例来介绍Java和Python如何连接访问Lightning进行应用开发(参考时需要替换为您项目所在region的Endpoint及用户认证信息)。
一、Java使用JDBC访问Lightning
示例如下:
import java.sql.*;public class Main { private static Connection connection; public static void main(String[] args) throws SQLException { String url = "jdbc:postgresql://lightning.cn-shanghai.maxcompute.aliyun.com:443/your_project_name?prepareThreshold=0&sslmode=require"; String accessId = ""; String accessKey = " "; String sql = "select * from dual"; try { Connection conn = getCon(url, accessId, accessKey); Statement st = conn.createStatement(); System.out.println("Send Lightning query"); ResultSet rs = st.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)+ "\t"); } System.out.println("End Lightning query"); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static Connection getCon(String lightningsHost, String lightningUser, String lightningPwd) { try { if (connection == null || connection.isClosed()) { try { Class.forName("org.postgresql.Driver").newInstance(); DriverManager.setLoginTimeout(1); connection = DriverManager.getConnection(lightningsHost, lightningUser, lightningPwd); } catch (Exception ex) { ex.printStackTrace(); } } } catch (Exception ex) { ex.printStackTrace(); } return connection; }}
二、Java使用druid访问Lightning
1.pom依赖
com.alibaba druid 1.0.23 org.postgresql postgresql 9.3-1101-jdbc4
2.spring配置
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(); Listrows = 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
示例如下:
#!/usr/bin/env python# coding=utf-8import psycopg2import sysdef query_lightning(lightning_conf, sql): """Query data through Lightning by sql Args: lightning_conf: a map contains settings of 'dbname', 'user', 'password', 'host', 'port' sql: query submit to Lightning Returns: result: the query result in format of list of rows """ result = None conn = None conn_str = None try: conn_str = ("dbname={dbname} " "user={user} " "password={password} " "host={host} " "port={port}").format(**lightning_conf) except Exception, e: print >> sys.stderr, ("Invalid Lightning' configuration " "{}".format(e)) sys.exit(1) try: conn = psycopg2.connect(conn_str) conn.set_session(autocommit=True) # This will disable transaction # started with keyword BEGIN, # which is currently not # supported by Lightning' public service cur = conn.cursor() # execute Lightning' query cur.execute(sql) # get result result = cur.fetchall() except Exception, e: print >> sys.stderr, ("Failed to query data through " "Lightning: {}".format(e)) finally: if conn: conn.close() return resultif __name__ == "__main__": # step1. setup configuration lightning_conf = { "dbname": "your_project_name", "user": "", "password": " ", "host": "lightning.cn-shanghai.maxcompute.aliyun.com", #your region lightning endpoint "port": 443 } # step2. issue a query result = query_lightning(lightning_conf, "select * from test") # step3. print result if result: for i in xrange(0, len(result)): print "Got %d row from Lightning:%s" % (i + 1, result[i])
四、Python使用ODBC访问Lightning
您需要现在电脑上安装并和配置odbc驱动。代码示例如下:
import pyodbcconn_str = ( "DRIVER={PostgreSQL Unicode};" "DATABASE=your_project_name;" "UID=your_maxcompute_access_id;" "PWD=your_maxcompute_access_key;" "SERVER=lightning.cn-shanghai.maxcompute.aliyun.com;" #your region lightning endpoint "PORT=443;" )conn = pyodbc.connect(conn_str)crsr = conn.execute("SELECT * from test")row = crsr.fetchone()print(row)crsr.close()conn.close()
由于Lightning提供了PostgreSQL兼容的接口,您可以像开发PostgreSQL的应用一样开发Lightning应用程序。
关于怎样使用应用程序访问MaxCompute Lightning进行数据开发就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
开发
应用
数据
示例
应用程序
程序
代码
项目
查询
内容
文章
更多
知识
篇文章
参考
处理
配置
不错
交互式
产品
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
计算机软件开发及销售
虎符网络安全赛道报名
加强节日期间网络安全防护
中国海关贸易数据库
数据库日志存储查询结果
网络安全宣传语录
宇轩网络技术有限公司产品
桐城网络安全宣传周
香港服务器哪里好
如何利用web服务器工具及规则
怎么找三国战争服务器
云浮配方管理软件开发
江苏大数据软件开发价格
用友t3系统怎么删除数据库
中国网络安全公共服务平台
怎么查找已建好的数据库
金蝶kis服务器不小心升级了
qt一般用什么数据库
中联云服务器
福州康乐网络技术有限公司
销售软件开发成本
虚拟机装上服务器
边缘服务器芯片
服务器认证的安全性
昌平区制造软件开发推广
旧的笔记本怎么做成服务器
软件开发电商平台
云浮配方管理软件开发
求每门课的平均成绩数据库
销售软件开发合同