= spark.read.csv("file.csv", header=True)
df_csv df_csv.show()
JSON
Batch Data Ingestion
Here are some batch data ingestion methods in Spark.
File Based Ingestion
File-based ingestion refers to the process of importing data from various file formats into Apache Spark.
CSV
= spark.read.json("file.json")
df_json df_json.show()
Parquet
= spark.read.parquet("file.parquet")
df_parquet df_parquet.show()
cXML
= spark.read.format("com.databricks.spark.xml").option("rowTag", "record").load("file.xml")
df_xml df_xml.show()
Replication & ETL
You can leverage database replication and ETL processes to facilitate continuous data ingestion and transformation.
Database Replication
# Assuming df_db is the DataFrame from the replicated database
"append").saveAsTable("database_table") df_db.write.mode(
ETL Process
# Assuming df_raw is the raw data DataFrame
= df_raw.select("col1", "col2").withColumn("new_col", expr("col1 + col2"))
df_transformed "append").saveAsTable("transformed_table") df_transformed.write.mode(
Import from API
Integrating external APIs seamlessly allows you to efficiently retrieve and ingest data.
HTTP API
import requests
= requests.get("https://api.example.com/data")
response = response.json()
json_data = spark.read.json(spark.sparkContext.parallelize([json_data]))
df_api df_api.show()
Data Transfer Protocols
Data transfer protocols like File Transfer Protocol (FTP), Secure File Transfer Protocol (SFTP), and Hypertext Transfer Protocol (HTTP) are essential for efficient and secure data transfer.
FTP Ingestion
format("com.springml.spark.sftp").option("host", "ftp.example.com").load("/path/to/file.csv") spark.read.
HTTP Ingestion
"http://example.com/data.txt") spark.read.text(
Source Assessment
You must evaluate data source characteristics and quality, as they are fundamental for effective data integration.
Characteristics Assessment
df_source.describe().show()
Quality Assessment
"count(distinct *) as unique_records").show() df_source.selectExpr(
Schema Mapping
Mapping and transforming data schemas can be a challenge for you, but at the same time, they are essential for fitting data into the target model.
Schema Mapping
= df_raw.selectExpr("col1 as new_col1", "col2 as new_col2") df_mapped
Schema Transformation
= df_mapped.withColumn("new_col3", expr("new_col1 + new_col2")) df_transformed
Data Validation
You must ensure high data quality during ingestion, as it is critical for downstream processes.
Data Validation
= df_raw.filter("col1 IS NOT NULL AND col2 > 0") df_validated
Data Cleansing
= df_raw.na.fill(0, ["col1"]) df_cleansed
Data deduplication
You must prevent duplicate records during ingestion, as it is vital to maintain data integrity downstream.
Remove Duplicates
= df_raw.dropDuplicates(["col1", "col2"]) df_deduplicated
Unstructured Data
Unstructured data includes documents, images, logs, and more. Techniques for ingesting and extracting insights are crucial.
NLP
# Using Spark NLP library for text data processing
= spark.read.text("text_file.txt") df_text
Governance & Compliance
You should ensure data governance practices and compliance with regulatory requirements and data privacy laws, as these are essential for responsible data handling.
Compliance Checks
# Example: Ensure GDPR compliance
= df_raw.filter("country IN ('EU')") df_gdpr_compliant