Skip to main content

A package to load and preprocess JSON data using PySpark

Project description

PySpark JSON Loader

Overview

pyspark-json-loader is a Python package designed to facilitate loading and preprocessing JSON data using PySpark. It provides functions to start a Spark session, connect to a PostgreSQL database, preprocess data, and convert Spark DataFrames to Pandas DataFrames.

Installation

To install the package, run:

pip install pyspark-json-loader

Usage

Here is a detailed guide on how to use the functions provided by the pyspark-json-loader package.

Importing the Module

To use the functions in this package, you need to import them as follows:

from pyspark_json_loader import load_json_file, start_spark_session, connect_to_db, preprocess_dataframe, convert_to_pandas

Functions

1. load_json_file(file_name)

Description: This function loads a JSON file and returns its contents as a Python dictionary.

Parameters:

  • file_name (str): The path to the JSON file.

Returns:

  • dict: The contents of the JSON file.

Example:

json_data = load_json_file('data.json')
print(json_data)

Output:

{
    "column1": "value1",
    "column2": "value2"
}

2. start_spark_session(jar_file_path)

Description: This function starts a Spark session with the specified JAR file.

Parameters:

  • jar_file_path (str): The path to the PostgreSQL JDBC JAR file.

Returns:

  • SparkSession: The Spark session object.

Example:

spark = start_spark_session('/path/to/postgresql.jar')
print(spark)

Output:

<pyspark.sql.session.SparkSession object at 0x...>

3. connect_to_db(spark, host_name, port_number, db_name, user, password, query, null_value=0)

Description: This function connects to a PostgreSQL database using the provided connection details and query.

Parameters:

  • spark (SparkSession): The Spark session object.
  • host_name (str): The hostname of the PostgreSQL server.
  • port_number (int): The port number of the PostgreSQL server.
  • db_name (str): The name of the database.
  • user (str): The username for the database.
  • password (str): The password for the database.
  • query (str): The SQL query to execute.
  • null_value (int, optional): The value to use for null values. Default is 0.

Returns:

  • DataFrame: The resulting DataFrame from the query.

Example:

df = connect_to_db(spark, 'localhost', 5432, 'mydatabase', 'user', 'password', 'SELECT * FROM mytable')
df.show()

Output:

| id | name |
|----|------|
| 1  | John |
| 2  | Jane |

4. preprocess_dataframe(df, json_data)

Description: This function preprocesses a DataFrame by filling null values based on the provided JSON data.

Parameters:

  • df (DataFrame): The Spark DataFrame to preprocess.
  • json_data (dict): The JSON data to use for preprocessing.

Returns:

  • DataFrame: The preprocessed DataFrame.

Example:

preprocessed_df = preprocess_dataframe(df, json_data)
preprocessed_df.show()

Output:

+---+------+
| id|  name|
+---+------+
|  1|  John|
|  2|  Jane|
+---+------+

5. convert_to_pandas(grouped_metrics_df)

Description: This function converts a Spark DataFrame to a Pandas DataFrame and adds a UUID column.

Parameters:

  • grouped_metrics_df (DataFrame): The Spark DataFrame to convert.

Returns:

  • DataFrame: The resulting Pandas DataFrame.

Example:

pandas_df = convert_to_pandas(preprocessed_df)
print(pandas_df)

Output:

   id   name                               Id
0   1   John  0a539f3c... (UUID)
1   2   Jane  1d2e4f5a... (UUID)

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

pyspark-json-loader-0.1.3.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

pyspark_json_loader-0.1.3-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pyspark-json-loader-0.1.3.tar.gz.

File metadata

  • Download URL: pyspark-json-loader-0.1.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.7

File hashes

Hashes for pyspark-json-loader-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4f8b293730384a3cf5091c32f78187bd506bc8f935946519975fcb5808bea52e
MD5 16911237094e4ac1aa16ff312febd23a
BLAKE2b-256 2d9619662cd39bb171b0fb2e22eaa9d76422d814f933cb4467bff848f471b1f5

See more details on using hashes here.

File details

Details for the file pyspark_json_loader-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pyspark_json_loader-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9b563a3883a47399f729e358b20583167e90b4ef63cc702e63e7010f44452fa8
MD5 e1eabc39a71c38234e29a94e475342fb
BLAKE2b-256 17c5181c86f0fe2e434175a4a3397bcbc75fae6723ad5dbc68f744add658b2de

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