Fluo Tour: Read and Write Data

Tour page 4 of 28

The following shows Java code for writing and reading data using Fluo. In your local clone, modify the exercise(MiniFluo, FluoClient) function in src/main/java/ft/Main.java. Then run ft.Main as previously mentioned.

  private static void exercise(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 / 28 >