代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseTestCase {
//锟斤拷锟斤拷锟斤拷态锟斤拷锟斤拷
static Configuration cfg=HBaseConfiguration.create();
static {
cfg.set("hbase.zookeeper.quorum", "192.168.153.128");
//cfg.set("hbase.master", "192.168.153.128");
}
//锟斤拷锟斤拷 一锟脚憋拷
public static void creat(String tablename,String columnFamily) throws Exception {
HBaseAdmin admin=new HBaseAdmin(cfg);
if (admin.tableExists(tablename)) {
System.out.println("table exists");
System.exit(0);
}
else {
HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tablename));
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success !");
}
admin.close();
}
//锟斤拷锟揭伙拷锟斤拷锟铰�
public static void put(String tablename,String row,String columnFamily,String column,String data) throws Exception{
HTable table=new HTable(cfg, tablename);
Put p1=new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(p1);
System.out.println("put "+row+","+columnFamily+":"+column+","+data);
table.close();
}
//取一锟斤拷 锟斤拷录锟斤拷锟斤拷锟�rowkey
public static void get(String tablename,String row) throws Exception{
HTable table=new HTable(cfg, tablename);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
System.out.println("Get:"+result);
table.close();
}
//锟斤拷锟斤拷
public static void scan(String tablename) throws Exception{
HTable table=new HTable(cfg, tablename);
Scan s=new Scan();
ResultScanner rs=table.getScanner(s);
for (Result r:rs){
System.out.println("Scan:"+r);
}
table.close();
}
//删锟斤拷锟铰�
public static boolean delete (String tablename) throws Exception{
HBaseAdmin admin=new HBaseAdmin(cfg);
if (admin.tableExists(tablename)) {
try {
admin.disableTable(tablename);
admin.deleteTable(tablename);
} catch (Exception ex) {
ex.printStackTrace();
admin.close();
return false;
}
}
admin.close();
return true;
}
public static void main(String[] args) {
String tablename="htest524";
String columnFamily="cf";
try {
HBaseTestCase.creat(tablename,columnFamily);
HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "0000");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "0000");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "1111");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "2222");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl2", "aaaa");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl3", "bbbb");
HBaseTestCase.put(tablename, "row1", columnFamily, "cl4", "cccc");
HBaseTestCase.scan(tablename);
if (true==HBaseTestCase.delete(tablename)){
System.out.println("Delete table success! ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
问题:
java.io.IOException: java.lang.reflect.InvocationTargetException
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:457)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:434)
at org.apache.hadoop.hbase.client.HConnectionManager.getConnection(HConnectionManager.java:315)
at org.apache.hadoop.hbase.client.HBaseAdmin.(HBaseAdmin.java:194)
at HBaseTestCase.creat(HBaseTestCase.java:25)
at HBaseTestCase.main(HBaseTestCase.java:90)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:455)
... 5 more
Caused by: java.lang.ExceptionInInitializerError
at org.apache.hadoop.hbase.ClusterId.parseFrom(ClusterId.java:64)
at org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode(ZKClusterId.java:69)
at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId(ZooKeeperRegistry.java:83)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.retrieveClusterId(HConnectionManager.java:897)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.(HConnectionManager.java:694)
... 10 more
Caused by: java.lang.IllegalArgumentException: java.net.UnknownHostException: master
at org.apache.hadoop.security.SecurityUtil.buildTokenService(SecurityUtil.java:418)
at org.apache.hadoop.hdfs.NameNodeProxies.createNonHAProxy(NameNodeProxies.java:231)
at org.apache.hadoop.hdfs.NameNodeProxies.createProxy(NameNodeProxies.java:139)
at org.apache.hadoop.hdfs.DFSClient.(DFSClient.java:510)
at org.apache.hadoop.hdfs.DFSClient.(DFSClient.java:453)
at org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:136)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2433)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:88)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2467)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2449)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:367)
at org.apache.hadoop.fs.Path.getFileSystem(Path.java:287)
at org.apache.hadoop.hbase.util.DynamicClassLoader.(DynamicClassLoader.java:104)
at org.apache.hadoop.hbase.protobuf.ProtobufUtil.(ProtobufUtil.java:201)
... 15 more
Caused by: java.net.UnknownHostException: master
... 29 more