use of org.apache.hadoop.hbase.thrift.generated.Mutation in project hbase by apache.
the class TestThriftServer method getMutations.
/**
*
* @return a List of Mutations for a row, with columnA having valueA
* and columnB having valueB
*/
private static List<Mutation> getMutations() {
List<Mutation> mutations = new ArrayList<>(2);
mutations.add(new Mutation(false, columnAname, valueAname, true));
mutations.add(new Mutation(false, columnBname, valueBname, true));
return mutations;
}
use of org.apache.hadoop.hbase.thrift.generated.Mutation in project hbase by apache.
the class TestThriftServer method getBatchMutations.
/**
*
* @return a List of BatchMutations with the following effects:
* (rowA, columnA): delete
* (rowA, columnB): place valueC
* (rowB, columnA): place valueC
* (rowB, columnB): place valueD
*/
private static List<BatchMutation> getBatchMutations() {
List<BatchMutation> batchMutations = new ArrayList<>(3);
// Mutations to rowA. You can't mix delete and put anymore.
List<Mutation> rowAmutations = new ArrayList<>(1);
rowAmutations.add(new Mutation(true, columnAname, null, true));
batchMutations.add(new BatchMutation(rowAname, rowAmutations));
rowAmutations = new ArrayList<>(1);
rowAmutations.add(new Mutation(false, columnBname, valueCname, true));
batchMutations.add(new BatchMutation(rowAname, rowAmutations));
// Mutations to rowB
List<Mutation> rowBmutations = new ArrayList<>(2);
rowBmutations.add(new Mutation(false, columnAname, valueCname, true));
rowBmutations.add(new Mutation(false, columnBname, valueDname, true));
batchMutations.add(new BatchMutation(rowBname, rowBmutations));
return batchMutations;
}
use of org.apache.hadoop.hbase.thrift.generated.Mutation in project hbase by apache.
the class TestThriftServer method doTestTableScanners.
/**
* Tests the four different scanner-opening methods (with and without
* a stoprow, with and without a timestamp).
*
* @throws Exception
*/
public void doTestTableScanners() throws Exception {
// Setup
ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
long time1 = System.currentTimeMillis();
handler.mutateRowTs(tableAname, rowAname, getMutations(), time1, null);
// Sleep to assure that 'time1' and 'time2' will be different even with a
// coarse grained system timer.
Thread.sleep(1000);
// Apply timestamped BatchMutations for rowA and rowB
long time2 = System.currentTimeMillis();
handler.mutateRowsTs(tableAname, getBatchMutations(), time2, null);
time1 += 1;
// Test a scanner on all rows and all columns, no timestamp
int scanner1 = handler.scannerOpen(tableAname, rowAname, getColumnList(true, true), null);
TRowResult rowResult1a = handler.scannerGet(scanner1).get(0);
assertEquals(rowResult1a.row, rowAname);
// This used to be '1'. I don't know why when we are asking for two columns
// and when the mutations above would seem to add two columns to the row.
// -- St.Ack 05/12/2009
assertEquals(rowResult1a.columns.size(), 1);
assertEquals(rowResult1a.columns.get(columnBname).value, valueCname);
TRowResult rowResult1b = handler.scannerGet(scanner1).get(0);
assertEquals(rowResult1b.row, rowBname);
assertEquals(rowResult1b.columns.size(), 2);
assertEquals(rowResult1b.columns.get(columnAname).value, valueCname);
assertEquals(rowResult1b.columns.get(columnBname).value, valueDname);
closeScanner(scanner1, handler);
// Test a scanner on all rows and all columns, with timestamp
int scanner2 = handler.scannerOpenTs(tableAname, rowAname, getColumnList(true, true), time1, null);
TRowResult rowResult2a = handler.scannerGet(scanner2).get(0);
assertEquals(rowResult2a.columns.size(), 1);
// column A deleted, does not exist.
//assertTrue(Bytes.equals(rowResult2a.columns.get(columnAname).value, valueAname));
assertEquals(rowResult2a.columns.get(columnBname).value, valueBname);
closeScanner(scanner2, handler);
// Test a scanner on the first row and first column only, no timestamp
int scanner3 = handler.scannerOpenWithStop(tableAname, rowAname, rowBname, getColumnList(true, false), null);
closeScanner(scanner3, handler);
// Test a scanner on the first row and second column only, with timestamp
int scanner4 = handler.scannerOpenWithStopTs(tableAname, rowAname, rowBname, getColumnList(false, true), time1, null);
TRowResult rowResult4a = handler.scannerGet(scanner4).get(0);
assertEquals(rowResult4a.columns.size(), 1);
assertEquals(rowResult4a.columns.get(columnBname).value, valueBname);
// Test scanner using a TScan object once with sortColumns False and once with sortColumns true
TScan scanNoSortColumns = new TScan();
scanNoSortColumns.setStartRow(rowAname);
scanNoSortColumns.setStopRow(rowBname);
int scanner5 = handler.scannerOpenWithScan(tableAname, scanNoSortColumns, null);
TRowResult rowResult5 = handler.scannerGet(scanner5).get(0);
assertEquals(rowResult5.columns.size(), 1);
assertEquals(rowResult5.columns.get(columnBname).value, valueCname);
TScan scanSortColumns = new TScan();
scanSortColumns.setStartRow(rowAname);
scanSortColumns.setStopRow(rowBname);
scanSortColumns = scanSortColumns.setSortColumns(true);
int scanner6 = handler.scannerOpenWithScan(tableAname, scanSortColumns, null);
TRowResult rowResult6 = handler.scannerGet(scanner6).get(0);
assertEquals(rowResult6.sortedColumns.size(), 1);
assertEquals(rowResult6.sortedColumns.get(0).getCell().value, valueCname);
List<Mutation> rowBmutations = new ArrayList<>(20);
for (int i = 0; i < 20; i++) {
rowBmutations.add(new Mutation(false, asByteBuffer("columnA:" + i), valueCname, true));
}
ByteBuffer rowC = asByteBuffer("rowC");
handler.mutateRow(tableAname, rowC, rowBmutations, null);
TScan scanSortMultiColumns = new TScan();
scanSortMultiColumns.setStartRow(rowC);
scanSortMultiColumns = scanSortMultiColumns.setSortColumns(true);
int scanner7 = handler.scannerOpenWithScan(tableAname, scanSortMultiColumns, null);
TRowResult rowResult7 = handler.scannerGet(scanner7).get(0);
ByteBuffer smallerColumn = asByteBuffer("columnA:");
for (int i = 0; i < 20; i++) {
ByteBuffer currentColumn = rowResult7.sortedColumns.get(i).columnName;
assertTrue(Bytes.compareTo(smallerColumn.array(), currentColumn.array()) < 0);
smallerColumn = currentColumn;
}
TScan reversedScan = new TScan();
reversedScan.setReversed(true);
reversedScan.setStartRow(rowBname);
reversedScan.setStopRow(rowAname);
int scanner8 = handler.scannerOpenWithScan(tableAname, reversedScan, null);
List<TRowResult> results = handler.scannerGet(scanner8);
handler.scannerClose(scanner8);
assertEquals(results.size(), 1);
assertEquals(ByteBuffer.wrap(results.get(0).getRow()), rowBname);
// Teardown
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
}
use of org.apache.hadoop.hbase.thrift.generated.Mutation in project hbase by apache.
the class TestThriftServer method doTestCheckAndPut.
/**
* Check that checkAndPut fails if the cell does not exist, then put in the cell, then check that
* the checkAndPut succeeds.
*
* @throws Exception
*/
public static void doTestCheckAndPut() throws Exception {
ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
handler.createTable(tableAname, getColumnDescriptors());
try {
List<Mutation> mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, columnAname, valueAname, true));
Mutation putB = (new Mutation(false, columnBname, valueBname, true));
assertFalse(handler.checkAndPut(tableAname, rowAname, columnAname, valueAname, putB, null));
handler.mutateRow(tableAname, rowAname, mutations, null);
assertTrue(handler.checkAndPut(tableAname, rowAname, columnAname, valueAname, putB, null));
TRowResult rowResult = handler.getRow(tableAname, rowAname, null).get(0);
assertEquals(rowAname, rowResult.row);
assertEquals(valueBname, rowResult.columns.get(columnBname).value);
} finally {
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
}
}
use of org.apache.hadoop.hbase.thrift.generated.Mutation in project hbase by apache.
the class DemoClient method run.
private void run() throws Exception {
TTransport transport = new TSocket(host, port);
if (secure) {
Map<String, String> saslProperties = new HashMap<>();
saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
/**
* The Thrift server the DemoClient is trying to connect to
* must have a matching principal, and support authentication.
*
* The HBase cluster must be secure, allow proxy user.
*/
transport = new TSaslClientTransport("GSSAPI", null, // Thrift server user name, should be an authorized proxy user.
"hbase", // Thrift server domain
host, saslProperties, null, transport);
}
transport.open();
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
byte[] t = bytes("demo_table");
//
// Scan all tables, look for the demo table and delete it.
//
System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
client.disableTable(name);
}
System.out.println(" deleting table: " + utf8(name.array()));
client.deleteTable(name);
}
}
//
// Create the demo table with two column families, entry: and unused:
//
ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
ColumnDescriptor col;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("entry:"));
col.timeToLive = Integer.MAX_VALUE;
col.maxVersions = 10;
columns.add(col);
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("unused:"));
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);
System.out.println("creating table: " + utf8(t));
try {
client.createTable(ByteBuffer.wrap(t), columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}
System.out.println("column families in " + utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false;
//
// Test UTF-8 handling
//
byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
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<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), mutations, dummyAttributes);
// this row name is valid utf8
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
// non-utf8 is now allowed in row names because HBase stores values as binary
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
// Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner...");
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
break;
}
printRow(entry);
}
//
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<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations = new ArrayList<>(2);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
Mutation m;
mutations = new ArrayList<>(2);
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, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
// 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;
// shouldn't override latest
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions);
if (versions.isEmpty()) {
System.out.println("FATAL: wrong # of versions");
System.exit(-1);
}
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) {
System.out.println("FATAL: shouldn't get here");
System.exit(-1);
}
System.out.println("");
}
// scan all rows/columnNames
columnNames.clear();
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
System.out.println("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString());
columnNames.add(col2.name);
}
System.out.println("Starting scanner...");
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
System.out.println("Scanner finished");
break;
}
printRow(entry);
}
transport.close();
}
Aggregations