【问题标题】:Want to create python API and integrated with swagger/postman想要创建 python API 并与 swagger/postman 集成
【发布时间】:2023-01-20 16:26:29
【问题描述】:

要求: 1. 我想创建 python API,这将有助于在大查询表中插入数据,并且该 API 将托管在 swagger/postman 中,用户可以从那里提供输入数据,以便它反映在大查询表中。

谁能帮我找到合适的代码解决方案

将 sqlite3 导入为 sql 从 google.cloud 导入 bigquery 从 google.oauth2 导入 service_account credentials = service_account.Credentials.from_service_account_file('path/to/file.json')

project_id = 'project_id' 客户端= bigquery.Client(凭证=凭证,项目=项目ID) def add_data(组名,用户名):
尝试: # 连接数据库 con = sql.connect('shot_database.db') # 获取游标 c = con.cursor() # 添加数据 job_config.use_legacy_sql = True query_job = client.query(""" 插入table_name(组、用户) VALUES (%s, %s)""",job_config = job_config)

results = query_job.result() # Wait for the job to complete.
# Applying changes
con.commit() 

除了: print("发生错误")

【问题讨论】:

    标签: python performance rest flask google-bigquery


    【解决方案1】:

    您提供的代码混合了SQLite大查询,但它喜欢你正在尝试使用大查询将数据插入表中。要使用 Python 将数据插入 BigQuery 表,您可以使用插入数据()客户端类的方法。我在这里添加了一个示例,说明如何使用此方法将数据插入名为“我的表”在一个名为“我的数据集”:

    # Define the data you want to insert
    data = [
        {
            "group": group_name,
            "user": user_name
        }
    ]
    
    # Insert the data
    table_id = "mydataset.mytable"
    errors = client.insert_data(table_id, data)
    
    if errors == []:
        print("Data inserted successfully")
    else:
        print("Errors occurred while inserting data:")
        print(json.dumps(errors, indent=2))
    

    然后,您可以使用创建 API烧瓶或者姜戈并致电添加数据您定义的将数据插入大查询表的方法。

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 2018-01-21
      • 2019-03-28
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多