byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB};
ArrayList<Mutation> mutations;
// non-utf8 is fine for data
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid)));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), mutations);
// try empty strings
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:")), ByteBuffer.wrap(bytes(""))));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), mutations);
// this row name is valid utf8
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid)));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations);
// non-utf8 is now allowed in row names because HBase stores values as binary
ByteBuffer bf = ByteBuffer.wrap(invalid);
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid)));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations);
// Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<ByteBuffer>();
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner...");
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
break;
}
printRow(entry);
}
//
// Run some operations on a bunch of rows
//
for (int i = 100; i >= 0; --i) {
// format row keys as "00000" to "00100"
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
byte[] row = bytes(nf.format(i));
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME"))));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row)));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row));
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0"))));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO"))));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row)));
Mutation m = null;
mutations = new ArrayList<Mutation>();
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:foo"));
m.isDelete = true;
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row)));
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i)))));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i)))));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row)));
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations.clear();
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value= ByteBuffer.wrap(bytes("-999"));
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true;
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1); // shouldn't override latest
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row)));