java代码
HbaseTest
public class HbaseTest {
/**
* 配置
* */
static Configuration conf = null;
static{
conf = HBaseConfiguration.create();
}
/**
* create a table
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
* */
public void createTable(String tableName,String [] familys) {
/**
* 0.9X edition
* */
// HBaseAdmin admin = new HBaseAdmin(conf);
// HTableDescriptor descriptor = new HTableDescriptor();
/**
* 1.X edition
* */
Connection connection;
Admin admin = null;
try {
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 新的写法
* */
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
for(String family:familys){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(family);
table.addFamily(hColumnDescriptor);
}
try {
admin.createTable(table);
System.out.println("table is already created!\n");
/**
* 关闭admin
* */
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestCase
public class TestCase {
HbaseTest hbase = new HbaseTest();
@Test
public void testCreateTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
String [] fs = {"personal","public","name"};
hbase.createTable("tttzz", fs);
}
}
问题
在执行的时候一直卡在了`admin.createTable(table)`然后去查找资料,看到了一个解决,就是HDFS的`safemode`开着,但是查看自己的安全模式确实关闭的。
hadoop dfsadmin -safemode get
显示Safe mode is OFF
然后懵逼了。
解决
然后突然想到我的配置文件里面用的是hostname
而不是ip地址,所以执行的时候无法确定ip地址,需要改host文件或者改hbase-site.xml