Hbase 2.4.4 配置eclipse开发环境附示例代码
eclipse 环境配置
环境要求
Hadoop 3.2.2
Hbase 2.4.10
eclipse
Java 1.8
创建Java项目
- 打开eclipse
- file ->project
- 选择Java目录里的Java Project,然后next
- 输入项目名,将jre版本设置为1.8,然后选择finish.
- 在项目目录下(非src目录下)新建文件夹conf,将hbase配置文件hbase-site.xml复制到该目录下,然后在项目名右键,选择build path->configure build path,在右侧点击add class folder,选中刚才新建的conf文件夹,然后ok,最后选择apply and close。
- 打开上边的build path界面,点击右侧add external jars,选择hbase安装目录下的 lib文件夹里的jar包,如没有特别需求,全部选中,并且lib目录里还有五个子文件夹,里边的jar包也要选中。
完成后如下图,点击apply and close
示例代码
- 在上述项目内新建package 包 命名为example
- 在example包内新建class类 BaseDemo,复制以下代码
package example;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
public class BaseDemo {
public Connection getConnection() throws IOException {
final Configuration configuration = HBaseConfiguration.create();
//必选参数
configuration.set("hbase.zookeeper.quorum","localhost");//localhost可改成自己的主机名
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//其他可选参数
// configuration.set("hbase.client.scanner.timeout.period", "10000");
// configuration.set("hbase.hconnection.threads.max", "0");
// configuration.set("hbase.hconnection.threads.core", "0");
// configuration.set("hbase.rpc.timeout", "60000");
return ConnectionFactory.createConnection(configuration);
}
//查询表是否存在。默认使用default命名空间。其他命名空间需要传入 {namespace}:tableName
public Boolean isTableExist(Connection connection,String tableName) throws IOException {
final HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName))