Connector connector = instance.getConnector("root", password);
BatchWriterConfig config = new BatchWriterConfig();
TCredentials creds = CredentialHelper.create("root", password, instance.getInstanceID());
MultiTableBatchWriter mtbw = new MultiTableBatchWriterImpl(instance, creds, config, 60, TimeUnit.SECONDS);
try {
final String table1 = "testTableRenameNewWriters_table1", table2 = "testTableRenameNewWriters_table2";
final String newTable1 = "testTableRenameNewWriters_newTable1", newTable2 = "testTableRenameNewWriters_newTable2";
TableOperations tops = connector.tableOperations();
tops.create(table1);
tops.create(table2);
BatchWriter bw1 = mtbw.getBatchWriter(table1), bw2 = mtbw.getBatchWriter(table2);
Mutation m1 = new Mutation("foo");
m1.put("col1", "", "val1");
m1.put("col2", "", "val2");
bw1.addMutation(m1);
bw2.addMutation(m1);
tops.rename(table1, newTable1);
// MTBW is still caching this name to the correct table, but we should invalidate its cache
// after seeing the rename
try {
bw1 = mtbw.getBatchWriter(table1);
Assert.fail("Should not be able to find this table");
} catch (TableNotFoundException e) {
// pass
}
tops.rename(table2, newTable2);
try {
bw2 = mtbw.getBatchWriter(table2);
Assert.fail("Should not be able to find this table");
} catch (TableNotFoundException e) {
//pass
}
bw1 = mtbw.getBatchWriter(newTable1);
bw2 = mtbw.getBatchWriter(newTable2);
Mutation m2 = new Mutation("bar");
m2.put("col1", "", "val1");
m2.put("col2", "", "val2");
bw1.addMutation(m2);
bw2.addMutation(m2);
mtbw.close();
Map<Entry<String,String>,String> expectations = new HashMap<Entry<String,String>,String>();
expectations.put(Maps.immutableEntry("foo", "col1"), "val1");
expectations.put(Maps.immutableEntry("foo", "col2"), "val2");
expectations.put(Maps.immutableEntry("bar", "col1"), "val1");
expectations.put(Maps.immutableEntry("bar", "col2"), "val2");
for (String table : Arrays.asList(newTable1, newTable2)) {
Scanner s = connector.createScanner(table, new Authorizations());
s.setRange(new Range());
Map<Entry<String,String>,String> actual = new HashMap<Entry<String,String>,String>();
for (Entry<Key,Value> entry : s) {
actual.put(Maps.immutableEntry(entry.getKey().getRow().toString(), entry.getKey().getColumnFamily().toString()), entry.getValue().toString());
}
Assert.assertEquals("Differing results for " + table, expectations, actual);
}
} finally {
if (null != mtbw) {
mtbw.close();
}
}
}