分割キーを指定した場合は、その通りに。しなかった場合は、消す前のテーブルのリージョン(枠)を再現する。
- ソースコード
# java truncateWithRegions Usage : java truncateWithRegions[<filename>]
truncateWithRegions.java
import java.io.*;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class truncateWithRegions {
public static void main(String[] args) throws Exception {
if (args.length < 1 || args.length > 2) {
printUsage();
System.exit(1);
}
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hAdmin = new HBaseAdmin(conf);
HTableDescriptor desc;
byte[][] splits = null;
if (args.length == 2) splits = readSplitKeys(args[1]);
if (hAdmin.tableExists(args[0])) {
desc = hAdmin.getTableDescriptor(Bytes.toBytes(args[0]));
if (args.length == 1) {
splits = getSplitKeys(args[0]);
}
hAdmin.disableTable(args[0]);
hAdmin.deleteTable(args[0]);
} else {
desc = new HTableDescriptor(args[0]);
}
createTable(hAdmin, desc, splits);
}
private static byte[][] getSplitKeys(String tableName) throws Exception {
HTable hTable = new HTable(tableName);
byte[][] splits = new byte[hTable.getEndKeys().length - 1][];
for ( int i = 0; i < hTable.getEndKeys().length - 1; i++) {
splits[i] = hTable.getEndKeys()[i];
}
return splits;
}
private static byte[][] readSplitKeys(String filename) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(filename));
List<byte[]> splitList = new ArrayList<byte[]>();
String line = "";
while ((line = br.readLine()) != null) {
if (line.length() != 0) {
byte[] split = Bytes.toBytes(line);
splitList.add(split);
}
}
br.close();
return splitList.toArray(new byte[0][]);
}
private static void createTable(HBaseAdmin hAdmin, HTableDescriptor desc, byte[][] splits) throws Exception {
if (splits == null) {
hAdmin.createTable(desc);
} else {
hAdmin.createTable(desc, splits);
}
}
private static void printUsage() {
System.err.println("Usage : java truncateWithRegions <tablename> [<filename>]");
}
0 件のコメント:
コメントを投稿