【问题标题】:Alternative to nested cursor in AWS RedshiftAWS Redshift 中嵌套游标的替代方案
【发布时间】:2019-08-23 12:47:16
【问题描述】:

我们如何在 aws redshift 中实现嵌套游标?我们可以使用 python 或 spark 来实现该功能吗? 欢迎任何其他替代方案。 我在 aws 产品支持页面上发现了不兼容性。 https://docs.aws.amazon.com/redshift/latest/dg/declare.html

【问题讨论】:

    标签: amazon-redshift


    【解决方案1】:

    目前 AWS Redshift 不支持在会话期间打开多个光标。请从他们的官方cursor declare 文档中找到以下引用:

    您必须在事务块中声明游标。只有一个光标 一次可以在每个会话中打开。

    替代方案:

    几乎您唯一的选择是在您的应用程序层中实现此逻辑。设计某种使用本地主机资源来处理数据的python/脚本。您也可以将该流程卸载到 Redshift,但仍需要在应用层中设计该逻辑。

    以下是如何使用 Python 从 Redshift 读取数据并对其进行循环的示例:

    import psycopg2
    
    #Create connection
    con=psycopg2.connect(dbname= 'dbname', host='host', 
    port= 'port', user= 'user', password= 'pwd')
    
    cur = con.cursor() #Create a cursor from the connection
    
    cur.execute("SELECT * FROM table_name;") #Run the select you want to loop/cursor over
    
    for row in cur:
        #do something with every single row here
        #optionally print the row
        print row
    
    #Clean up cursor and connection by closing
    cur.close() 
    conn.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 2020-08-18
      • 2018-08-21
      • 2021-05-25
      相关资源
      最近更新 更多