This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculate_power(x, n): | |
| """calculate the power | |
| Args: | |
| x (integer): integr value | |
| n (integer): integer value | |
| Returns: | |
| _type_: power of x to the n | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def string_int(text): | |
| """_summary_ | |
| Args: | |
| text (string): a number enclosed in " or ' quotes as string | |
| Returns: | |
| integer: cast the string to number and return | |
| """ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if you would like to know how tage works, un commete these | |
| # list(range(5)) #0, 1, 2, 3, 4 | |
| # list(range(2, 8)) #2, 3, 4, 5, 6, 7 | |
| # list(range(2, 8, 2)) #2, 4, 6 | |
| firstdf = spark.range(100) | |
| firstdf.count() | |
| firstdf.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from multiprocessing.pool import ThreadPool | |
| import time | |
| ## Change the database name of your choice | |
| databse_name = 'your_database_name' | |
| #create a list of all tables to generate metrics | |
| table_list = spark.sql(f"show tables in {databse_name}").select('database','tablename').collect() | |
| print(table_list) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from multiprocessing.pool import ThreadPool | |
| table_list = ['customers','orders','adress','payments','shipment','sellers','products','tax','fraud','revenue'] | |
| print(table_list) | |
| def create_table_loop(table_name): | |
| spark.sql(f"create table loop_db.{table_name}(id int, name string)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from multiprocessing.pool import ThreadPool | |
| table_list = ['customers','orders','adress','payments','shipment','sellers','products','tax','fraud','revenue'] | |
| print(table_list) | |
| def create_table_fanout(table_name): | |
| spark.sql(f"create table fanout_db.{table_name}(id int, name string)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| table_list = ['customers','orders','adress','payments','shipment','sellers','products','tax','fraud','revenue'] | |
| def create_table_loop(table_name): | |
| spark.sql(f"create table loop_db.{table_name}(id int, name string)") | |
| loop_start_time = time.time() | |
| for table in table_list: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def positive_negative(input_list): | |
| # Write your code here | |
| zeros = input_list.count(0) | |
| total = len(input_list) | |
| if zeros == 0: | |
| input_list.append(0) | |
| input_list.sort() | |
| neg_list = input_list[:input_list.index(0)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import http | |
| import json | |
| def call_publicapi(table_name): | |
| """ | |
| purpose: | |
| This function does bla bla bla. | |
| params: reads 3 global parameters | |
| status, execution_log: for sttaus and loogging | |
| database name: for creating database | |
| response: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Creating an empty DF (This is kind of an Hack...) | |
| tbl_df = spark.sql("show tables in user_data like 'xxx'") | |
| #Loop through all databases | |
| for db in spark.sql("show databases like '*data'").collect(): | |
| #create a dataframe with list of tables from the database | |
| df = spark.sql(f"show tables in {db.databaseName}") | |
| #union the tables list dataframe with main dataframe | |
| tbl_df = tbl_df.union(df) | |
| #After the loop, show the results |