Fluo Tour: Read and Write Data

Tour page 4 of 26

The following example shows basic code for writing data using Fluo and then reading it back out. To run this code, modify src/main/java/ft/Main.java in the Fluo Tour git repository and run it by following the instructions in the repository.

  private static void exercises(MiniFluo mini, FluoClient client) {
    String row = "kerbalnaut0001";
    Column fName = new Column("name", "first");
    Column lName = new Column("name", "last");

    try (Transaction tx = client.newTransaction()) {
      tx.set(row, fName, "Jebediah");
      tx.set(row, lName, "Kerman");
      tx.commit();
    }

    try (Snapshot snapshot = client.newSnapshot()) {
      System.out.println("First name : " + snapshot.gets(row, fName));
      System.out.println("Last name  : " + snapshot.gets(row, lName));
    }
  }

In the example above, a Transaction is created in a try-with-resources block. A Transaction can read and write data. It can only read data that was committed before it started. Data set on a transaction will only be written when commit() is called.

A Snapshot is created to read the data previously written by the Transaction. Snapshots only see data committed before the snapshot was created. The code calls gets(CharSequence, Column) which is a convenience method that works with strings. Internally Fluo only deals with bytes and its get(Bytes, Column) method returns Bytes. The Fluo methods that take and return Strings assume UTF-8 encoding when converting to bytes.

Transactions and snapshots allocate resources and therefore have close() methods. The try-with-resources block will automatically call close(), even when exceptions occur.

< 4 / 26 >