据我所知,如果您想连接到远程HBase服务器,则普通的Java客户端无法正常工作。在其中我们只声明配置并尝试连接到远程HBase,就像之前的回答中提到的那样。
我尝试过上述方法,但从未成功过。相反,我使用Thrift API来连接远程服务器。
此链接是使用Thrift API Java客户端的最佳示例。它肯定有效。我正在使用相同的方法。但在使用之前,请仔细查看代码并删除不需要的内容。我也会提供成功运行的示例代码。
public class ThriftClient
{
port = 9090;
TTransport transport = new TSocket(hostname, port);
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
transport.open();
int z=Link.length();
byte[] tablename = bytes("YOUR TABLE NAME");
ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
ColumnDescriptor col = null;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME"));
col.maxVersions = 10;
columns.add(col);
System.out.println("creating table: " + utf8(tablename));
try
{
client.createTable(ByteBuffer.wrap(tablename), columns);
}
catch (AlreadyExists ae)
{
System.out.println("WARN: " + ae.message);
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false;
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;
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(10);
nf.setGroupingUsed(false);
byte[] row=bytes("YOUR ROW NAME");
mutations = new ArrayList<Mutation>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME:YOUR_COLUMN_NAME")), ByteBuffer.wrap(bytes("YOUR_ROW_VALUE")), writeToWal));
client.mutateRow(ByteBuffer.wrap(tablename), ByteBuffer.wrap(row), mutations, dummyAttributes);
transport.close();
private static String utf8(byte[] buf) {
try {
return decoder.decode(ByteBuffer.wrap(buf)).toString();
} catch (CharacterCodingException e) {
return "[INVALID UTF-8]";
}
}
private static byte[] bytes(String s) {
try {
return s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}