Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在胶水中的现有拼花地板文件上创建表格_Python_Amazon Web Services_Boto3_Amazon Athena - Fatal编程技术网

Python 在胶水中的现有拼花地板文件上创建表格

Python 在胶水中的现有拼花地板文件上创建表格,python,amazon-web-services,boto3,amazon-athena,Python,Amazon Web Services,Boto3,Amazon Athena,我有拼花地板文件在S3上,我想创建一个胶水表。 我正在使用boto3 python API来实现这一点。 代码如下: import boto3 c = boto3.client('glue') c.create_table(DatabaseName='staging', TableInput={ 'Name': 'test_table', 'Stora

我有拼花地板文件在S3上,我想创建一个胶水表。 我正在使用boto3 python API来实现这一点。 代码如下:

import boto3

c = boto3.client('glue')

c.create_table(DatabaseName='staging', 
               TableInput={
                           'Name': 'test_table', 
                           'StorageDescriptor': {
                               'Location': 's3://staging/tables/test_table/version=2020-03-26',             
                           'OutputFormat':'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat', 
                           'InputFormat': 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'},
                           'PartitionKeys': [{'Name': 'country'}, {'Name': 'city'}], 
                           'TableType': 'EXTERNAL_TABLE'})
如果我们查看位置字段,我们可以看到数据的url。 在version=2020-03-26文件夹中,我们有类似于“country=country name”的文件夹,在每个文件夹中,我们还有其他类似于“city=city name”的文件夹,用于统计拼花地板文件

执行此代码将返回:

{'ResponseMetadata': {'HTTPHeaders': {'connection': 'keep-alive',
   'content-length': '2',
   'content-type': 'application/x-amz-json-1.1',
   'date': 'Mon, 06 Apr 2020 08:46:58 GMT',
   'x-amzn-requestid': 'ca5e4af0-a2ec-4af0-a2ec-18c308132e21'},
  'HTTPStatusCode': 200,
  'RequestId': 'ca5e4af0-a10d-a2ec-a13d-453dfsdfsds',
  'RetryAttempts': 0}}
我可以看到glue上的表,但当我试图查询Athena上的表时,我出现了错误:

描述格式化的测试表

我做错了什么?
此外,我刚刚检查了glue,即使创建了表的条目,我也没有从接口返回任何信息。

使用boto3直接使用glue API有时过于冗长,缺少一两个参数可能会导致严重错误。在讨论API细节之前,我建议您先看看

通过几个命令,您可以将数据读入dataframe,然后创建具有首选结构的表:

import awswrangler as wr

df = wr.pandas.read_parquet(path='s3://staging/tables/test_table/version=2020-03-26', 
                            columns=['country', 'city', ...], filters=[("c5", "=", 0)])

# Typical Pandas, Numpy or Pyarrow transformation HERE!

wr.pandas.to_parquet(  # Storing the data and metadata to Data Lake
    dataframe=df,
    database='my_database',
    path='s3://production/tables/test_table/version=2020-03-26',
    partition_cols=['country', 'city'],
)
如果您正在使用PySpark并希望在Glue中注册数据帧:

import awswrangler as wr

dataframe.write \
        .mode("overwrite") \
        .format("parquet") \
        .partitionBy(["country", "city"]) \
        .save(compression="gzip", path="s3://production/tables/test_table/version=2020-03-26.")
sess = wr.Session(spark_session=spark)
sess.spark.create_glue_table(
    dataframe=dataframe,
    file_format="parquet",
    partition_by=["country", "city"],
    path="s3://production/tables/test_table/version=2020-03-26",
    compression="gzip",
    database="my_database")

我看了aws牧马人,很有趣。实际上,我要保存的数据帧是pyspark数据帧,我使用pyspark的saveAsTable方法,但我遇到了一些表不希望创建的问题(因为在我的url路径中有一个空格,比如version=2020-03-26 11:02:45.456789),你认为它能与awswrangler一起工作吗?@RobertReynolds,我在上面的答案中添加了将其与PySpark一起使用的示例
import awswrangler as wr

dataframe.write \
        .mode("overwrite") \
        .format("parquet") \
        .partitionBy(["country", "city"]) \
        .save(compression="gzip", path="s3://production/tables/test_table/version=2020-03-26.")
sess = wr.Session(spark_session=spark)
sess.spark.create_glue_table(
    dataframe=dataframe,
    file_format="parquet",
    partition_by=["country", "city"],
    path="s3://production/tables/test_table/version=2020-03-26",
    compression="gzip",
    database="my_database")