Amazon S3是一种云存储解决方案,提供了可扩展、安全和经济高效的对象存储。 Amazon Athena是一种服务,可让您在Amazon S3上运行分析查询。 然而,不带分区的Amazon Athena查询可能需要很长时间才能运行。 在这种情况下,使用分区的Athena查询将更快。
以下是使用Amazon S3和Amazon Athena进行分区的代码示例:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-test-bucket')
if not bucket.creation_date:
bucket.create(CreateBucketConfiguration={
'LocationConstraint': 'us-west-2'
})
import boto3
import csv
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-test-bucket')
with open('data.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
bucket.put_object(Key=row['id'], Body=row)
CREATE EXTERNAL TABLE my_table (
id bigint,
name string,
...
)
PARTITIONED BY (year int, month int, day int)
ROW FORMAT SERDE ‘org.apache.hadoop.hive.serde2.OpenCSVSerde’
LOCATION ‘s3://my-test-bucket/path/to/data/’;
ALTER TABLE my_table ADD PARTITION (year=2021, month=8, day=1) LOCATION 's3://my-test-bucket/path/to/data/2021/8/1/';
然后,您可以使用分区查询,如下所示:
SELECT *
FROM my_table
WHERE year=2021 AND month=8 AND day=1;
这将更快地查询您