Studiu de caz: Generarea unui depozit de rapoarte

Introducere în AWS Boto în Python

Maksim Pecherskiy

Data Engineer

Produs final

Produs final

Introducere în AWS Boto în Python

Pașii

Pregătirea datelor
  • Descărcați fișierele lunii din bucket-ul de date brute
  • Concatenați-le într-un singur CSV
  • Creați un DataFrame agregat
Introducere în AWS Boto în Python

Pașii

Crearea raportului
  • Scrieți DataFrame-ul în CSV și HTML
  • Generați un grafic Bokeh, salvați ca HTML
Introducere în AWS Boto în Python

Pașii

Încărcarea raportului pe un website partajabil
  • Creați bucket-ul gid-reports
  • Încărcați cele trei fișiere ale lunii în S3
  • Generați un fișier index.html cu lista fișierelor
  • Obțineți URL-ul website-ului!
Introducere în AWS Boto în Python

Bucket de date brute

Bucket date brute

  • Fișiere private
  • CSV-uri zilnice cu solicitări din aplicație
  • Date brute

Bucket gid-requests

Introducere în AWS Boto în Python

Citirea fișierelor de date brute

# Create list to hold our DataFrames
df_list = []

# Request the list of csv's from S3 with prefix; Get contents response = s3.list_objects( Bucket='gid-requests', Prefix='2019_jan')
# Get response contents request_files = response['Contents']
Introducere în AWS Boto în Python

Citirea fișierelor de date brute

# Iterate over each object
for file in request_files:
    obj = s3.get_object(Bucket='gid-requests', Key=file['Key'])

# Read it as DataFrame obj_df = pd.read_csv(obj['Body'])
# Append DataFrame to list df_list.append(obj_df)
Introducere în AWS Boto în Python

Citirea fișierelor de date brute

# Concatenate all the DataFrames in the list
df = pd.concat(df_list)

# Preview the DataFrame df.head()

Previzualizare DataFrame

Introducere în AWS Boto în Python

Crearea rapoartelor agregate

  • Efectuați o agregare
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

Rapoarte agregate

Introducere în AWS Boto în Python

Bucket rapoarte

Bucket rapoarte

  • Website bucket
  • Accesibil public
  • Date agregate și rapoarte HTML

Bucket rapoarte

Introducere în AWS Boto în Python

Încărcarea CSV-ului agregat

# Upload Aggregated CSV to S3
s3.upload_file(Filename='./jan_final_report.csv', 
                      Key='2019/jan/final_report.csv', 
                      Bucket='gid-reports',
                      ExtraArgs = {'ACL': 'public-read'})
Introducere în AWS Boto în Python

Încărcarea tabelului HTML

# Upload HTML table to S3
s3.upload_file(Filename='./jan_final_report.html', 
                      Key='2019/jan/final_report.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Introducere în AWS Boto în Python

Încărcarea graficului HTML

# Upload Aggregated Chart to S3
s3.upload_file(Filename='./jan_final_chart.html', 
                      Key='2019/jan/final_chart.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Introducere în AWS Boto în Python

Rapoarte încărcate

Rapoarte încărcate

Introducere în AWS Boto în Python

Crearea index.html

# List the gid-reports bucket objects starting with 2019/
r = s3.list_objects(Bucket='gid-reports', Prefix='2019/')

# Convert the response contents to DataFrame objects_df = pd.DataFrame(r['Contents'])
# Create a column "Link" that contains website url + key base_url = "https://gid-reports." objects_df['Link'] = base_url + objects_df['Key']
Introducere în AWS Boto în Python

Crearea index.html

# Write DataFrame to html
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

Scriere DF în html

Introducere în AWS Boto în Python

Încărcarea index.html

# Upload the file to gid-reports bucket root.
s3.upload_file(
  Filename='./report_listing.html', 
  Key='index.html', 
  Bucket='gid-reports',
  ExtraArgs = {
    'ContentType': 'text/html', 
    'ACL': 'public-read'
  })
Introducere în AWS Boto în Python

Obținerea URL-ului indexului!

URL-ul website-ului bucket *


"http://gid-reports.index.html"
Introducere în AWS Boto în Python

Să ajustăm!

Introducere în AWS Boto în Python

Preparing Video For Download...