Code Monkey home page Code Monkey logo

amazon-keyspaces-large-object-s3-demo's Introduction

Amazon Keyspaces (for Apache Cassandra) and S3 Codec demo.

Description

Custom S3 Codec supports transparent, user-configurable mapping of UUID pointers to S3 objects.

Practical use cases that justify such a feature:

  1. Your data is larger than 1MB, which exceeds the Amazon Keyspaces row size quota
  2. Your data needs to be accessible via Amazon Keyspaces (the driver side)
  3. You want to reduce storage costs by moving large objects to a different location for storage
  4. You need to access your data infrequently

Prerequisites

    JDK 1.7.0u71 or better
    Maven 3.3.9 or better
    AWS SDK S3 1.11.907 or better
    DataStax java driver 4.9.0 or better
    Authentication Plugin for the DataStax Java Driver 4.0.3 or better

If you want to build everything at once, from the top directory run

    mvn install

Let's create one keyspace and one table

    CREATE KEYSPACE ks WITH replication = {'class':
    'com.amazonaws.cassandra.DefaultReplication'} AND durable_writes = true;
    CREATE TABLE ks.test2 (
       k int PRIMARY KEY,
       v uuid
       );

Usage

You can define S3 Codec in the following way:

    TypeCodec<String> s3Codec = new CqlUuidToTextCodec("your-bucket-name", "keyspace-name", "table-name", your-s3-client)

Once you have your S3 Codec, register it when building your session. The following is an example of how to register S3 Codec.

    CqlSession session = CqlSession.builder()
                    .addContactPoints(contactPoints)
                    .withSslContext(SSLContext.getDefault())
                    .withLocalDatacenter("us-east-1")
                    .withAuthProvider(new SigV4AuthProvider("us-east-1"))
                    // Add our Type Codec to integrate with Amazon S3
                    .addTypeCodecs(s3Codec)
                    .build();

In the above example, the driver will look up S3 codec for CQL String and Java UUID in the codec registry and will transparently pick S3 Codec for that.

You can now use the new mappings in your code:

     PreparedStatement ps = session.prepare("INSERT INTO ks.test2 (k, v) VALUES (?, ?)");
     session.execute(ps.boundStatementBuilder()
                            .setInt("k", i)
                            .set("v", object, s3Codec) // write String object to S3 and persist UUID pointer into ks.test2 
                            .setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)
                            .build());

In the above example, the driver writes your large file into Amazon S3 and inserts an uuid pointer into Amazon Keyspaces.

      PreparedStatement ps1 = session.prepare("SELECT k,v FROM ks.test2 WHERE k = ?");
                ResultSet rs = session.execute(ps1.
                        boundStatementBuilder().
                        setInt("k", i).
                        build());
                String v = rs.one().get("v", s3Codec); // Read S3 Object from Amazon S3
    

In the last example, the driver reads your large file from Amazon S3 by providing the uuid pointer in the CQL statement.

Enjoy! Feedback and PR's welcome!

License

This library is licensed under the MIT-0 License. See the LICENSE file.

amazon-keyspaces-large-object-s3-demo's People

Contributors

dependabot[bot] avatar nwheeler81 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

rathan8

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.