forked from larsgeorge/hbase-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7280c1b
commit 5705c0b
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |