下面列出了怎么用org.apache.hadoop.hbase.protobuf.generated.HBaseProtos的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* create snapshot
*
* @param snapshotName snapshot name
* @param admin admin
* @param schemaName schema name
* @param tableName table name
* @throws IOException io exception
*/
public static void createSnapshot(String snapshotName,
Admin admin,
String schemaName,
String tableName) throws IOException {
long start = System.currentTimeMillis();
String fullTableName;
if (Constant.HBASE_NAMESPACE_DEFAULT.equals(schemaName)
|| "".equals(schemaName)) {
fullTableName = tableName;
} else {
fullTableName = schemaName + ":" + tableName;
}
HBaseProtos.SnapshotDescription snapshot = HBaseProtos.SnapshotDescription.newBuilder()
.setName(snapshotName)
.setTable(fullTableName)
.setType(HBaseProtos.SnapshotDescription.Type.FLUSH)
// .setType(HBaseProtos.SnapshotDescription.Type.DISABLED)
.build();
admin.snapshot(snapshot);
log.info("createSnapshot: create snapshot " + snapshotName
+ " used " + (System.currentTimeMillis() - start) + " mill seconds.");
}
/**
* get region infos
*
* @param zookeeperQuorum zookeeper quorum
* @param zookeeperClientPort zookeeper client port
* @param hBaseRootDir HBase root dir
* @param snapshotName snapshot name
* @return region info list
* @throws IOException IOException
*/
public static List<HRegionInfo> getRegionInfos(String zookeeperQuorum, String zookeeperClientPort,
String hBaseRootDir, String snapshotName) throws IOException {
try {
Configuration conf = Utils.getHadoopConf(zookeeperQuorum, zookeeperClientPort);
Path root = new Path(hBaseRootDir);
FileSystem fs = FileSystem.get(conf);
Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, root);
HBaseProtos.SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshotDesc);
return Utils.getRegionInfosFromManifest(manifest);
} catch (IOException ex) {
logger.error("get region info error: " + ex.getMessage(), ex);
throw ex;
}
}
@Test
public void testAfterSuccess() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
// all tables, keep unlimited
String[] argsParam = {"localhost", ".*", "--test",
"--" + Args.OPTION_AFTER_SUCCESS + "=" + AlertSenderTest.ALERT_SCRIPT};
SnapshotArgs args = new SnapshotArgs(argsParam);
Snapshot app = new Snapshot(admin, args);
int sendCountBefore = AlertSender.getSendCount();
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
Assert.assertEquals(sendCountBefore + 1, AlertSender.getSendCount());
}
@Test
public void testExclude() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName, "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
}
@Test
public void testClearWatchLeak() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// with option
argsParam = new String[]{"localhost", ".*", "--test", "--clear-watch-leak"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testExcludeRegexList() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
createAdditionalTable(tableName + "21");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName2 + ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName, snapshotDescriptions.get(0).getTable());
}
@Test
public void testDuplicatedName() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
argsParam = new String[]{"localhost", ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
String snapshotName = app.getPrefix(tableName) + "test";
// create snapshot first
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
// create snapshot again
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testNamespace() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
String fullTableName = null;
try {
// create table with namespace
fullTableName = TEST_NAMESPACE + ":" + TestBase.tableName;
createTable(fullTableName);
// table with namespace
argsParam = new String[]{"localhost", fullTableName};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = admin.listSnapshots(app.getPrefix(fullTableName) + ".*");
assertEquals(1, snapshotDescriptions.size());
} finally {
dropTable(fullTableName);
}
}
@Test
public void testAfterSuccess() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
// all tables, keep unlimited
String[] argsParam = {"localhost", ".*", "--test",
"--" + Args.OPTION_AFTER_SUCCESS + "=" + AlertSenderTest.ALERT_SCRIPT};
SnapshotArgs args = new SnapshotArgs(argsParam);
Snapshot app = new Snapshot(admin, args);
int sendCountBefore = AlertSender.getSendCount();
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
Assert.assertEquals(sendCountBefore + 1, AlertSender.getSendCount());
}
@Test
public void testExclude() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName, "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
}
@Test
public void testClearWatchLeak() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// with option
argsParam = new String[]{"localhost", ".*", "--test", "--clear-watch-leak"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testExcludeRegexList() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
createAdditionalTable(tableName + "21");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName2 + ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName, snapshotDescriptions.get(0).getTable());
}
@Test
public void testDuplicatedName() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
argsParam = new String[]{"localhost", ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
String snapshotName = app.getPrefix(tableName) + "test";
// create snapshot first
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
// create snapshot again
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testNamespace() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
String fullTableName = null;
try {
// create table with namespace
fullTableName = TEST_NAMESPACE + ":" + TestBase.tableName;
createTable(fullTableName);
// table with namespace
argsParam = new String[]{"localhost", fullTableName};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = admin.listSnapshots(app.getPrefix(fullTableName) + ".*");
assertEquals(1, snapshotDescriptions.size());
} finally {
dropTable(fullTableName);
}
}
@Test
public void testAfterSuccess() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
// all tables, keep unlimited
String[] argsParam = {"localhost", ".*", "--test",
"--" + Args.OPTION_AFTER_SUCCESS + "=" + AlertSenderTest.ALERT_SCRIPT};
SnapshotArgs args = new SnapshotArgs(argsParam);
Snapshot app = new Snapshot(admin, args);
int sendCountBefore = AlertSender.getSendCount();
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
Assert.assertEquals(sendCountBefore + 1, AlertSender.getSendCount());
}
@Test
public void testExclude() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName, "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
}
@Test
public void testClearWatchLeak() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// with option
argsParam = new String[]{"localhost", ".*", "--test", "--clear-watch-leak"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testExcludeRegexList() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
createAdditionalTable(tableName + "21");
// with table list
argsParam = new String[]{"localhost", ".*", "--exclude=" + tableName2 + ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(tableName, snapshotDescriptions.get(0).getTable());
}
@Test
public void testDuplicatedName() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
argsParam = new String[]{"localhost", ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
String snapshotName = app.getPrefix(tableName) + "test";
// create snapshot first
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
// create snapshot again
app.snapshot(null, tableName, snapshotName);
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
}
@Test
public void testNamespace() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
String fullTableName = null;
try {
// create table with namespace
fullTableName = TEST_NAMESPACE + ":" + TestBase.tableName;
createTable(fullTableName);
// table with namespace
argsParam = new String[]{"localhost", fullTableName};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = admin.listSnapshots(app.getPrefix(fullTableName) + ".*");
assertEquals(1, snapshotDescriptions.size());
} finally {
dropTable(fullTableName);
}
}
public HBaseProtos.SnapshotDescription.Type flushType(String tableName) {
if (tableFlushMap.get(tableName) == null) {
return SnapshotAdapter.getType(optionSet);
} else {
return SnapshotAdapter.getType(tableName, tableFlushMap);
}
}
public static HBaseProtos.SnapshotDescription.Type getType(String tableName, Map<String, Boolean> tableFlushMap) {
if (tableFlushMap.get(tableName)) {
return HBaseProtos.SnapshotDescription.Type.SKIPFLUSH;
} else {
return HBaseProtos.SnapshotDescription.Type.FLUSH;
}
}
public static HBaseProtos.SnapshotDescription.Type getType(OptionSet optionSet) {
if (optionSet.has(Args.OPTION_SKIP_FLUSH)) {
return HBaseProtos.SnapshotDescription.Type.SKIPFLUSH;
} else {
return HBaseProtos.SnapshotDescription.Type.FLUSH;
}
}
protected void deleteSnapshots(String tableName) throws Exception {
for (HBaseProtos.SnapshotDescription snapshotDescription : admin.listSnapshots(".*" + tableName + ".*")) {
if (snapshotDescription.getTable().equals(tableName) || snapshotDescription.getTable().equals(TEST_NAMESPACE + ":" + tableName)) {
admin.deleteSnapshots(snapshotDescription.getName());
}
}
}
public HBaseProtos.SnapshotDescription.Type flushType(String tableName) {
if (tableFlushMap.get(tableName) == null) {
return SnapshotAdapter.getType(optionSet);
} else {
return SnapshotAdapter.getType(tableName, tableFlushMap);
}
}
@Test
public void testRegexList() throws Exception {
// create tables
String tableName2 = createAdditionalTable(tableName + "2");
String tableName3 = createAdditionalTable(tableName + "3");
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
// all tables, keep unlimited
String[] argsParam = {"localhost", tableName + ".*," + tableName2 + ".*," + tableName3 + ".*"};
SnapshotArgs args = new SnapshotArgs(argsParam);
Snapshot app = new Snapshot(admin, args);
// create snapshot 1
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(3, snapshotDescriptions.size());
// create snapshot 2
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(6, snapshotDescriptions.size());
// create snapshot 3
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(9, snapshotDescriptions.size());
// create snapshot 3
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(12, snapshotDescriptions.size());
}
@Test
public void testList() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", tableName + "," + tableName2};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(2, snapshotDescriptions.size());
// with table list contains blank
argsParam = new String[]{"localhost", tableName + " , " + tableName2};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(4, snapshotDescriptions.size());
}
@Test
public void testDetailList() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", tableName + "/1/true," + tableName2 + "/2/false"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot 1
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(2, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(0).getType());
assertEquals(tableName, snapshotDescriptions.get(1).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.SKIPFLUSH, snapshotDescriptions.get(1).getType());
// create snapshot 2
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(3, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(0).getType());
assertEquals(tableName2, snapshotDescriptions.get(1).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(1).getType());
assertEquals(tableName, snapshotDescriptions.get(2).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.SKIPFLUSH, snapshotDescriptions.get(2).getType());
}
@Test
public void testSkipFlush() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// skip flush
argsParam = new String[]{"localhost", ".*", "--skip-flush=true", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(1, snapshotDescriptions.size());
assertEquals(HBaseProtos.SnapshotDescription.Type.SKIPFLUSH, snapshotDescriptions.get(0).getType());
// do not skip flush
argsParam = new String[]{"localhost", ".*", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(2, snapshotDescriptions.size());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(1).getType());
}
@Test
public void testOverride() throws Exception {
List<HBaseProtos.SnapshotDescription> snapshotDescriptions;
String[] argsParam;
SnapshotArgs args;
Snapshot app;
// create table
String tableName2 = createAdditionalTable(tableName + "2");
// with table list
argsParam = new String[]{"localhost", ".*", "--override=" + tableName + "/1/true", "--keep=2", "--test"};
args = new SnapshotArgs(argsParam);
app = new Snapshot(admin, args);
// create snapshot 1
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(2, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(0).getType());
assertEquals(tableName, snapshotDescriptions.get(1).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.SKIPFLUSH, snapshotDescriptions.get(1).getType());
// create snapshot 2
Thread.sleep(1000);
app.run();
snapshotDescriptions = listSnapshots(tableName + ".*");
assertEquals(3, snapshotDescriptions.size());
assertEquals(tableName2, snapshotDescriptions.get(0).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(0).getType());
assertEquals(tableName2, snapshotDescriptions.get(1).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.FLUSH, snapshotDescriptions.get(1).getType());
assertEquals(tableName, snapshotDescriptions.get(2).getTable());
assertEquals(HBaseProtos.SnapshotDescription.Type.SKIPFLUSH, snapshotDescriptions.get(2).getType());
}