docker 安装 hbase安装 java连接hbase (mac环境)

docker 安装

      https://hub.docker.com/editions/community/docker-ce-desktop-mac 下载地址

     下载完之后,安装app一样安装就好

  安装完 配置 Docker 镜像站

  mac 国内镜像站 http://f1361db2.m.daocloud.io
         

 

hbase 安装

  https://www.jianshu.com/p/71db312807c7 参考连接

   

docker search hbase   # 查找hbase

dokcker pull harisekhon/hbase  # 选择最火的版本安装

docker run -d -h myhbase -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 --name hbase harisekhon/hbase:1.3  # 运行habse


docker exec -it hbase  bash # 进入bash

hbase shell # 进入hbase命令行


list # 查看表列表


scan '表名' , {LIMIT=>1} # 扫描表

 

hadoop 安装

     https://www.cnblogs.com/garfieldcgf/p/5462038.html (参考链接)

 

java连接hbase

     第一步,需要在hosts 里面加上对应的hbase名称

  

sudo vim sudo /private/etc/hosts

     

 第二步 添加依赖包(maven项目)

  

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.3</version>
        </dependency>

  

第三步 写代码连接  

package org.myorg.quickstart.hbase_tool;

import org.apache.commons.net.ntp.TimeStamp;
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.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

public class TestHbase {
    private static String zkServer = "localhost";
    private static Integer port = 9095;
    private static TableName tableName = TableName.valueOf("testflink");
    private static final String cf = "ke";

    @Test
    public void testWH() throws IOException{
        write2HBase("test");
    }


    public static void write2HBase(String value) throws IOException {
        Configuration config = HBaseConfiguration.create();

        config.set("hbase.zookeeper.quorum","myhbase");
        config.set("hbase.zookeeper.property.clientPort","2181");

        System.out.println("开始连接hbase");
        Connection connect = ConnectionFactory.createConnection(config);
        System.out.println(connect.isClosed());
        Admin admin = connect.getAdmin();
        System.out.println("连接成功");
//        admin.listTableNames();
//        Table table = connect.getTable(TableName.valueOf("midas_ctr_test"));
        System.out.println("获取表数据成功");
//        for i :table.getScanner().iterator();


        if (!admin.tableExists(tableName)) {
            admin.createTable(new HTableDescriptor(tableName).addFamily(new HColumnDescriptor(cf)));
        }
        System.out.println("建表数据成功");

        Table table = connect.getTable(tableName);
        TimeStamp ts = new TimeStamp(new Date());
        Date date = ts.getDate();
        Put put = new Put(Bytes.toBytes(date.getTime()));
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes("test"), Bytes.toBytes(value));
        table.put(put);
        table.close();
        connect.close();
    }
}

 参考连接 http://www.voidcn.com/article/p-ddrdpdpc-bqt.html     

posted @ 2019-04-18 10:48  jiuyang  阅读(2883)  评论(0编辑  收藏  举报