Windows10环境下安装Hbase单机版

题记:


拉勾JAVA高薪训练营

1.下载安装包

hadoop官网地址:https://hadoop.apache.org/releases.html

hbase官网地址:http://archive.apache.org/dist/hbase

2.配置hadoop环境

在系统环境变量中增加HADOOP_HOME,并在path中增加%HADOOP_HOME%\bin

3.配置hbase环境

在hbase-1.2.10\conf下找到hbase-env.cmd

配置以下属性:

set HBASE_MANAGES_ZK=false
set JAVA_HOME=D:\DEV\env\jdk1.8.0_77

set HBASE_CLASSPATH=F:\hbase\hbase-1.2.10\conf

JAVA_HOME配置自己的jdk路径

HBASE_CLASSPATH配置自己的hbase中conf文件夹路径

在hbase-1.2.10\conf中找到hbase-site.xml

配置以下属性:

<configuration>
    <property>  
        <name>hbase.rootdir</name>  
        <value>file:///F:/hbase/hbase-1.2.10/root</value>  
    </property>  
    <property>  
        <name>hbase.tmp.dir</name>  
        <value>F:/hbase/hbase-1.2.10/tem</value>  
    </property>  
    <property>  
        <name>hbase.zookeeper.quorum</name>  
        <value>127.0.0.1</value>  
    </property>  
    <property>  
        <name>hbase.zookeeper.property.dataDir</name>  
        <value>F:/hbase/hbase-1.2.10/zoo</value>  
    </property>  
    <property>  
        <name>hbase.cluster.distributed</name>  
        <value>false</value>  
    </property>  
</configuration>

同样,涉及到路径的都配置自己的hbase中的路径,其中没有的文件夹,会自己创建

4.启动hbase

进入到hbase-1.2.10\bin目录下,启动start-hbase.cmd,看到控制台无报错信息,并启动停止即可

在cmd模式下,进入到hbase-1.2.10\bin目录下:

输入命令:hbase shell

出现hbase(main):001.0>

恭喜,成功搭建好单机版的hbase环境

5.java连接测试


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HbaseClientUserRela {
    Configuration conf = null;
    Connection conn = null;

    final static String tableName = "user_relation";
    final static String fimilyName = "friends";

    @Before
    public void init() throws IOException {
        //获取一个配置文件对象
        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "127.0.0.1");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取到hbase集群的连接
        conn = ConnectionFactory.createConnection(conf);
    }

    //创建一张hbase表
    @Test
    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器,表描述器
        final HTableDescriptor userRelation = new HTableDescriptor(TableName.valueOf(tableName));
        //指定列族
        userRelation.addFamily(new HColumnDescriptor(fimilyName));
        admin.createTable(userRelation);
        System.out.println(tableName+"表创建成功!!");
    }

    //插入一条数据
    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));

        //准备put对象 uid = uid1
        final Put put = new Put(Bytes.toBytes("uid1"));//指定rowkey
        // 添加好友关系
        put.addColumn(Bytes.toBytes(fimilyName), Bytes.toBytes("rela1"),  Bytes.toBytes("uid2"));
        put.addColumn(Bytes.toBytes(fimilyName), Bytes.toBytes("rela2"),  Bytes.toBytes("uid5"));
        put.addColumn(Bytes.toBytes(fimilyName), Bytes.toBytes("rela3"),  Bytes.toBytes("uid7"));
        put.addColumn(Bytes.toBytes(fimilyName), Bytes.toBytes("rela4"),  Bytes.toBytes("uid9"));
        //插入数据,参数类型是put
        worker.put(put);

        //准备put对象 uid = uid1
        final Put put2 = new Put(Bytes.toBytes("uid2"));//指定rowkey
        // 添加好友关系
        put2.addColumn(Bytes.toBytes(fimilyName), Bytes.toBytes("rela1"),  Bytes.toBytes("uid1"));
        //插入数据,参数类型是put
        worker.put(put2);


        //准备list<puts>,可以执行批量插入
        //关闭table对象
        worker.close();
        System.out.println("插入数据成功!!");
    }

    // 关联删除数据
    @Test
    public void unionDeleteData() throws IOException {
        putData();

        scanData();

        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));

        //删除 uid2 数据
        final Delete delete = new Delete(Bytes.toBytes("uid2"));
        worker.delete(delete);

        //删除 删除uid1中引用uid2的数据
        final Delete delete2 = new Delete(Bytes.toBytes("uid1"));
        delete2.addColumn(fimilyName.getBytes(),"rela1".getBytes());
        worker.delete(delete2);

        //关闭table对象
        worker.close();
        System.out.println("删除数据成功!!");

        scanData();
    }


    //删除一条数据
    @Test
    public void deleteData() throws IOException {
        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));

        //准备delete对象: 删除一行数据
        final Delete delete = new Delete(Bytes.toBytes("uid1"));
        //执行删除
        worker.delete(delete);
        //关闭table对象
        worker.close();
        System.out.println("删除数据成功!!");
    }

    //删除某行一列数据
    @Test
    public void deleteColumnData() throws IOException {
        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));

        //准备delete对象: 删除一行数据
        final Delete delete = new Delete(Bytes.toBytes("uid1"));
        // 指定列族
//        delete.addFamily(fimilyName.getBytes());
        // 指定删除列
        delete.addColumn(fimilyName.getBytes(),"rela1".getBytes());
        //执行删除
        worker.delete(delete);
        //关闭table对象
        worker.close();
        System.out.println("删除数据成功!!");
    }

    //查询数据
    @Test
    public void getData() throws IOException {
        //准备table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));
        //准备get对象
        final Get get = new Get(Bytes.toBytes("uid1"));
        //指定查询某个列族或者列
        get.addFamily(Bytes.toBytes(fimilyName));
        //执行查询
        final Result result = worker.get(get);
        //获取到result中所有cell对象
        final Cell[] cells = result.rawCells();
        //遍历打印
        for (Cell cell : cells) {
            final String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
            final String f = Bytes.toString(CellUtil.cloneFamily(cell));
            final String column = Bytes.toString(CellUtil.cloneQualifier(cell));
            final String value = Bytes.toString(CellUtil.cloneValue(cell));

            System.out.println("rowkey-->" + rowkey + "--;cf-->" + f + "---;column--->" + column + "--;value-->" + value);
        }
        worker.close();
    }

    //全表扫描
    @Test
    public void scanData() throws IOException {
        //准备table对象
        final Table worker = conn.getTable(TableName.valueOf(tableName));
        //准备scan对象
        final Scan scan = new Scan();

        //执行扫描
        final ResultScanner resultScanner = worker.getScanner(scan);
        for (Result result : resultScanner) {
            //获取到result中所有cell对象
            final Cell[] cells = result.rawCells();
            //遍历打印
            for (Cell cell : cells) {
                final String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                final String f = Bytes.toString(CellUtil.cloneFamily(cell));
                final String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                final String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("rowkey-->" + rowkey + "--;cf-->" + f + ";column--->" + column + "--;value-->" + value);
            }
        }

        worker.close();
    }
//指定scan 开始rowkey和结束rowkey,这种查询方式建议使用,指定开始和结束rowkey区间避免全表扫描
@Test
public void scanStartEndData() throws IOException {
    //准备table对象
    final Table worker = conn.getTable(TableName.valueOf(tableName));
    //准备scan对象
    final Scan scan = new Scan();
    //指定查询的rowkey区间,rowkey在hbase中是以字典序排序
    scan.setStartRow(Bytes.toBytes("001"));
    scan.setStopRow(Bytes.toBytes("004"));
    //执行扫描
    final ResultScanner resultScanner = worker.getScanner(scan);
    for (Result result : resultScanner) {
        //获取到result中所有cell对象
        final Cell[] cells = result.rawCells();
        //遍历打印
        for (Cell cell : cells) {
            final String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
            final String f = Bytes.toString(CellUtil.cloneFamily(cell));
            final String column = Bytes.toString(CellUtil.cloneQualifier(cell));
            final String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println("rowkey-->" + rowkey + "--;cf-->" + f + ";column--->" + column + "--;value-->" + value);
        }
    }

    worker.close();
}



    //释放连接
    @After
    public void close() {
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

pom依赖

<dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>
    </dependencies>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。