Table creation
Table must be created before saving data. Table has columns more than one. If you create a table, neptune creates empty tablet and assign to TabletServer.
Table name must start with the alphabet and a-z, A-Z, 0-9, '_' are avaliable.
You can create table with NTable class. Following code is simple
example and you can find complete source code in neptune examples
pacakge.
NConfiguration nconf = new NConfiguration();
private void createTable() throws IOException {
String tableName = "SampleTable";
//Column names
String[] columns = new String[]{"Column1", "Column2"};
String description = "This is test table";
//TableSchema object.
TableSchema tableSchema = new TableSchema(tableName, description, columns);
//set number of version, 0: unlimited, default:3
tableSchema.setNumOfVersion(0);
//check table exists.
boolean exists = NTable.existsTable(nconf, tableName);
if(exists) {
System.out.println("Already exists table");
return;
}
//create table
NTable.createTable(nconf, tableSchema);
}
private void dropTable() throws IOException {
NTable.dropTable(nconf, "SampleTable");
}
Data saving
You can put/get into/from neptune's table with NTable class. NTable class has put, get method.
RowFilter, CellFilter is used to specify a more complex search condition.
private void putRow() throws IOException {
String tableName = "SampleTable";
//get NTable object
NTable ntable = NTable.openTable(nconf, tableName);
//create rowkey object
Row.Key rowKey = new Row.Key("RowKey1");
//create row object
Row row = new Row(rowKey);
//add Cell to row
row.addCell("Column1", new Cell(new Cell.Key("CK1"), "TestData1".getBytes()));
row.addCell("Column2", new Cell(Cell.Key.EMPTY_KEY, "TestData2".getBytes()));
//put method stores data into Tablet
ntable.put(row);
//put another row
row = new Row(new Row.Key("RowKey2"));
row.addCell("Column1", new Cell(new Cell.Key("CK1"), "TestData3".getBytes()));
ntable.put(row);
}
Data retreving
Neptune has two kinds of data retreving API. Frist is online data retreving. For online operation like web
application you can use get() method in NTable. get() method requires
Row.Key parameter. so If you want to retreive from table for
real-time operation, you must know Row.Key. Neptune has like and between operation for online data retreving. Like,
between operation return Row array but maxinum number of Cell.Value is
5000. Like operation's Row.Key parameter must have at least 4 bytes.
Second is scanning. Scanner is high speed data retreving tool. You can
scan full or range data fastly with scanner. Scanner is used
InputFormat for Hadoop MapReduce Job.
Online data retreving
private void getRow() throws IOException {
String tableName = "SampleTable";
NTable ntable = NTable.openTable(nconf, tableName);
Row row = ntable.get(new Row.Key("RK1"));
List| column1Cells = row.getCellList("Column1");
for(Cell eachCell: column1Cells) {
System.out.println(eachCell.getValue().getValueAsString());
}
//get multiple versoin in a single row-cell
//already 100 versions stored in putRow() method. show FirstApp in examples code
RowFilter rowFilter = new RowFilter(new Row.Key("RowKey10"));
CellFilter cellFilter = new CellFilter("Column1");
cellFilter.setStartTimestamp(Long.MIN_VALUE);
cellFilter.setEndTimestamp(Long.MAX_VALUE);
rowFilter.addCellFilter(cellFilter);
row = ntable.get(rowFilter);
Cell cell = row.getCellList("Column1").get(0);
for(Cell.Value eachValue: cell.getValues()) {
System.out.println(eachValue.getValueAsString());
}
//like operation, where rowkey = 'RowKey%'
RowFilter likeRowFilter = new RowFilter(new Row.Key("RowKey"), RowFilter.OP_LIKE);
likeRowFilter.addCellFilter(new CellFilter("Column1"));
rowFilter.addCellFilter(cellFilter);
Row[] rows = ntable.gets(likeRowFilter);
if(rows == null || rows.length == 0) {
System.out.println("No data");
} else {
for(Row eachRow: rows) {
cell = eachRow.getCellList("Column1").get(0);
for(Cell.Value eachValue: cell.getValues()) {
System.out.println(eachValue.getValueAsString());
}
}
}
} |
Scanning
Scanner is optimized batch processing.
private void scanning() throws IOException {
NTable ntable = NTable.openTable(nconf, tableName);
TableScanner scanner = null;
try {
scanner = ScannerFactory.openScanner(ntable, ntable.getColumnsArray());
System.out.println("---------- scan by ScanCell -----------");
ScanCell scanCell = null;
while( (scanCell = scanner.next()) != null ) {
System.out.println(scanCell.toString());
}
} finally {
//must close
if(scanner != null) {
scanner.close();
}
}
scanner = null;
try {
scanner = ScannerFactory.openScanner(ntable, ntable.getColumnsArray());
System.out.println("---------- scan by Row -----------");
Row row = null;
while( (row = scanner.nextRow()) != null ) {
row.print("Column1");
}
} finally {
//must close
if(scanner != null) {
scanner.close();
}
}
}