pro.foundev.GzipCompressionOfColumnExample.java Source code

Java tutorial

Introduction

Here is the source code for pro.foundev.GzipCompressionOfColumnExample.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Ryan Svihla
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package pro.foundev;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.google.common.base.Strings;
import org.apache.commons.codec.binary.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipCompressionOfColumnExample {

    private static final String UTF_8 = "UTF-8";
    private static final Charset charset = Charset.forName(UTF_8);

    public static void main(String[] args) {
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        try {
            String inputText = "";
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                String randomString = String.valueOf(random.nextFloat());
                String paddedRandomString = Strings.padEnd(randomString, 10, 'a');
                inputText += paddedRandomString + "\n";
            }
            Session session = cluster.newSession();
            session.execute("create keyspace if not exists test with replication ="
                    + " { 'class':'SimpleStrategy', 'replication_factor':1 } ");
            session.execute("create table if not exists test.blobs (id int, value blob, primary key(id))");
            ByteBuffer zippedBlob = toByteBuffer(inputText);
            session.execute("insert into test.blobs (id, value) values (1, ?)", zippedBlob);
            Row row = session.execute("select * from test.blobs where id = 1 ").one();

            ByteBuffer fromCassandra = row.getBytes("value");
            String outputText = fromByteBuffer(fromCassandra);

            if (outputText.length() > 0) {
                System.out.println("output text is as follows");
                System.out.println(outputText);
            } else {
                System.out.println("no data in value from database FAILURE!!!");
            }
            if (inputText.equals(outputText)) {
                System.out.println("input text matches output text");
            } else {
                System.exit(188);
                System.out.println("critical error text does not match FAILURE!!!");
            }
        } finally {
            cluster.close();
        }
    }

    //source https://github.com/Netflix/astyanax/blob/master/astyanax-cassandra/src/main/java/com/netflix/astyanax/serializers/GzipStringSerializer.java
    private static ByteBuffer toByteBuffer(String obj) {
        if (obj == null) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(StringUtils.getBytesUtf8(obj));
            gzip.close();
            return ByteBuffer.wrap(out.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException("Error compressing column data", e);
        }
    }

    //source https://github.com/Netflix/astyanax/blob/master/astyanax-cassandra/src/main/java/com/netflix/astyanax/serializers/GzipStringSerializer.java
    private static String fromByteBuffer(ByteBuffer byteBuffer) {
        if (byteBuffer == null) {
            return null;
        }
        GZIPInputStream gzipInputStream = null;
        ByteArrayOutputStream baos = null;
        ByteBuffer dup = ByteBuffer.allocate(byteBuffer.limit());
        dup.clear();
        dup.put(byteBuffer);
        dup.flip();

        try {
            gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(dup.array(), 0, dup.limit()));

            baos = new ByteArrayOutputStream();
            for (int value = 0; value != -1;) {
                value = gzipInputStream.read();
                if (value != -1) {
                    baos.write(value);
                }
            }
            gzipInputStream.close();
            baos.flush();
            baos.close();
            return new String(baos.toByteArray(), charset);
        } catch (IOException e) {
            throw new RuntimeException("Error decompressing column data", e);
        } finally {
            if (gzipInputStream != null) {
                try {
                    gzipInputStream.close();
                } catch (IOException e) {
                }
            }
            if (baos != null) {
                try {
                    baos.flush();
                    baos.close();
                } catch (IOException e) {
                }
            }
        }
    }
}