Advertisement
Guest User

Untitled

a guest
Apr 6th, 2022
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. files = [
  2.     white_wine,
  3.     red_wine,
  4.     rose_wine,
  5.     sparkling_wine
  6. ]
  7.  
  8. names = [
  9.     "white_wine",
  10.     "red_wine",
  11.     "rose_wine",
  12.     "sparkling_wine"
  13. ]
  14.  
  15. def process_csv(dataframes, categories):
  16.  
  17.     new_dataframes = []
  18.  
  19.     for dataframe, category in zip(dataframes, categories):
  20.  
  21.         category = category.split("_")[0].title()
  22.  
  23.         new_dataframe = dataframe.groupBy('Country', 'Region', 'Winery') \
  24.                                  .avg("Rating", "Price") \
  25.                                  .sort("Country")
  26.        
  27.         new_dataframe = new_dataframe.withColumn("index", lit((1 + monotonically_increasing_id()).cast("string")))
  28.  
  29.         new_dataframe = new_dataframe.withColumn("id", lit(uuid.uuid1().hex))
  30.  
  31.         new_dataframes.append(new_dataframe)
  32.  
  33.     for dataframe, category in zip(new_dataframes, categories):
  34.  
  35.         dfJson = dataframe.toPandas()
  36.        
  37.         dfJson.to_json(f'{category}.json', orient="records", force_ascii=False, lines=False)
  38.  
  39. def send_files_to_cosmos(file_path: str, database_name: str, container_name: str, cosmos_connection_string: str) -> dict:
  40.  
  41.     cosmos_client = CosmosClient.from_connection_string(cosmos_connection_string)
  42.  
  43.     with open(f"{file_path}", 'r', encoding="UTF-8") as file:
  44.         file_as_json = json.load(file)
  45.  
  46.     file_as_dict = {}
  47.  
  48.     for dicts in file_as_json:
  49.         file_as_dict[dicts["id"]] = dicts
  50.  
  51.     for index in range(1, len(file_as_dict)):
  52.         cosmos_client.get_database_client(database_name).get_container_client(container_name).create_item((file_as_dict[str(index)]))
  53.  
  54. for name in names:
  55.     send_files_to_cosmos(
  56.         file_path=name,
  57.         database_name=DATABASE_NAME,
  58.         container_name=CONTAINER_NAME,
  59.         cosmos_connection_string=COSMOS_CONNECTION_STRING
  60.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement