Skip to content

Commit

Permalink
New examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgeorge committed Mar 15, 2015
1 parent 7280c1b commit 5705c0b
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ch03/src/main/java/client/CellComparatorExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client;

// cc CellComparatorExample Shows how to use the cell scanner
import java.util.ArrayList;
import java.util.Collections;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellComparator;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class CellComparatorExample {

public static void main(String[] args) throws Exception {
// vv CellComparatorExample
Put put1 = new Put(Bytes.toBytes("row-1"));
put1.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-1"),
Bytes.toBytes("val-1"));
Put put2 = new Put(Bytes.toBytes("row-2"));
put2.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-2"),
Bytes.toBytes("val-2"));
Put put3 = new Put(Bytes.toBytes("row-3"));
put3.addColumn(Bytes.toBytes("fam-2"), Bytes.toBytes("qual-3"),
Bytes.toBytes("val-3"));
Put put4 = new Put(Bytes.toBytes("row-1"));
put4.addColumn(Bytes.toBytes("fam-2"), Bytes.toBytes("qual-2"),
Bytes.toBytes("val-2"));

CellComparator comparator = new CellComparator.RowComparator();
ArrayList<Cell> cells = new ArrayList<>();

Put[] puts = { put1, put2, put3, put4 };

for (Put put : puts) {
CellScanner scanner = put.cellScanner();
while (scanner.advance()) {
Cell cell = scanner.current();
cells.add(cell);
}
}

System.out.println("Shuffling...");
Collections.shuffle(cells);
for (Cell cell : cells) {
System.out.println("Cell: " + cell);
}

System.out.println("Sorting...");
Collections.sort(cells, comparator);
for (Cell cell : cells) {
System.out.println("Cell: " + cell);
}
// ^^ CellComparatorExample
}
}
28 changes: 28 additions & 0 deletions ch03/src/main/java/client/CellScannerExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client;

// cc CellScannerExample Shows how to use the cell scanner
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class CellScannerExample {

public static void main(String[] args) throws Exception {
// vv CellScannerExample
Put put = new Put(Bytes.toBytes("testrow"));
put.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-1"),
Bytes.toBytes("val-1"));
put.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-2"),
Bytes.toBytes("val-2"));
put.addColumn(Bytes.toBytes("fam-2"), Bytes.toBytes("qual-3"),
Bytes.toBytes("val-3"));

CellScanner scanner = put.cellScanner();
while (scanner.advance()) {
Cell cell = scanner.current();
System.out.println("Cell: " + cell);
}
// ^^ CellScannerExample
}
}
34 changes: 34 additions & 0 deletions ch03/src/main/java/client/FingerprintExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package client;

// cc FingerprintExample Shows what the fingerprint and ID of a data class comprises
import java.net.InetAddress;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class FingerprintExample {

public static void main(String[] args) throws Exception {
// vv FingerprintExample
Put put = new Put(Bytes.toBytes("testrow"));
put.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-1"),
Bytes.toBytes("val-1"));
put.addColumn(Bytes.toBytes("fam-1"), Bytes.toBytes("qual-2"),
Bytes.toBytes("val-2"));
put.addColumn(Bytes.toBytes("fam-2"), Bytes.toBytes("qual-3"),
Bytes.toBytes("val-3"));

String id = String.format("Hostname: %s, App: %s",
InetAddress.getLocalHost().getHostName(),
System.getProperty("sun.java.command"));
put.setId(id);

System.out.println("Put.size: " + put.size());
System.out.println("Put.id: " + put.getId());
System.out.println("Put.fingerprint: " + put.getFingerprint());
System.out.println("Put.toMap: " + put.toMap());
System.out.println("Put.toJSON: " + put.toJSON());
System.out.println("Put.toString: " + put.toString());
// ^^ FingerprintExample
}
}
26 changes: 26 additions & 0 deletions ch03/src/main/java/client/RowKeyExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client;

// cc RowKeyExample Example row key usage from existing array
import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class RowKeyExample {

public static void main(String[] args) {
// vv RowKeyExample
byte[] data = new byte[100];
Arrays.fill(data, (byte) '@');
String username = "johndoe";
byte[] username_bytes = username.getBytes(Charset.forName("UTF8"));

System.arraycopy(username_bytes, 0, data, 45, username_bytes.length);
System.out.println("data length: " + data.length + ", data: " + Bytes.toString(data));

Put put = new Put(data, 45, username_bytes.length);
System.out.println("Put: " + put);
// ^^ RowKeyExample
}
}

0 comments on commit 5705c0b

Please sign in to comment.