Data integrity refers to the quality, consistency, and reliability of data throughout its life cycle. Data engineering pipelines are methods and structures that collect, transform, store, and analyse data from many sources.
If you are working as a PySpark developer, data engineer, data analyst, or data scientist for any organisation requires you to be familiar with dataframes because data manipulation is the act of transforming, cleansing, and organising raw data into a format that can be used for analysis and decision making.
For example, you have some user’s data in dataframe-1, and you have to new users’ data in a dataframe-2, then you must find out all the matched records from dataframe-2 and dataframe-1. In PySpark, you can retrieve matching rows from two Dataframes using the join operation. The join operation combines rows from two Dataframes based on a common column.
# importing sparksession from
from pyspark.sql import SparkSession
# pyspark.sql module
from pyspark.sql.functions import col
# Create a Spark session and giving an app name
spark = SparkSession.builder.appName("UpdateMutliColumns").getOrCreate()
Dataset 1: In this dataset, we have three columns such as Name, Age and Occupation and have a pre-defined schema for our PySpark dataframe as given below —
# Sample data for DataFrame1
dataset1 = [("Ryan Arjun", 25, "Engineer"),
("Kimmy Wang", 30, "Data Scientist"),
("Saurabh Yadav", 22, "Analyst")]
# Define the schema for DataFrame1
ds_schema1 = ["Name", "Age", "Occupation"]
PySpark Dataframe 1 from dataset 1 — In PySpark, we are going to call already existing pre-defined createDataFrame function which takes two parameters such as data and schema and passing the above dataset1 and ds_schema1 as given below-
# Create DataFrames
df1 = spark.createDataFrame(dataset1, schema=ds_schema1)
### show the schema of the dataframe
df1.printSchema()
# Show the original DataFrames
print("DataFrame 1:")
df1.show()
# Sample data for DataFrame2
dataset2 = [("Ryan Arjun", "Male", "Indian"),
("Kimmy Wang", "Female", "Japan"),
("Lovish Singh", "Male", "China")]
# Define the schema for DataFrame2
ds_schema2 = ["Name", "Gender", "Country"]
PySpark
Dataframe 2 from dataset 2 —
In PySpark, we are going to call already existing pre-defined createDataFrame function
which takes two parameters such as data and schema and passing the above
dataset1 and ds_schema2 as given below-
# Create DataFrames for second dataset
df2 = spark.createDataFrame(dataset2, schema=ds_schema2)
### show the schema of the dataframe
df2.printSchema()
# Show the original DataFrames
print("DataFrame 2:")
df2.show()
# Join DataFrames based on the "Name" columnjoined_df = df1.join(df2, "Name", "inner")
### show the schema of the dataframe
joined_df.printSchema()
# Show the original DataFrames
print("DataFrame with Matching rows:")
joined_df.show()
Note: You can adjust the join type (inner, left, right, full)
based on your specific requirements. Additionally, if the column names are
different in the two Dataframes, you can specify the join condition explicitly
using the on parameter. You can adjust the join condition based on
your specific use case and column names.
Now, you can see that it is just piece of cake to get the
matching records from both dataframe based on your matching keys.
⌛Lets learn
more on the data validation side which is the most important part of the data
engineering.
Data
validation — Data
validation is the process of checking the data against predefined rules and
standards, such as data types, formats, ranges, and constraints.
- 💫Schema
Validation: Verify data
adherence to predefined schemas, checking types, formats, and structures.
- 💫Integrity
Constraints: Enforce
rules and constraints to maintain data integrity, preventing inconsistencies.
- 💫Cross-Field
Validation: Validate
relationships and dependencies between different fields to ensure logical
coherence.
- 💫Data
Quality Metrics: Define and
track quality metrics, such as completeness, accuracy, and consistency.
- 💫Automated
Validation Scripts: Develop
and run automated scripts to check data against predefined rules and criteria.
To
learn more, please follow us -
🔊 http://www.sql-datatools.com
To
Learn more, please visit our YouTube channel at —
🔊 http://www.youtube.com/c/Sql-datatools
To
Learn more, please visit our Instagram account at -
🔊 https://www.instagram.com/asp.mukesh/
To
Learn more, please visit our twitter account at -
🔊 https://twitter.com/macxima