hbase-1.1.3 hadoop-2.5.2 zookeeper-3.4.6
在HBase测试的时候出现HBaseConfiguration为null; ClassLoader cc = HBaseConfiguration.class.getClassLoader();
System.out.println(cc);
显示cc为null;
有网友说是jar引入错了将hbase的jar删除, 重新引入
结果正常了, 可以创建表了, 出现新的错误:log4j:ERROR Could not find value for key log4j.appender.Console
log4j:ERROR Could not instantiate appender named "Console".
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
网上说要配置log4j.properties文件, 我也配置了, 仍然报错
代码如下
package com.chb.hbase;
import java.io.IOException;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class TestHbase {
public static Connection conn = null;
public static TableName tableName = TableName.valueOf("tb_phone");
public static void create() throws IOException {
Configuration conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor htd = new HTableDescriptor(tableName);
HColumnDescriptor hcd = new HColumnDescriptor("f1".getBytes());
htd.addFamily(hcd);
admin.createTable(htd);
}
public static void main(String[] args) throws IOException {
System.setProperty("HADOOP_USER_NAEM", "chb");
create();
}
}
1、HBaseConfiguration
用来加载hbase的配置文件到Configuration
2、Connection 连接类一个集群的Connection封装了到实际Server的低级Connection和一个与Zookeeper的Connection. Connection通过ConnectionFactory类实例化
conn = ConnectionFactory.createConnection(conf);
Connection 的生命周期是由调用者管理, 可以通过close()来释放连接资源。
Connection对象包含内容:The connection object contains logic to
- find the master, 查找主节点
- locate regions out on the cluster 定位集群中的region
- keeps a cache of locations and then knows how to re-calibrate after they move.
The individual connections to servers, meta cache, zookeeper connection, etc are all shared by the Table and Admin instances obtained from this connection.
在Hbase-1.1.3中提示Hxx类已经被遗弃
/*
* Implementation notes:
* - Only allow new style of interfaces:
* -- All table names are passed as TableName. No more byte[] and string arguments
* -- Most of the classes with names H is deprecated in favor of non-H versions
* (Table, Connection vs HConnection, etc)
* -- Only real client-facing public methods are allowed
* - Connection should contain only getTable(), getAdmin() kind of general methods.
*/
Connection的创建时一个重量级的操作,Connection的实现都是thread-safe,所以client可以创建一个connection,供不同的线程使用。 Table和Admin的实例,都是light-weight and are not thread-safe. 通常,每个客户端应用实例化一个connection, 每个线程都会获取自己的Table。 Caching or pooling of Table and Admin is not recommended.
3、Admin是HBase的管理API, 实例化, 通过Connection.getAdmin();
Admin admin = conn.getAdmin();
Admin的功能: Admin can be used to create, drop, list, enable and disable tables, add and drop table column families and other administrative operations.
如判断表的存在, 删除:删除表: 首先disable tableName, 然后 drop tableName
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HColumnDescriptor
列簇的信息, 包含列簇的详细信息,如:version, compression 等。 It is used as input when creating a table or adding a column 通常作为一个输入, 用来创建表或添加一个column.
HTableDescriptor表的描述信息
通过HTableDescriptor的对象调用addFammiy(HColumnDeescriptor)来加载一个列簇
HColumnDescriptor hcd = new HColumnDescriptor("f1".getBytes());
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(hcd);
通过Admin.createTable(HTableDescriptor)来创建一个表
admin.createTable(htd);
关闭Connection
//关闭连接
conn.close();
完整代码:
package com.chb.hbase;
import java.io.IOException;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class TestHbase {
public static Connection conn = null;
public static TableName tableName = TableName.valueOf("tb_phone");
public static void create() throws IOException {
Configuration conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HColumnDescriptor hcd = new HColumnDescriptor("f1".getBytes());
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(hcd);
admin.createTable(htd);
//关闭连接
conn.close();
}
public static void main(String[] args) throws IOException {
System.setProperty("HADOOP_USER_NAEM", "chb");
create();
}
}