欢迎投稿

今日深度:

Hbase实例,

Hbase实例,


hbase
版本 hadoop-2.7.3
版本 hbase-1.2.7
Hadoop启动
start-hbase.sh
进入HBASE
hbase shell

以下为HBASE安装过程
#解压文件到指定目录
tar -zxvf /hbase-1.2.7-bin.tar.gz -C /usr/lib/
#配置 hbase-site.xml 文件
/root/opt/module/hbase-1.2.7/conf
##增加

hbase.rootdir
hdfs://itcast01:9000/hbase

hbase.cluster.distributed true #设置环境变量 .bashrc 隐藏的文件和profile 都要改,然后使用 source+文件名使其生效 修改HBase下的conf目录中的hbase-env.sh文件 export JAVA_HOME=/usr/java/jdk1.8.0_181 export HBASE_MANAGES_ZK=true #修改 /etc/profile 文件 export PATH=$PATH:/usr/lib/hbase/bin #启动,在bin目录下 ./start-hbase.sh 启动 /stop-hbase.sh 停止 3.2.2 验证启动 1. 在hadoop节点使用jps查看节点状态
  1. 进入hbase的shell命令行,创建表member并进行查看
    hbase shell
    hbase>create ‘member’, ‘m_id’, ‘address’, ‘info’

遇到的主要问题:

hbase主节点HMaster启动失败
ERROR: Can’t get master address from ZooKeeper; znode data == null
注意:看错误提示运行hbase(zookeeper)的用户无法写入zookeeper文件,导致znode data为空。
解决:在hbase-site.xml指定一个运行hbase的用户有写入文件权限的目录作为zookeeper数据目录,如

hbase.zookeeper.property.dataDir /opt/zkData

(2)hbase-site.xml文件中的

hbase.rootdir hdfs://itcast01:9000/hbase rootdir中的IP设定很重要,需要设定对应的IP与core-site.xml中fs.defaultFS中的路径不相同 fs.defaultFS hdfs://itcast01:9000 不能写IP,必须在host文件里配置 域名 与hadoop配置一样 关闭hbase服务的时候,HRegionserver服务一直关不掉,修改配置文件无法重启生效, hbase-daemon.sh stop regionserver RegionServer就报错提示如下 no zookeeper to stop because no pid file /tmp/hbase-hadoop-zookeeper.pid 原因是,默认情况下pid文件保存在/tmp目录下,/tmp目录下的文件很容易丢失, 解决办法:在hbase-env.sh中修改pid文件的存放路径; 在hbase-env.sh中下面的文字默认是注释掉的,放开即可,也可以自己指定存放位置: # The directory where pid files are stored. /tmp by default. export HBASE_PID_DIR=/opt/hbase/pids

HBase 开发环境搭建(Eclipse+Maven)
第一步:Eclipse中新建Maven项目,编辑pom.xml并更新下载jar包
<projectxmlns=“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.0http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
fulong.bigdata
myHbase
0.0.1-SNAPSHOT


org.apache.hbase
hbase-client
0.96.2-hadoop2


org.apache.hadoop
hadoop-hdfs
2.2.0


jdk.tools
jdk.tools
1.7
system
${JAVA_HOME}/lib/tools.jar



第二步:将目标集群的Hadoop和HBase配置文件拷贝到工程中
目的是为了让工程能找到Zookeeper及Hbase Master。
配置文件在工程中的路径为:
/src/main/resources/hadoop
/src/main/resources/hbase

然后将这两个目录添加进工程的classpath中:

最终目录结构如下:

第三步:hbase-site.xml中添加

fs.hdfs.impl
org.apache.hadoop.hdfs.DistributedFileSystem

第四步:编写Java程序调用Hbase接口
该代码包含了部分常用HBase接口。

package myHbase;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseDAO {

static Configuration conf = HBaseConfiguration.create();
/**

  • create a table :table_name(columnFamily)
  • @param tablename
  • @param columnFamily
  • @throws Exception
    */
    public static void createTable(String tablename, String columnFamily) throws Exception {
    HBaseAdmin admin = new HBaseAdmin(conf);
    if(admin.tableExists(tablename)) {
    System.out.println(“Table exists!”);
    System.exit(0);
    }
    else {
    HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
    tableDesc.addFamily(new HColumnDescriptor(columnFamily));
    admin.createTable(tableDesc);
    System.out.println(“create table success!”);
    }
    admin.close();

}

/**

  • delete table ,caution!!! ,dangerous!!!
  • @param tablename
  • @return
  • @throws IOException
    /
    public static boolean deleteTable(String tablename) throws IOException {
    HBaseAdmin admin = new HBaseAdmin(conf);
    if(admin.tableExists(tablename)) {
    try {
    admin.disableTable(tablename);
    admin.deleteTable(tablename);
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    admin.close();
    return false;
    }
    }
    admin.close();
    return true;
    }
    /
    *
  • put a cell data into a row identified by rowKey,columnFamily,identifier
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @param rowKey
  • @param columnFamily
  • @param identifier
  • @param data
  • @throws Exception
    */
    public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data) throws Exception{
    Put p1 = new Put(Bytes.toBytes(rowKey));
    p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));
    table.put(p1);
    System.out.println(“put '”+rowKey+"’, ‘"+columnFamily+":"+identifier+"’, ‘"+data+"’");
    }

/**

  • get a row identified by rowkey
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @param rowKey
  • @throws Exception
    */
    public static Result getRow(HTable table, String rowKey) throws Exception {
    Get get = new Get(Bytes.toBytes(rowKey));
    Result result = table.get(get);
    System.out.println("Get: "+result);
    return result;
    }

/**

  • delete a row identified by rowkey
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @param rowKey
  • @throws Exception
    */
    public static void deleteRow(HTable table, String rowKey) throws Exception {
    Delete delete = new Delete(Bytes.toBytes(rowKey));
    table.delete(delete);
    System.out.println("Delete row: "+rowKey);
    }

/**

  • return all row from a table
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @throws Exception
    */
    public static ResultScanner scanAll(HTable table) throws Exception {
    Scan s =new Scan();
    ResultScanner rs = table.getScanner(s);
    return rs;
    }

/**

  • return a range of rows specified by startrow and endrow
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @param startrow
  • @param endrow
  • @throws Exception
    /
    public static ResultScanner scanRange(HTable table,String startrow,String endrow) throws Exception {
    Scan s =new Scan(Bytes.toBytes(startrow),Bytes.toBytes(endrow));
    ResultScanner rs = table.getScanner(s);
    return rs;
    }
    /
    *
  • return a range of rows filtered by specified condition
  • @param HTable, create by : HTable table = new HTable(conf, “tablename”)
  • @param startrow
  • @param filter
  • @throws Exception
    */
    public static ResultScanner scanFilter(HTable table,String startrow, Filter filter) throws Exception {
    Scan s =new Scan(Bytes.toBytes(startrow),filter);
    ResultScanner rs = table.getScanner(s);
    return rs;
    }

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

HTable table = new HTable(conf, “apitable”);

// ResultScanner rs = HBaseDAO.scanRange(table, “2013-07-10*”, “2013-07-11*”);
// ResultScanner rs = HBaseDAO.scanRange(table, “100001”, “100003”);
ResultScanner rs = HBaseDAO.scanAll(table);

for(Result r:rs) {
System.out.println("Scan: "+r);
}
table.close();

// HBaseDAO.createTable(“apitable”, “testcf”);
// HBaseDAO.putRow(“apitable”, “100001”, “testcf”, “name”, “liyang”);
// HBaseDAO.putRow(“apitable”, “100003”, “testcf”, “name”, “leon”);
// HBaseDAO.deleteRow(“apitable”, “100002”);
// HBaseDAO.getRow(“apitable”, “100003”);
// HBaseDAO.deleteTable(“apitable”);

}
}


运行结果图:

www.htsjk.Com true http://www.htsjk.com/hbase/40913.html NewsArticle Hbase实例, hbase 版本 hadoop-2.7.3 版本 hbase-1.2.7 Hadoop启动 start-hbase.sh 进入HBASE hbase shell 以下为HBASE安装过程 #解压文件到指定目录 tar -zxvf /hbase-1.2.7-bin.tar.gz -C /usr/lib/ #配置 hbase-site.xml 文件...
相关文章
    暂无相关文章
评论暂时关闭