Skip to main content

A Module to enable Hepsiburada Data Science Team to utilize different tools.

Project description

Hepsiburada Data Science Utilities

This module includes utilities for Hepsiburada Data Science Team.

  • Library is available via PyPi.
  • Library can be downloaded using pip as follows: pip install heps-ds-utils
  • Existing library can be upgraded using pip as follows: pip install heps-ds-utils --upgrade

Available Modules

  1. Hive Operations
from heps_ds_utils import HiveOperations

# A connection is needed to be generated in a specific runtime.
# There are 3 ways to set credentials for connection.

# 1) Instance try to set default credentials from Environment Variables.
hive_ds = HiveOperations()
hive_ds.connect_to_hive()

# 2) One can pass credentials to instance initiation to override default.
hive_ds = HiveOperations(HIVE_HOST="XXX", HIVE_PORT="YYY", HIVE_USER="ZZZ", HIVE_PASS="WWW", HADOOP_EDGE_HOST="QQQ")
hive_ds = HiveOperations(HIVE_USER="ZZZ", HIVE_PASS="WWW")
hive_ds.connect_to_hive()

# 3) One can change any of the credentials after initiation using appropriate attribute.
hive_ds = HiveOperations()
hive_ds.hive_username = 'XXX'
hive_ds.hive_password = 'YYY'
hive_ds.connect_to_hive()

# Execute an SQL query to retrieve data.
# Currently Implemented Types: DataFrame, Numpy Array, Dictionary, List.
SQL_QUERY = "SELECT * FROM {db}.{table}"
data, columns = hive_ds.execute_query(SQL_QUERY, return_type="dataframe", return_columns=False)

# Execute an SQL query to create and insert data into table.
SQL_QUERY = "INSERT INTO .."
hive_ds.create_insert_table(SQL_QUERY)

# Send Files to Hive and Create a Table with the Data.
# Currently DataFrame or Numpy Array can be sent to Hive.
# While sending Numpy Array columns have to be provided.
SQL_QUERY = "INSERT INTO .."
hive_ds.send_files_to_hive("{db}.{table}", data, columns=None)

# Close the connection at the end of the runtime.

hive_ds.disconnect_from_hive()
  1. BigQuery Operations
from heps_ds_utils import BigQueryOperations, execute_from_bq_file

# A connection is needed to be generated in a specific runtime.
# There are 3 ways to set credentials for connection.

# 1) Instance try to set default credentials from Environment Variables.
bq_ds = BigQueryOperations()

# 2) One can pass credentials to instance initiation to override default.
bq_ds = BigQueryOperations(gcp_key_path="/tmp/keys/ds_qos.json")

# Unlike HiveOperations, initiation creates a direct connection. Absence of
# credentials will throw an error.

# Execute an SQL query to retrieve data.
# Currently Implemented Types: DataFrame.
QUERY_STRING = """SELECT * FROM `[project_name].[dataset_name].[table_name]` LIMIT 20"""
data = bq_ds.execute_query(QUERY_STRING, return_type='dataframe')

# Create a Dataset in BigQuery.
bq_ds.create_new_dataset("example_dataset")

# Create a Table under a Dataset in BigQuery.
schema = [
    {"field_name": "id", "field_type": "INTEGER", "field_mode": "REQUIRED"},
    {"field_name": "first_name", "field_type": "STRING", "field_mode": "REQUIRED"},
    {"field_name": "last_name", "field_type": "STRING", "field_mode": "REQUIRED"},
    {"field_name": "email", "field_type": "STRING", "field_mode": "REQUIRED"},
    {"field_name": "gender", "field_type": "STRING", "field_mode": "REQUIRED"},
    {"field_name": "ip_address", "field_type": "STRING", "field_mode": "REQUIRED"}]

bq_ds.create_new_table(dataset='example_dataset', table_name='mock_data', schema=schema)

# Insert into an existing Table from Dataframe.
# Don't create and insert in the same runtime.
# Google throws an error when creation and insertion time is close.
bq_ds.insert_rows_into_existing_table(dataset='example_dataset', table='mock_data', data=df)

# Delete a Table.
bq_ds.delete_existing_table('example_dataset', 'mock_data')

# Delete a Dataset.
# Trying to delete a dataset consisting of tables will throw an error.
bq_ds.delete_existing_dataset('example_dataset')

# Load Dataframe As a Table. BigQuery will infer the data types.
bq_ds.load_data_to_table('example_dataset', 'mock_data', df, overwrite=False)

# To execute BQ commands sequentially from a BigQuery Script without a return statement !
execute_from_bq_file(bq_client=bq_ds, bq_file_path="tests/test_data/test_case_2.bq", verbose=True)
  1. Logging Operations
from heps_ds_utils import LoggingOperations

# A connection is needed to be generated in a specific runtime.
# There are 3 ways to set credentials for connection.

# 1) Instance try to set default credentials from Environment Variables.
logger_ds = LoggingOperations()

# 2) One can pass credentials to instance initiation to override default.
logger_ds = LoggingOperations(gcp_key_path="/tmp/keys/ds_qos.json")

# Unlike HiveOperations, initiation creates a direct connection. Absence of
# credentials will throw an error.

Release Notes:

0.4.4:

  • BigQueryOperations:
    • insert_rows_into_existing_table: insertion exception handling added.
    • insert_rows_into_existing_table: retry added.
      • Put time between table creation and insertion.
    • execute_query: total_bytes_processed info added.
    • execute_query: max allowed total_bytes_processed set to 100GB.
    • execute_query: return_type=None for Queries w/o any return.
    • load_data_to_table: kwargs['overwrite'] is added.
      • load_data_to_table(..., overwrite=True) to overwrite to table.
      • load_data_to_table(..., overwrite=False) to append to table.
      • not passing overwrite kwarg will print a DeprecationWarning.
    • execute_from_bq_file: sequential execution of BigQuery commands from a file. It has its own parser.
      • execute_from_bq_file(..., verbose=True) to print BigQuery commands to console.
      • execute_from_bq_file(..., verbose=False) not to print BigQuery commands to console.

0.4.5:

  • LoggingOperations
    • Bug Fix in Authentication to GCP Logging !
  • BigQueryOperations
    • Executing BQ files for different environments !

0.4.6:

  • BigQueryOperations
    • BQ Parser bug fix !
    • BQ File Execution dependent queries
      • Some of the queries depends on the previous command executions. For these cases: dependent_queries is needed to be set to True ! execute_from_bq_file( bq_ds, "tests/test_data/test_case_4.bq", verbose=True, config=configs, dependent_queries=True)
    • BQ Create Table Results in Empty Table Check Added!
      • Raises an error if CREATE TABLE ... SELECT AS ... query results in empty table.
      • This doesn't work in the case of dependent_queries=True !!!
    • 100GB limit is turned into a warning, which will not be displayed in prod env.
  • LoggingOperations
    • protobuf dependency issue resolved!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heps_ds_utils-0.4.6a4.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heps_ds_utils-0.4.6a4-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file heps_ds_utils-0.4.6a4.tar.gz.

File metadata

  • Download URL: heps_ds_utils-0.4.6a4.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.9 Darwin/21.2.0

File hashes

Hashes for heps_ds_utils-0.4.6a4.tar.gz
Algorithm Hash digest
SHA256 773a5ca751e7e4030723d5be816bc9ae30749cfd7e767c495329e6d79262b070
MD5 06cf13bddf784bb189ef8619c389f846
BLAKE2b-256 2da6bf21423c68a4bc19a7d1107b1f232ac5e7d5fb73a3479d162e5cdd74227d

See more details on using hashes here.

File details

Details for the file heps_ds_utils-0.4.6a4-py3-none-any.whl.

File metadata

  • Download URL: heps_ds_utils-0.4.6a4-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.9 Darwin/21.2.0

File hashes

Hashes for heps_ds_utils-0.4.6a4-py3-none-any.whl
Algorithm Hash digest
SHA256 021fe043f0be1c15577660d344f017b54b59dfbdb310cb5f004e58a0033e1421
MD5 05f64926ca4018c67fe0e7d24301b968
BLAKE2b-256 c40468e5f7422767aedda8e3587809688f9e8ca615ffe3e4d1904d930307b15b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page