hadoop版本2.7.1,hbase版本0.98.13
1.配置pom.xml文件:
[mw_shl_code=xml,true]<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.byzoro</groupId>
<artifactId>hbaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hbaseTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.13-hadoop2</version>
</dependency>
</dependencies>
</project>
[/mw_shl_code]
2. 创建资源目录src/main/resources/hdfs和src/main/resources/hbase,将相关的配置文件拷贝过来
3. 将src/main/resources/hdfs和src/main/resources/hbase添加至项目的build path。
4. 配置完成,测试代码:
[mw_shl_code=java,true]package com.byzoro.hbaseTest;
import java.io.File;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class StartUp {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
// TODO Auto-generated method stub
Configuration conf = HBaseConfiguration.create();
System.out.println(conf.get("hbase.rootdir"));
HBaseAdmin admin = new HBaseAdmin(conf);
TableName tbName = TableName.valueOf("person");
if (!admin.tableExists(tbName)){
HTableDescriptor tableDesc = new HTableDescriptor(tbName);
HColumnDescriptor columDesc = new HColumnDescriptor("content");
tableDesc.addFamily(columDesc);
admin.createTable(tableDesc);
System.out.println("Create table person");
}else{
System.out.println("Table person exsist");
}
}
}
[/mw_shl_code]
|