DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Getting Started With the YugabyteDB Managed REST API
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Getting Started With the YugabyteDB Managed REST API
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Bulk vs Individual Compression

Bulk vs Individual Compression

In this article, take a look at bulk vs individual compression.

Bozhidar Bozhanov user avatar by
Bozhidar Bozhanov
·
Sep. 10, 20 · Tutorial
Like (2)
Save
Tweet
Share
5.48K Views

Join the DZone community and get the full member experience.

Join For Free

I'd like to share something very brief and very obvious - that compression works better with large amounts of data. That is, if you have to compress 100 sentences you'd better compress them in bulk rather than once sentence at a time. Let me illustrate that:

Java
 




x
13


 
1
public static void main(String[] args) throws Exception {
2
    List<String> sentences = new ArrayList<>();
3
    for (int i = 0; i < 100; i ++) {
4
        StringBuilder sentence = new StringBuilder();
5
        for (int j = 0; j < 100; j ++) { 
6
          sentence.append(RandomStringUtils.randomAlphabetic(10)).append(" "); 
7
        } 
8
        sentences.add(sentence.toString()); 
9
    } 
10
    byte[] compressed = compress(StringUtils.join(sentences, ". ")); 
11
    System.out.println(compressed.length); 
12
    System.out.println(sentences.stream().collect(Collectors.summingInt(sentence -> compress(sentence).length)));
13
}


The compress method is using commons-compress to easily generate results for multiple compression algorithms:

Java
 




xxxxxxxxxx
1
14


 
1
public static byte[] compress(String str) {
2
   if (str == null || str.length() == 0) {
3
       return new byte[0];
4
   }
5
   ByteArrayOutputStream out = new ByteArrayOutputStream();
6
   try (CompressorOutputStream gzip = new CompressorStreamFactory()
7
           .createCompressorOutputStream(CompressorStreamFactory.GZIP, out)) {
8
       gzip.write(str.getBytes("UTF-8"));
9
       gzip.close();
10
       return out.toByteArray();
11
   } catch (Exception ex) {
12
       throw new RuntimeException(ex);
13
   }
14
}


The results are as follows, in bytes (note that there's some randomness, so algorithms are not directly comparable):

Why is that an obvious result? Because of the way most compression algorithms work - they look for patterns in the raw data and create a map of those patterns (a very rough description).

How is that useful? In big data scenarios where the underlying store supports per-record compression (e.g. a database or search engine), you may save a significant amount of disk space if you bundle multiple records into one stored/indexed record.

This is not a generically useful advice, though. You should check the particular datastore implementation. For example MS SQL Server supports both row and page compression. Cassandra does compression on an SSTable level, so it may not matter how you structure your rows. Certainly, if storing data in files, storing it in one file and compressing it is more efficient than compressing multiple files separately.

Disk space is cheap so playing with data bundling and compression may be seen as premature optimization. But in systems that operate on large datasets it's a decision that can save you a lot of storage costs.

Big data

Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Never Use Credentials in a CI/CD Pipeline Again
  • Getting Started With the YugabyteDB Managed REST API
  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: