HBase应用程序开发01创建表
2017-03-05 14:43
162 查看
hbase-1.1.3
hadoop-2.5.2
zookeeper-3.4.6
显示cc为null;
网上说要配置log4j.properties文件, 我也配置了, 仍然报错
Connection通过ConnectionFactory类实例化
Connection 的生命周期是由调用者管理, 可以通过close()来释放连接资源。
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.
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.
实例化, 通过Connection.getAdmin();
Admin的功能:
Admin can be used to create, drop, list, enable and disable tables, add and drop table column families and other administrative operations.
It is used as input when creating a table or adding a column
通常作为一个输入, 用来创建表或添加一个column.
通过HTableDescriptor的对象调用addFammiy(HColumnDeescriptor)来加载一个列簇
通过Admin.createTable(HTableDescriptor)来创建一个表
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的配置文件到Configuration2、Connection 连接类
一个集群的Connection封装了到实际Server的低级Connection和一个与Zookeeper的Connection.Connection通过ConnectionFactory类实例化
conn = ConnectionFactory.createConnection(conf);
Connection 的生命周期是由调用者管理, 可以通过close()来释放连接资源。
Connection对象包含内容:
The connection object contains logic tofind 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 tableNameif (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(); } }
相关文章推荐
- IOS开发笔记-01按钮操作-10.创建应用程序中的一些细节
- 如何从多个项目创建 ASP.NET 应用程序以进行组开发(转贴)
- XAML开发入门之创建XAML应用程序
- SAP 开发陷阱一箩筐(01)——创建了函数组却无法激活
- 使用Apworks开发基于CQRS架构的应用程序(二):创建领域模型项目
- AdvancED Flex 4 (一):使用测试驱动开发模式创建应用程序
- 多层数据库开发三:创建多层应用程序
- Android简明开发教程九:创建应用程序框架
- Visual Studio 2008开发新特性系列课程(10):使用Visual Studio 2008和.NET Compact Framework 3.5创建Mobile应用程序
- Visual Studio 2008开发新特性系列课程(7):使用WCF,WF,Cardspace创建互联的应用程序
- 使用Apworks开发基于CQRS架构的应用程序(六):创建.NET WCF服务
- XAML开发入门之创建XAML应用程序
- 在Java Web开发框架中创建VoiceXML页面-扩展 Java 驱动的 VoiceXML 应用程序
- linux应用程序开发(第2版),创建和使用库章节中共享库的测试(摘录)
- Flex开发中创建并加载一个子应用程序
- 关于如何从多个项目创建 ASP.NET 应用程序以进行组开发问题
- 智能设备开发演练:创建用于设备的 Windows 窗体应用程序
- Visual Studio 2008开发新特性系列课程(10):使用Visual Studio 2008和.NET Compact Framework 3.5创建Mobile应用程序
- Visual C++ .NET 开发环境中为应用程序创建符号文件
- android2.2官方开发手册(01)-Application Fundamentals-应用程序基础知识