Simulation de Streaming sur SSPCloud
1 Objectif
- un dossier source contient 280 fichiers
tweets-xxx.json
- un dossier destination servira d’entrée pour le TP
- créer un programme qui copie toutes les 2 secondes un fichier depuis source vers destination
- cela simule un flux continu de données
- quand tous les fichiers sont copiés, le dossier destination est vidé et ça recommence
Connectez-vous au SSPCloud
2 Créer un dossier destination
4 Lancer le programme de copie
import time
import boto3
<<< COLLER VOTRE CONFIG DE CONNEXION A S3 ICI >>>
= "ludo2ne"
bucket_source = "diffusion/ensai/stream_tweet"
source_folder
<<< REMPLACER LE BUCKET ET LE FOLDER DESTINATION >>>
= "sspcloud_idep"
bucket_destination = "destination"
destination_folder
print("Number of files : ")
while True:
= s3.list_objects_v2(Bucket=bucket_source, Prefix=source_folder)
response = [obj["Key"] for obj in response["Contents"] if obj["Key"].endswith(".jsonl.gz")]
json_files
for file in json_files:
= s3.list_objects_v2(Bucket=bucket_destination, Prefix=destination_folder)
response_dest = [obj["Key"] for obj in response_dest["Contents"] if obj["Key"].endswith(".jsonl.gz")]
json_dest_files print(f"{len(json_dest_files)}", end=", ")
if len(json_dest_files) % 20 == 0:
print()
= file.split("/")[-1]
file_name = f"{destination_folder}/{file_name}"
destination_object
={'Bucket': bucket_source, 'Key': file},
s3.copy_object(CopySource=bucket_destination,
Bucket=destination_object)
Key#print(f"File '{file_name}' copied from '{source_folder}' to '{destination_folder}'")
2)
time.sleep(
# Delete each JSON file from the bucket
= s3.list_objects_v2(Bucket=bucket_destination, Prefix=destination_folder)
response_dest = [obj["Key"] for obj in response_dest["Contents"] if obj["Key"].endswith(".jsonl.gz")]
json_dest_files for json_file in json_dest_files:
=bucket_destination, Key=json_file) s3.delete_object(Bucket
- en cas de soucis, il suffit d’interrompte le kernel
- il est également possible de supprimer puis recréer le dossier destination
3 Comment se connecter à votre S3