Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.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
Amazon web services aws glue/pyspark-如何使用glue以编程方式创建Athena表_Amazon Web Services_Amazon S3_Pyspark_Amazon Athena_Aws Glue - Fatal编程技术网

Amazon web services aws glue/pyspark-如何使用glue以编程方式创建Athena表

Amazon web services aws glue/pyspark-如何使用glue以编程方式创建Athena表,amazon-web-services,amazon-s3,pyspark,amazon-athena,aws-glue,Amazon Web Services,Amazon S3,Pyspark,Amazon Athena,Aws Glue,我正在AwsGlue中运行一个脚本,它从s3加载数据,进行一些转换,并将结果保存到s3。我正试图在这个程序中增加一个步骤。我想在Athena的现有数据库中创建一个新表 我在AWS文档中找不到任何类似的例子。在我遇到的示例中,结果被记录到S3。 用胶水可以吗 下面是一些代码示例。如何修改它以创建带有输出结果的Athena表 import sys from awsglue.transforms import * from awsglue.utils import getResolvedOptions

我正在AwsGlue中运行一个脚本,它从s3加载数据,进行一些转换,并将结果保存到s3。我正试图在这个程序中增加一个步骤。我想在Athena的现有数据库中创建一个新表

我在AWS文档中找不到任何类似的例子。在我遇到的示例中,结果被记录到S3。 用胶水可以吗

下面是一些代码示例。如何修改它以创建带有输出结果的Athena表

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from awsglue.context import GlueContext
from awsglue.job import Job
from awsglue.dynamicframe import DynamicFrame

from pyspark.sql import SparkSession
from pyspark.context import SparkContext
from pyspark.sql.functions import *
from pyspark.sql import SQLContext
from pyspark.sql.types import *


args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)


datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "dataset", table_name = "table_1", transformation_ctx = "datasource0")
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("id", "long", "id", "long"), ("description", "string", "description", "string")], transformation_ctx = "applymapping1")
resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_struct", transformation_ctx = "resolvechoice2")
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
datasink4 = glueContext.write_dynamic_frame.from_options(frame = dropnullfields3, connection_type = "s3", connection_options = {"path": "s3://..."}, format = "parquet", transformation_ctx = "datasink4")


*create Athena table with the output results*

job.commit()

我可以想出两种方法。一种是使用sdk获取对athena API的引用,并使用它执行带有create table语句的查询

另一种可能更有趣的方法是使用Glue API为S3 bucket创建一个新的应用程序,然后执行爬虫程序


使用第二种方法,您的表格被编目,您不仅可以使用雅典娜、EMR或红移光谱

@Javier Ramirez,在单独的py脚本中使用boto3创建一个表将是一个解决方案,但是我想知道这个操作是否可以直接在glue上进行。aws文档中提到了一个函数create table,但没有示例,我不清楚这是否可以用于脚本中的glue,如果可以,那么函数应该是什么样子的。您可以始终在同一个脚本中创建表,不需要其他脚本。正如您所指出的,您还可以使用Glue API来创建表。您指向的是web API文档,它可以通过boto3访问(与我提到的解决方案非常类似),您可以在中看到完整的示例。(在下一篇评论中有更多内容)有另一个版本的API直接在Glue上记录(所以不涉及boto3)。这方面没有例子,但由于参数与web上的参数相同,您可能可以将其用作灵感。希望能有帮助