Sunday, May 31, 2026
banner
Top Selling Multipurpose WP Theme

Environmental setting

This information makes use of JupyterLab with Docker and MinIO. Consider Docker as a great tool that simplifies working purposes, and MinIO as a versatile storage resolution excellent for processing giant quantities of various kinds of knowledge. Here is tips on how to set it up:

I will not go into element about each step right here, as there are already good tutorials for that. I like to recommend checking it first after which coming again and persevering with with this.

As soon as the whole lot is prepared, begin by getting ready the pattern knowledge. Begin by opening a brand new Jupyter pocket book.

To begin with, it’s worthwhile to set up s3fs Python package deal. Important for working with MinIO in Python.

!pip set up s3fs

Then import the required dependencies and modules.

import os
import s3fs
import pyspark
from pyspark.sql import SparkSession
from pyspark import SparkContext
import pyspark.sql.capabilities as F
from pyspark.sql import Row
import pyspark.sql.sorts as T
import datetime
import time

We additionally set some atmosphere variables which can be helpful when interacting with MinIO.

# Outline atmosphere variables
os.environ["MINIO_KEY"] = "minio"
os.environ["MINIO_SECRET"] = "minio123"
os.environ["MINIO_ENDPOINT"] = "http://minio1:9000"

Subsequent, arrange a Spark session with the required settings.

# Create Spark session
spark = SparkSession.builder
.appName("big_data_file_formats")
.config("spark.jars.packages", "org.apache.hadoop:hadoop-aws:3.3.4,com.amazonaws:aws-java-sdk-bundle:1.11.1026,org.apache.spark:spark-avro_2.12:3.5.0,io.delta:delta-spark_2.12:3.0.0")
.config("spark.hadoop.fs.s3a.endpoint", os.environ["MINIO_ENDPOINT"])
.config("spark.hadoop.fs.s3a.entry.key", os.environ["MINIO_KEY"])
.config("spark.hadoop.fs.s3a.secret.key", os.environ["MINIO_SECRET"])
.config("spark.hadoop.fs.s3a.path.model.entry", "true")
.config("spark.hadoop.fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension")
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog")
.enableHiveSupport()
.getOrCreate()

Let’s simplify this for higher understanding.

  • spark.jars.packages: Save the required JAR information Maven repository. The Maven repository is a central location used to retailer construct artifacts comparable to JAR information, libraries, and different dependencies utilized in Maven-based initiatives.
  • spark.hadoop.fs.s3a.endpoint: That is the MinIO endpoint URL.
  • spark.hadoop.fs.s3a.entry.key and spark.hadoop.fs.s3a.secret.key: MinIO entry key and secret key. Observe that this is similar username and password used to entry the MinIO internet interface.
  • spark.hadoop.fs.s3a.path.model.entry: Set to true to allow path-style entry to the MinIO bucket.
  • spark.hadoop.fs.s3a.impl: Implementation class for the S3A file system.
  • spark.sql.extensions: Register Delta Lake SQL instructions and configurations throughout the Spark SQL parser.
  • spark.sql.catalog.spark_catalog: Set your Spark catalog to be a catalog in Delta Lake in order that desk administration and metadata operations will be dealt with by Delta Lake.

You will need to select the right JAR model to keep away from errors. Utilizing the identical Docker picture, the JAR model described right here ought to work fantastic. If you happen to encounter any setup points, please be at liberty to depart a remark. I’ll do my greatest that can assist you 🙂

The subsequent step is to create a big Spark dataframe. There are 10 million rows, divided into 10 columns. Half are textual content and half are numbers.

# Generate pattern knowledge
num_rows = 10000000
df = spark.vary(0, num_rows)

# Add columns
for i in vary(1, 10): # Since we have already got one column
if i % 2 == 0:
# Integer column
df = df.withColumn(f"int_col_{i}", (F.randn() * 100).solid(T.IntegerType()))
else:
# String column
df = df.withColumn(f"str_col_{i}", (F.rand() * num_rows).solid(T.IntegerType()).solid("string"))

df.rely()

Let’s take a peek on the first few entries and see what they’re all about.

# Present rows from pattern knowledge
df.present(10,truncate = False)

+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|id |str_col_1|int_col_2|str_col_3|int_col_4|str_col_5|int_col_6|str_col_7|int_col_8|str_col_9|
+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|0 |7764018 |128 |1632029 |-15 |5858297 |114 |1025493 |-88 |7376083 |
|1 |2618524 |118 |912383 |235 |6684042 |-115 |9882176 |170 |3220749 |
|2 |6351000 |75 |3515510 |26 |2605886 |89 |3217428 |87 |4045983 |
|3 |4346827 |-70 |2627979 |-23 |9543505 |69 |2421674 |-141 |7049734 |
|4 |9458796 |-106 |6374672 |-142 |5550170 |25 |4842269 |-97 |5265771 |
|5 |9203992 |23 |4818602 |42 |530044 |28 |5560538 |-75 |2307858 |
|6 |8900698 |-130 |2735238 |-135 |1308929 |22 |3279458 |-22 |3412851 |
|7 |6876605 |-35 |6690534 |-41 |273737 |-178 |8789689 |88 |4200849 |
|8 |3274838 |-42 |1270841 |-62 |4592242 |133 |4665549 |-125 |3993964 |
|9 |4904488 |206 |2176042 |58 |1388630 |-63 |9364695 |78 |2657371 |
+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+
solely exhibiting high 10 rows

To know the construction of a knowledge body, use: df.printSchema() Verify the kind of knowledge included. After this, we are going to create 4 her CSV information. These are used for Parquet, Avro, ORC, and Delta Lake. That is accomplished to keep away from bias in efficiency testing. Utilizing the identical CSV permits Spark to cache and optimize it within the background.

# Write 4 CSVs for evaluating efficiency for each file kind
df.write.csv("s3a://mybucket/ten_million_parquet.csv")
df.write.csv("s3a://mybucket/ten_million_avro.csv")
df.write.csv("s3a://mybucket/ten_million_orc.csv")
df.write.csv("s3a://mybucket/ten_million_delta.csv")

Now create 4 separate dataframes from these CSVs. Every helps completely different file codecs.

# Learn all 4 CSVs to create dataframes
schema = T.StructType([
T.StructField("id", T.LongType(), nullable=False),
T.StructField("str_col_1", T.StringType(), nullable=True),
T.StructField("int_col_2", T.IntegerType(), nullable=True),
T.StructField("str_col_3", T.StringType(), nullable=True),
T.StructField("int_col_4", T.IntegerType(), nullable=True),
T.StructField("str_col_5", T.StringType(), nullable=True),
T.StructField("int_col_6", T.IntegerType(), nullable=True),
T.StructField("str_col_7", T.StringType(), nullable=True),
T.StructField("int_col_8", T.IntegerType(), nullable=True),
T.StructField("str_col_9", T.StringType(), nullable=True)
])

df_csv_parquet = spark.learn.format("csv").choice("header",True).schema(schema).load("s3a://mybucket/ten_million_parquet.csv")
df_csv_avro = spark.learn.format("csv").choice("header",True).schema(schema).load("s3a://mybucket/ten_million_avro.csv")
df_csv_orc = spark.learn.format("csv").choice("header",True).schema(schema).load("s3a://mybucket/ten_million_orc.csv")
df_csv_delta = spark.learn.format("csv").choice("header",True).schema(schema).load("s3a://mybucket/ten_million_delta.csv")

that is all! Now you are able to discover these huge knowledge file codecs.

parquet work

Parquet is a column-oriented file format that works very properly with Apache Spark, making it the only option for processing huge knowledge. That is helpful in analytical situations, particularly when sifting knowledge by columns.

One in every of its nice options is the flexibility to retailer knowledge in a compressed format. crisp compression It is a dependable choice. This not solely saves area but additionally improves efficiency.

One other wonderful thing about Parquet is its versatile strategy to knowledge schemas. Begin with a primary construction and scale easily by including columns as your wants develop. This adaptability makes it extraordinarily straightforward to make use of for evolving knowledge initiatives.

Now that you just perceive Parquet, let’s try it out. Let’s write 10 million data to a Parquet file and monitor the time it takes. as a substitute of utilizing %timeit Python capabilities are executed a number of occasions and will be resource-intensive for large knowledge duties, so we solely measure them as soon as.

# Write knowledge as Parquet
start_time = time.time()
df_csv_parquet.write.parquet("s3a://mybucket/ten_million_parquet2.parquet")
end_time = time.time()
print(f"Time taken to put in writing as Parquet: {end_time - start_time} seconds")

For me this job took a very long time 15.14 seconds, Nonetheless, please word that this time might fluctuate relying in your pc. For instance, it took longer on a much less highly effective PC. So don’t be concerned if the occasions are completely different. The important thing right here is to check the efficiency of various file codecs.

Subsequent, run an combination question in opposition to the Parquet knowledge.

# Perfom aggregation question utilizing Parquet knowledge
start_time = time.time()
df_parquet = spark.learn.parquet("s3a://mybucket/ten_million_parquet2.parquet")
df_parquet
.choose("str_col_5","str_col_7","int_col_2")
.groupBy("str_col_5","str_col_7")
.rely()
.orderBy("rely")
.restrict(1)
.present(truncate = False)
end_time = time.time()
print(f"Time taken for question: {end_time - start_time} seconds")

+---------+---------+-----+
|str_col_5|str_col_7|rely|
+---------+---------+-----+
|1 |6429997 |1 |
+---------+---------+-----+

This question resulted in 12.33 seconds. Now let’s change gears and try the ORC file format.

Cooperation with ORC

One other columnar candidate, the ORC file format, will not be as well-known as Parquet, however it has its personal benefits. One in every of its distinguishing options is that it may compress knowledge much more successfully than Parquet, whereas utilizing the identical quick compression algorithm.

It’s standard within the Hive world due to its assist for ACID operations on Hive tables. ORC is personalized to effectively deal with large-scale streaming reads.

Moreover, it’s simply as versatile as Parquet relating to schemas. Begin with a primary construction and add columns as your undertaking grows. This makes ORC a strong selection to your evolving huge knowledge wants.

Let’s take a look at the write efficiency of ORC.

# Write knowledge as ORC
start_time = time.time()
df_csv_orc.write.orc("s3a://mybucket/ten_million_orc2.orc")
end_time = time.time()
print(f"Time taken to put in writing as ORC: {end_time - start_time} seconds")

it took me 12.94 seconds to finish the duty. One other attention-grabbing level is the dimensions of the info written to the MinIO bucket.inside ten_million_orc2.orc Contained in the folder, you will see a number of partition information, every of a sure dimension. The contents of every partition ORC file are as follows: 22.3 MiBthere are 16 information in whole.

ORC partition file (picture by creator)

Evaluating this to Parquet, every partition file in Parquet is 26.8 MiB, which can be a complete of 16 information. This reveals that ORC really offers higher compression than Parquet.

Subsequent, take a look at how ORC handles combination queries. We use the identical question for all file codecs to take care of a good benchmark.

# Carry out aggregation utilizing ORC knowledge
df_orc = spark.learn.orc("s3a://mybucket/ten_million_orc2.orc")
start_time = time.time()
df_orc
.choose("str_col_5","str_col_7","int_col_2")
.groupBy("str_col_5","str_col_7")
.rely()
.orderBy("rely")
.restrict(1)
.present(truncate = False)
end_time = time.time()
print(f"Time taken for question: {end_time - start_time} seconds")

+---------+---------+-----+
|str_col_5|str_col_7|rely|
+---------+---------+-----+
|1 |2906292 |1 |
+---------+---------+-----+

ORC question completed in 13.44 seconds, a little bit longer than Parquet’s time. Now that we have checked ORC off the listing, let’s transfer on to experimenting with Avro.

Cooperation with Avro

Avro is a line-based file format with distinctive benefits. It does not compress knowledge as effectively as Parquet or ORC, however it makes up for it with sooner write speeds.

What actually units Avro aside is its glorious schema evolution capabilities. It might probably simply deal with modifications comparable to including, eradicating, or modifying fields, making it perfect for situations the place knowledge constructions evolve over time.

Avro is particularly fitted to workloads that contain writing giant quantities of knowledge.

Now let’s examine how Avro writes knowledge.

# Write knowledge as Avro
start_time = time.time()
df_csv_avro.write.format("avro").save("s3a://mybucket/ten_million_avro2.avro")
end_time = time.time()
print(f"Time taken to put in writing as Avro: {end_time - start_time} seconds")

it took me 12.81 seconds, which is definitely sooner than each Parquet and ORC. Subsequent, let’s take a look at Avro’s efficiency with combination queries.

# Carry out aggregation utilizing Avro knowledge
df_avro = spark.learn.format("avro").load("s3a://mybucket/ten_million_avro2.avro")
start_time = time.time()
df_avro
.choose("str_col_5","str_col_7","int_col_2")
.groupBy("str_col_5","str_col_7")
.rely()
.orderBy("rely")
.restrict(1)
.present(truncate = False)
end_time = time.time()
print(f"Time taken for question: {end_time - start_time} seconds")

+---------+---------+-----+
|str_col_5|str_col_7|rely|
+---------+---------+-----+
|1 |6429997 |1 |
+---------+---------+-----+

This question took about 15.42 seconds. So relating to queries, Parquet and ORC are higher when it comes to velocity. Now, let’s check out the final and latest file format: Delta Lake.

delta lake operations

Delta Lake is a rising star on the earth of huge knowledge file codecs and is intently associated to Parquet when it comes to storage dimension. Just like Parquet, however with some extra options.

Delta Lake takes a little bit longer than Parquet when writing knowledge. That is primarily because of the following causes. _delta_log Folders are key to superior performance. These options embody ACID compliance for dependable transactions, time journey for accessing historic knowledge, small file compression for group, and extra.

Delta Lake is a newcomer to the large knowledge scene, however it shortly grew to become standard on cloud platforms working Spark, surpassing its use on on-premises techniques.

Let’s begin with knowledge write assessments and transfer on to testing Delta Lake efficiency.

# Write knowledge as Delta
start_time = time.time()
df_csv_delta.write.format("delta").save("s3a://mybucket/ten_million_delta2.delta")
end_time = time.time()
print(f"Time taken to put in writing as Delta Lake: {end_time - start_time} seconds")

Time taken for write operation 17.78 seconds, which is a little bit longer than different file codecs we have thought of thus far. Factors to notice are: ten_million_delta2.delta If it is in a folder, every partition file is definitely a Parquet file and has the identical dimension as we noticed in Parquet. furthermore, _delta_log folder.

Write knowledge as Delta Lake (picture by creator)

of _delta_log Delta Lake file format folders play an essential function in how Delta Lake manages and maintains knowledge integrity and model management. It is a key part that distinguishes Delta Lake from different huge knowledge file codecs. Here is a fast breakdown of its options:

  1. transaction log: of _delta_log The folder comprises a transaction log that data all modifications made to the info within the delta desk. This log is a sequence of JSON information detailing knowledge additions, deletions, and modifications. It acts like a complete diary of all knowledge transactions.
  2. ACID compliance: This log allows ACID (Atomicity, Consistency, Isolation, Sturdiness) compliance. All transactions in Delta Lake, comparable to writing new knowledge or modifying present knowledge, are atomic and constant, making certain knowledge integrity and reliability.
  3. Time journey and auditing: Transaction logs enable for “time journey”. This implies you possibly can simply view and restore earlier variations of your knowledge. That is extraordinarily helpful for knowledge restoration, auditing, and understanding how your knowledge has advanced over time.
  4. Schema enforcement and evolution: of _delta_log It additionally tracks the schema (construction) of your knowledge. You’ll be able to implement schemas throughout knowledge writes and safely evolve schemas over time with out knowledge corruption.
  5. Concurrency and merge operations: Manages concurrent reads and writes, permitting a number of customers to entry and modify knowledge on the identical time with out conflicts. This makes it perfect for complicated operations comparable to merges, updates, and deletes.

In abstract, _delta_log Folders are the brains of Delta Lake’s superior knowledge administration capabilities, offering strong transaction logging, model management, and reliability enhancements not usually out there with easy file codecs like Parquet or ORC.

Now let’s check out how Delta Lake works with combination queries.

# Carry out aggregation utilizing Delta knowledge
df_delta = spark.learn.format("delta").load("s3a://mybucket/ten_million_delta2.delta")
start_time = time.time()
df_delta
.choose("str_col_5","str_col_7","int_col_2")
.groupBy("str_col_5","str_col_7")
.rely()
.orderBy("rely")
.restrict(1)
.present(truncate = False)
end_time = time.time()
print(f"Time taken for question: {end_time - start_time} seconds")

+---------+---------+-----+
|str_col_5|str_col_7|rely|
+---------+---------+-----+
|1 |2906292 |1 |
+---------+---------+-----+

This question completed in about 3 minutes 15.51 seconds. It is a little slower than Parquet or ORC, however it’s fairly shut. This means that Delta Lake’s efficiency in real-world situations is similar to Parquet’s.

fantastic! All experiments have been accomplished. Allow us to summarize our findings within the subsequent part.

Which file format to make use of and when?

Now that the testing is full, let’s summarize all the outcomes. In knowledge writing, Avro took the highest spot. That is what it does greatest in real-life situations.

Parquet is by far the most effective relating to studying and executing combination queries. Nonetheless, this doesn’t imply that ORC and Delta Lake are inadequate. As a columnar file format, it performs properly in most conditions.

Efficiency comparability (offered by the creator)

Here is a fast overview:

  • Select ORC for optimum compression, particularly in case you are utilizing Hive and Pig for evaluation duties.
  • Are you utilizing Spark? Parquet and Delta Lake are really helpful.
  • Avro is good for situations the place giant quantities of knowledge are written, comparable to touchdown zone areas.

This concludes this tutorial.

banner
Top Selling Multipurpose WP Theme

Converter

Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner
Top Selling Multipurpose WP Theme

Leave a Comment

banner
Top Selling Multipurpose WP Theme

Latest

Best selling

22000,00 $
16000,00 $
6500,00 $
5999,00 $

Top rated

6500,00 $
22000,00 $
900000,00 $

Products

Knowledge Unleashed
Knowledge Unleashed

Welcome to Ivugangingo!

At Ivugangingo, we're passionate about delivering insightful content that empowers and informs our readers across a spectrum of crucial topics. Whether you're delving into the world of insurance, navigating the complexities of cryptocurrency, or seeking wellness tips in health and fitness, we've got you covered.