HBase Maven依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hbase.version>1.3.5</hbase.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
</dependencies>
配置文件
- 把HBase配置文件夹中的hbase-site.xml和Hadoop配置文件夹中的core-site.xml配置文件都从服务器上拖下来放到resources文件夹内
Hello HBase
-
于加载需要连接HBase的各项配置
Configuration config = HBaseConfiguration.create();
-
添加配置文件
config.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI())); config.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
-
创建Connection
Connection connection = ConnectionFactory.createConnection(config);
-
建表
- 定义表名的TableName
TableName tableName = TableName.valueOf("mytable");
- 定义表属性的HTableDescriptor
HTableDescriptor table = new HTableDescriptor(tablename);
- 创建列族
HColumnDescriptor mycf=new HColumnDescriptor("mycf"); table.addFamily(new HColumnDescriptor(mycf));
- 执行
Admin admin = connection.qetAdmin(); admin.createTable(table);
- 结束
admin.close(); connection.close();
- 综上
public class Client {
public static void main(String[] args) throws URISyntaxException {
// 获取配置文件
Configuration conf = HBaseConfiguration.create();
conf.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
conf.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
// 创建连接
try {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 定义表名
TableName tableName = TableName.valueOf("mytable");
// 定义表
HTableDescriptor table = new HTableDescriptor(tableName);
// 定义列族
HColumnDescriptor mycf = new HColumnDescriptor("mycf");
table.addFamily(new HColumnDescriptor(mycf));
// 执行创建表动作
admin.createTable(table);
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}