Shift-JISだかららしい。
- Mac OS X 10.8.2
これを実行するか、しかるべきスクリプトに書く。
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
# java truncateWithRegions Usage : java truncateWithRegions[<filename>]
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>]");
}
# yum -y groupinstall "Japanese Support"
# vi /etc/sysconfig/i18n
LANG="C"<変更後>
LANG="ja_JP.UTF-8"
# source /etc/sysconfig/i18n # echo $LANG ja_JP.UTF-8
http://thrift.apache.org/docs/install/centos/
# yum install hadoop-hbase-thrift
http://www.apache.org/dyn/closer.cgi/hbase/
# cp {hadoopのソースコード展開ディレクトリ}/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift {作業ディレクトリ}
# cd {作業ディレクトリ}
# thrift --gen py Hbase.thrift
# cd gen-py # vi hbaseClient.py
import sys
sys.path.append('./gen-py')
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
transport=TBufferedTransport(TSocket('localhost', 9090))
transport.open()
protocol=TBinaryProtocol.TBinaryProtocol(transport)
client=Hbase.Client(protocol)
print(client.getTableNames())
#service hadoop-hbase-thrift start # python hbaseClient.py ['usertable']