【问题标题】:How to get the column names in redshift using Python boto3如何使用 Python boto3 在 redshift 中获取列名
【发布时间】:2021-02-03 21:27:57
【问题描述】:

我想使用 python boto3 获取 redshift 中的列名

  1. 已创建 Redshift 集群
  2. 向其中插入数据
  3. 配置的 Secrets Manager
  4. 配置 SageMaker 笔记本

打开Jupyter Notebook写下如下代码

import boto3
import time    
client = boto3.client('redshift-data')    
response = client.execute_statement(ClusterIdentifier = "test", Database= "dev", SecretArn= "{SECRET-ARN}",Sql= "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='dev' AND `TABLE_NAME`='dojoredshift'")

我收到了响应,但里面没有表架构

下面是我用来连接的代码,我超时了

import psycopg2
HOST = 'xx.xx.xx.xx'
PORT = 5439
USER = 'aswuser'
PASSWORD = 'Password1!'
DATABASE = 'dev'
def db_connection():
    conn = psycopg2.connect(host=HOST,port=PORT,user=USER,password=PASSWORD,database=DATABASE)
    return conn

如何获取ip地址去https://ipinfo.info/html/ip_checker.php

传递您的 redshiftcluster 主机名xx.xx.us-east-1.redshift.amazonaws.com 或者您可以在集群页面本身中看到

我在运行上面的代码时遇到了错误

OperationalError:无法连接到服务器:连接超时 服务器是否在主机“x.xx.xx..xx”上运行并接受 端口 5439 上的 TCP/IP 连接?

【问题讨论】:

标签: python amazon-redshift boto3


【解决方案1】:

我修正了代码,并添加了上面的规则

import boto3
import psycopg2
 
# Credentials can be set using different methodologies. For this test,
# I ran from my local machine which I used cli command "aws configure"
# to set my Access key and secret access key
 
client = boto3.client(service_name='redshift',
                      region_name='us-east-1')
#
#Using boto3 to get the Database password instead of hardcoding it in the code
#
cluster_creds = client.get_cluster_credentials(
                         DbUser='awsuser',
                         DbName='dev',
                         ClusterIdentifier='redshift-cluster-1',
                         AutoCreate=False)
 
try:
    # Database connection below that uses the DbPassword that boto3 returned
    conn = psycopg2.connect(
                host = 'redshift-cluster-1.cvlywrhztirh.us-east-1.redshift.amazonaws.com',
                port = '5439',
                user = cluster_creds['DbUser'],
                password = cluster_creds['DbPassword'],
                database = 'dev'
                )
    # Verifies that the connection worked
    cursor = conn.cursor()
    cursor.execute("SELECT VERSION()")
    results = cursor.fetchone()
    ver = results[0]
    if (ver is None):
        print("Could not find version")
    else:
        print("The version is " + ver)
 
except:
    logger.exception('Failed to open database connection.')
    print("Failed")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多