Fallstudie: Ein Berichts-Repository erstellen

Einführung in AWS Boto mit Python

Maksim Pecherskiy

Data Engineer

Endprodukt

Endprodukt

Einführung in AWS Boto mit Python

Die Schritte

Daten vorbereiten
  • Monatsdateien aus dem Raw-Data-Bucket laden
  • Zu einer CSV zusammenführen
  • Ein aggregiertes DataFrame erzeugen
Einführung in AWS Boto mit Python

Die Schritte

Bericht erstellen
  • DataFrame als CSV und HTML schreiben
  • Bokeh-Plot erzeugen, als HTML speichern
Einführung in AWS Boto mit Python

Die Schritte

Bericht auf teilbare Website hochladen
  • Bucket gid-reports erstellen
  • Alle drei Monatsdateien zu S3 hochladen
  • Eine index.html erzeugen, die alle Dateien auflistet
  • Die Website-URL holen!
Einführung in AWS Boto mit Python

Raw-Data-Bucket

Raw-Data-Bucket

  • Private Dateien
  • Tägliche CSVs mit App-Anfragen
  • Rohdaten

Gid-Requests-Bucket

Einführung in AWS Boto mit Python

Rohdaten-Dateien einlesen

# 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']
Einführung in AWS Boto mit Python

Rohdaten-Dateien einlesen

# 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)
Einführung in AWS Boto mit Python

Rohdaten-Dateien einlesen

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

# Preview the DataFrame df.head()

DataFrame-Vorschau

Einführung in AWS Boto mit Python

Aggregierte Berichte erstellen

  • Etwas aggregieren
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

Aggregierte Berichte

Einführung in AWS Boto mit Python

Report-Bucket

Report-Bucket

  • Bucket-Website
  • Öffentlich zugänglich
  • Aggregierte Daten und HTML-Berichte

Report-Bucket

Einführung in AWS Boto mit Python

Aggregierte CSV hochladen

# 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'})
Einführung in AWS Boto mit Python

HTML-Tabelle hochladen

# 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'})
Einführung in AWS Boto mit Python

HTML-Chart hochladen

# 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'})
Einführung in AWS Boto mit Python

Hochgeladene Berichte

Hochgeladene Berichte

Einführung in AWS Boto mit Python

index.html erstellen

# 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']
Einführung in AWS Boto mit Python

index.html erstellen

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

DF nach HTML schreiben

Einführung in AWS Boto mit Python

index.html hochladen

# 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'
  })
Einführung in AWS Boto mit Python

Die URL der Indexdatei holen!

Bucket-Website-URL *


"http://gid-reports.index.html"
Einführung in AWS Boto mit Python

Lass uns etwas feinjustieren!

Einführung in AWS Boto mit Python

Preparing Video For Download...