A GSGStreamsWriteTable
The examples in this document rely on a
Users
table that is populated with data. The application we used to create and populate this table is provided in this appendix.
Note:
While this example does not use namespaces, the streaming API supports them. To assess a table in a namespace, such as ns1, prefix the table name with the namespace, followed by a colon. For example:ns1:Users
.
We provide this class without comment and solely for completeness. The actions taken by this class should be familiar to anyone who has used the Oracle NoSQL Database Java API. See Java Direct Driver Developer's Guide.
package pubsub;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import oracle.kv.FaultException;
import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.StatementResult;
import oracle.kv.table.PrimaryKey;
import oracle.kv.table.Row;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
public class GSGStreamsWriteTable {
private static final String[] hhosts = {"localhost:5000"};
private static final int MAX_ROWS = 200;
public static void main(String args[]) {
GSGStreamsWriteTable gswt = new GSGStreamsWriteTable();
gswt.run(args);
System.out.println("All done.");
}
private void run(String args[]) {
KVStoreConfig kconfig = new KVStoreConfig("kvstore", hhosts);
KVStore kvstore = KVStoreFactory.getStore(kconfig);
defineTable(kvstore);
writeTable(kvstore);
}
private void defineTable(KVStore kvstore) {
System.out.println("Creating table schema....");
TableAPI tableAPI = kvstore.getTableAPI();
StatementResult result = null;
String statement = null;
try {
statement = "DROP TABLE IF EXISTS Users";
result = kvstore.executeSync(statement);
displayResult(result, statement);
statement = "CREATE TABLE Users ( " +
" uid INTEGER, " +
" myJSON JSON, " +
" PRIMARY KEY(uid))";
result = kvstore.executeSync(statement);
displayResult(result, statement);
} catch (IllegalArgumentException e) {
System.out.println("Invalid statement:\n" +
e.getMessage());
} catch (FaultException e) {
System.out.println
("Statement couldn't be executed, please retry: " + e);
}
}
private void writeTable(KVStore kvstore) {
System.out.println("In writeTable....");
TableAPI tableH = kvstore.getTableAPI();
Table myTable = tableH.getTable("Users");
int count = 0;
Random rand = new Random();
/*
* Write rows to the table, using random information
* for the JSON data.
*/
while (count < MAX_ROWS) {
Row row = myTable.createRow();
row.put("uid", count);
int q = rand.nextInt(10) + 1;
List<Integer> integersList = new ArrayList<Integer>();
int a_count = 0;
while (a_count < q) {
int val = rand.nextInt(q + 10) + 1;
integersList.add(val);
a_count++;
}
String json = "{";
json += "\"quantity\" : " + q + ", ";
json += "\"myArray\" : " + integersList.toString();
json += "}";
/* Write the row to the store */
row.putJson("myJSON", json);
tableH.put(row, null, null);
/* Randomly delete table rows */
int shouldDelete = rand.nextInt(10);
if (shouldDelete == 1) {
/* Randomly select a row to delete */
int toDelete = rand.nextInt(count);
PrimaryKey pk = myTable.createPrimaryKey();
pk.put("uid", toDelete);
tableH.delete(pk, null, null);
}
count++;
}
System.out.println("Wrote " + count + " rows");
}
private void displayResult(StatementResult result,
String statement) {
System.out.println("===========================");
if (result.isSuccessful()) {
System.out.println("Statement was successful:\n\t" +
statement);
System.out.println("Results:\n\t" + result.getInfo());
} else if (result.isCancelled()) {
System.out.println("Statement was cancelled:\n\t" +
statement);
} else {
/*
* statement wasn't successful: may be in error, or may
* still be in progress.
*/
if (result.isDone()) {
System.out.println("Statement failed:\n\t" +
statement);
System.out.println("Problem:\n\t" +
result.getErrorMessage());
} else {
System.out.println("Statement in progress:\n\t" +
statement);
System.out.println("Status:\n\t" +
result.getInfo());
}
}
}
}