HBase配置及操作示例

8 篇文章 0 订阅
5 篇文章 0 订阅

一、Hbase集群搭建

1.上传压缩包及解压

   下载压缩包hbase.tar.gz,执行tar zxvf hbase.tar.gz

2.配置hbase集群,修改配置文件

2.1 修改hbase-env.sh
export JAVA_HOME=/usr/java/jdk1.7.0_55
//告诉hbase使用外部的zk
export HBASE_MANAGES_ZK=false
# Extra Java CLASSPATH elements.  Optional.
export HBASE_CLASSPATH=/home/hadoop/etc/hadoop/
2.2 hbase-site.xml
	<configuration>
        <property>
                  <value>mini1:60000</value>
        </property>
        <property>
                  <value>180000</value>
        </property>
        <property>
                  <name>hbase.rootdir</name>
                  <value>hdfs://mini1:9000/data/hbase</value>#hbase共享目录,持久化hbase数据
        </property>
        <property>
                  <name>hbase.master.info.port</name>
                    <value>60010</value>
            </property>
        <property>
                  <name>hbase.cluster.distributed</name>  #是否分布式运行,false即为单机
                  <value>true</value>
        </property>
        <property>
                   <name>hbase.zookeeper.quorum</name>#zookeeper地址
                   <value>mini1,mini2,mini3</value>
        </property>
        <property>
                   <name>hbase.zookeeper.property.dataDir</name>#zookeeper>配置信息快照的位置
                   <value>/home/hadoop/hbase/tmp/zookeeper</value>
        </property>
</configuration>
2.3 regionservers
mini2
mini3
mini1

3.拷贝hbase到其他节点

	scp -r /home/hadoop/hbase/ mini2:/home/hadoop/
	scp -r /home/hadoop/hbase/ mini3:/home/hadoop/

4.同步时间

时间修改
tzselect

sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

5.启动

	分别启动zk
		./zkServer.sh start
	启动hbase集群
		start-dfs.sh
	启动hbase,在主节点上运行:
		start-hbase.sh

   通过浏览器访问hbase管理页面,192.168.157.129:60010

6.为保证集群的可靠性,要启动多个HMaster

	hbase-daemon.sh start master

7.扩展节点

复制原子节点到新节点上
然后执行 hbase-daemon.sh start regionserver

二、保存数据demo

   新建HBaseUtils

public class HBaseUtils {

    HBaseAdmin admin = null;
    Configuration configuration = null;

    private HBaseUtils(){
        configuration = new Configuration();
        configuration.set("hbase.zookeeper.quorum", "mini1,mini2,mini3");
//        configuration.set("hbase.zookeeper.property.clientPort","2181"); //端口号
        configuration.set("hbase.rootdir", "hdfs://192.168.157.129:9000/data/hbase");
        try {
            admin = new HBaseAdmin(configuration);
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    private static HBaseUtils instance = null;

    public  static synchronized HBaseUtils getInstance() {
        if(null == instance) {
            instance = new HBaseUtils();
        }
        return instance;
    }

    /**
     * 根据表名获取到HTable实例
     * @param tableName
     * @return
     */
    public HTable getTable(String tableName) {

        HTable table = null;
        try {
            table = new HTable(configuration,tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return table;
    }

    /**
     * 添加一条记录到HBase表
     * @param tableName HBase表名
     * @param rowkey HBase表的rowkey
     * @param cf  HBase表的columnfamily
     * @param column  HBase表的列
     * @param value  写入HBase表的值
     */
    public void put(String tableName, String rowkey, String cf, String column, String value) {
        HTable table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowkey));

        put.add(Bytes.toBytes(cf),Bytes.toBytes(column), Bytes.toBytes(value));

        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String tableName = "course_clickcount" ;
        String rowkey = "20171111_88";
        String cf = "info" ;
        String column = "click_count";
        String value = "2";

        HBaseUtils.getInstance().put(tableName, rowkey, cf, column, value);

//        HTable table = HBaseUtils.getInstance().getTable("course_clickcount");
//        System.out.println(table.getName().getNameAsString());
    }


	}

   开发语言使用scala:

   先定义实体类:

case class CourseClickCount(day_course:String, click_count:Long)

   定义dao层操作数据

def save(list: ListBuffer[CourseClickCount]) : Unit = {

    val table = HBaseUtils.getInstance().getTable(tableName)

    for(ele <- list) {
      table.incrementColumnValue(Bytes.toBytes(ele.day_course),
        Bytes.toBytes(cf),
        Bytes.toBytes(qualifer),
        ele.click_count
      )
    }

  }

   测试:

def main(args: Array[String]): Unit = {
    val list = new ListBuffer[CourseClickCount]
    list.append(CourseClickCount("20171111_8",8))
    list.append(CourseClickCount("20171111_9",9))
    list.append(CourseClickCount("20171111_1",100))

    save(list)	
  }

三、查询数据demo

   采用scala语言,根据rowkey查询值

def count(day_course: String):Long = {
    val table = HBaseUtils.getInstance().getTable(tableName)
    val get = new Get(Bytes.toBytes(day_course))
    val value = table.get(get).getValue(cf.getBytes,qualifer.getBytes)

    if(value == null){
      0L
    } else {
      Bytes.toLong(value)
    }

  }

   java语言中根据表名和输入条件获取HBase的记录数

public Map<String, Long> query(String tableName, String condition) throws Exception {
        Map<String, Long> map = new HashMap<>();
        HTable table = getTable(tableName);

        String cf = "info";
        String qualifier = "click_count";
        Scan scan = new Scan();

        Filter filter = new PrefixFilter(Bytes.toBytes(condition));
        scan.setFilter(filter);
        ResultScanner rs = table.getScanner(scan);
        for(Result result : rs){
            String row = Bytes.toString(result.getRow());

            long clickCount = Bytes.toLong(result.getValue(cf.getBytes(),qualifier.getBytes()));
            map.put(row,clickCount);

        }

        return  map;
    }

   测试:

 public static void main(String[] args) throws Exception {

        Map<String, Long> map = HBaseUtils.getInstance().query("course_clickcount" , "20171022");

        for(Map.Entry<String, Long> entry: map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值