• windows 10环境下搭建基于HBase的Eclipse开发环境


    Apache HBase是一个分布式的、面向列的开源数据库,其主要特点是:

    •  HBase – Hadoop Database,是一个高可靠性、高性能、面向列、可伸缩、 实时读写的分布式数据库 
    • 利用Hadoop HDFS作为其文件存储系统,利用Hadoop MapReduce来处理 HBase中的海量数据,利用Zookeeper作为其分布式协同服务
    •  主要用来存储非结构化和半结构化的松散数据(列存NoSQL数据库)

    本文用于指导hbase在windows10环境下单机版的使用,采用伪分布式。

    1.1  准备工作

    下载对应的软件版本:

    • Windows10家庭版
    • JDK 1.8.0_171-b11
    • hbase-1.2.6.1

    lHBase的伪分布模式是依赖于HDFS的,因此在安装HBase之前需要安装匹配版本的Hadoop。新建环境变量HADOOP_HOME,在path后添加:%HADOOP_HOME%in

    Hadoop安装参考文章:https://www.cnblogs.com/taoweizhong/p/10502261.html

    1.2  HBase的配置与启动

    【JDK安装不再说明】

    第一步:HBase解压软件

    我使用的路径是:D:Studycodeprojecthbase-1.2.6.1

    第二步:修改HBase下的conf/hbase-env.cmd

    set JAVA_HOME=C:Javajdk1.8.0_171

    set HBASE_MANAGES_ZK=false

    其他都使用默认值,无需修改。

    第三步:修改HBase下的hbase-site.xml

    D:Studycodeprojecthbase-1.2.6.1conf

    <configuration> 

        <property> 

            <name>hbase.rootdir</name> 

            <value>file:///D:/Study/codeproject/hbase-1.2.6.1/data</value> 

        </property> 

        <property> 

            <name>hbase.tmp.dir</name> 

            <value>D:/Study/codeproject/hbase-1.2.6.1/data/tmp</value>  

        </property> 

        <property> 

            <name>hbase.zookeeper.quorum</name> 

            <value>127.0.0.1</value> 

        </property> 

        <property> 

            <name>hbase.zookeeper.property.dataDir</name> 

            <value>D:/Study/codeproject/hbase-1.2.6.1/data/zoo</value> 

        </property> 

        <property> 

            <name>hbase.cluster.distributed</name> 

            <!--HBase以分布式模式进行,这个功能在win下不支持,写成false-->

            <value>false</value> 

        </property> 

    </configuration>

    第四步:启动HBase

    D:Studycodeprojecthbase-1.2.6.1in下打开命令行,输入start-hbase.cmd,启动HBase。

    第五步:测试Shell

     HBase启动后,打开一个新窗口,进入D:Studycodeprojecthbase-1.2.6.1in,在命令行输入hbase shell,出现如下结果表示连接成功。

    D:Studycodeprojecthbase-1.2.6.1in>hbase shell

    HBase Shell; enter 'help<RETURN>' for list of supported commands.

    Type "exit<RETURN>" to leave the HBase Shell

    Version 1.2.6.1, rUnknown, Sun Jun  3 23:19:26 CDT 2018

    hbase(main):001:0>

    第六步:打开HBase主页GUI,在浏览器输入网址:http://127.0.0.1:16010/master-status

     

    1.3  HBase的基本操作

    第一步:创建表hbase_test有两个列族cf1cf2

    此表有两个列族,cf1和cf2,其中CF1和CF2下分别有两个列name和gender,Chinese和Math

    hbase(main):010:0>  create 'hbase_test',  {NAME=>'cf1'}, {NAME=>'cf2'}

    0 row(s) in 1.2440 seconds

    => Hbase::Table - hbase_test

    切换到管理页面查看表:

    第二步:向表中添加数据,在想HBase的表中添加数据的时候,只能一列一列的添加,不能同时添加多列。

    hbase(main):011:0> put'hbase_test', '001','cf1:name','Tao'

    0 row(s) in 0.0720 seconds

    hbase(main):019:0> put'hbase_test', '002','cf1:gender','man'

    0 row(s) in 0.0060 seconds

    hbase(main):020:0>

    hbase(main):021:0*  put'hbase_test', '001','cf2:chinese','99'

    0 row(s) in 0.0030 seconds

    hbase(main):022:0>  put'hbase_test', '001','cf2:math','100'

    0 row(s) in 0.0040 seconds

    这样表结构就起来了,其实比较自由,列族里边可以自由添加子列很方便。如果列族下没有子列,加不加冒号都是可以的。

    如果在添加数据的时候,需要手动的设置时间戳,则在put命令的最后加上相应的时间戳,时间戳是long类型的,所以不需要加引号

    hbase(main):045:0> put'hbase_test', '001','cf2:math','91',1478053832459

    第三步:查看表中的所有数据

    hbase(main):027:0* scan 'hbase_test'

    ROW                 COLUMN+CELL                                                                            

     001                column=cf1:name, timestamp=1552210114388, value=Tao                                    

     001                column=cf2:chinese, timestamp=1552210284719, value=99                                  

     001                column=cf2:math, timestamp=1552210290155, value=100                                    

     002               column=cf1:gender, timestamp=1552210272044, value=man                                  

    2 row(s) in 0.0250 seconds

    第四步:查看其中某一个Key的数据

    hbase(main):028:0>  get'hbase_test','001'

    COLUMN                      CELL                                                                                   

     cf1:name                      timestamp=1552210114388, value=Tao                                                     

     cf2:chinese                    timestamp=1552210284719, value=99                                                      

     cf2:math                       timestamp=1552210290155, value=100                                                     

    3 row(s) in 0.0280 seconds

    1.4  基于eclipse的HBase的java操作

    第一步:在eclipse中创建如下demo代码(参考网络代码编写),注意:相关包的引入,否则编译不通过,同时HBase已经启动:

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    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.KeyValue;
    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.HTablePool;
    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;
    
    public class HbaseTest
    {
        private HBaseAdmin admin = null;
        // 定义配置对象HBaseConfiguration
        private HBaseConfiguration cfg = null;
        public HbaseTest() throws Exception
        {
            Configuration HBASE_CONFIG = new Configuration();
            HBASE_CONFIG.set("hbase.zookeeper.quorum", "127.0.0.1");
            HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
            //HBASE_CONFIG.set("hbase.master", "192.168.1.109:9001"); 
            cfg = new HBaseConfiguration(HBASE_CONFIG);
            admin = new HBaseAdmin(cfg);
        }
        // 创建一张表,指定表名,列族
        public void createTable(String tableName, String columnFarily)throws Exception
        {
            if (admin.tableExists(tableName))
            {
                System.out.println(tableName + "存在!");
                System.exit(0);
    
            } else
            {
                HTableDescriptor tableDesc = new HTableDescriptor(tableName);
                tableDesc.addFamily(new HColumnDescriptor(columnFarily));
                admin.createTable(tableDesc);
                System.out.println("创建表成功!");
            }
        }
        // Hbase获取所有的表信息
        public List getAllTables()
        {
            List<String> tables = null;
            if (admin != null)
            {
                try
                {
                    HTableDescriptor[] allTable = admin.listTables();
                    if (allTable.length > 0)
                        tables = new ArrayList<String>();
                    for (HTableDescriptor hTableDescriptor : allTable)
                    {
                        tables.add(hTableDescriptor.getNameAsString());
                        System.out.println(hTableDescriptor.getNameAsString());
                    }
                } 
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return tables;
        }
        // Hbase中往某个表中添加一条记录
        public boolean addOneRecord(String table, String key, String family,String col, byte[] dataIn)
        {
            HTablePool tp = new HTablePool(cfg, 1000);
            // HTable tb = (HTable) tp.getTable(table);
            Put put = new Put(key.getBytes());
            put.add(family.getBytes(), col.getBytes(), dataIn);
            try
            {
                HTablePool pool = new HTablePool(cfg, 1000);
                pool.getTable(table).put(put);
                // tb.put(put);
                System.out.println("插入数据条" + key + "成功!!!");
                return true;
            } catch (IOException e)
            {
                e.printStackTrace();
                System.out.println("插入数据条" + key + "失败!!!");
                return false;
            }
        }
        // Hbase表中记录信息的查询
        public void getValueFromKey(String table, String key)
        {
            HTablePool tp = new HTablePool(cfg, 1000);
            // HTable tb = (HTable) tp.getTable(table);
            Get get = new Get(key.getBytes());
            try
            {
                Result rs = tp.getTable(table).get(get);
                if (rs.raw().length == 0)
                {
                    System.out.println("不存在关键字为" + key + "的行!!");
                } else
                {
                    for (KeyValue kv : rs.raw())
                    {
                        System.out.println(new String(kv.getKey()) + " " + new String(kv.getValue()));
                    }
                }
    
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        // 显示所有数据,通过HTable Scan类获取已有表的信息
        public void getAllData(String tableName) throws Exception
        {
            HTable table = new HTable(cfg, tableName);
            Scan scan = new Scan();
            ResultScanner rs = table.getScanner(scan);
            for (Result r : rs)
            {
                for (KeyValue kv : r.raw())
                {
                    System.out.println(new String(kv.getKey())+ new String(kv.getValue()));
                }
            }
        }
    
        // Hbase表中记录信息的删除
        public boolean deleteRecord(String table, String key)
        {
            HTablePool tp = new HTablePool(cfg, 1000);
            // HTable tb = (HTable) tp.getTable(table);
            Delete de = new Delete(key.getBytes());
            try
            {
                tp.getTable(table).delete(de);
                return true;
            } catch (IOException e)
            {
                System.out.println("删除记录" + key + "异常!!!");
                return false;
            }
        }
        // Hbase中表的删除
        public boolean deleteTable(String table)
        {
            try
            {
                if (admin.tableExists(table))
                {
                    admin.disableTable(table);
                    admin.deleteTable(table);
                    System.out.println("删除表" + table + "!!!");
                }
                return true;
            } catch (IOException e)
            {
                System.out.println("删除表" + table + "异常!!!");
                return false;
            }
    
        }
        // 测试函数
        public static void main(String[] args)
        {
            try
            {
                HbaseTest hbase = new HbaseTest();
                hbase.createTable("hbase_test2", "cf1");
                // hbase.getAllTables();
                hbase.addOneRecord("hbase_test2", "id1", "cf1", "name", "Kite".getBytes());
                hbase.addOneRecord("hbase_test2", "id1", "cf1", "address", "HW".getBytes());
                // hbase.getValueFromKey("hbase_test2","id1");
                // hbase.getAllData("hbase_test2");
                // hbase.deleteRecord("hbase_test2", "id1");
                // hbase.deleteTable("hbase_test2");
            } 
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

     第二步:执行上述代码,成功之后eclipse打印如下信息(上述测试函数中注释掉的代码,可以分别放开测试并查看数据,这里不一一列举):

    上述代码执行后结果打印如下:

    DEBUG [main] - Connecting to LAPTOP-TAOTAO/192.168.1.104:59608

     INFO [main] - Created hbase_test2

    创建表成功!

    DEBUG [main-SendThread(127.0.0.1:2181)] - Reading reply sessionid:0x16977330f120007, packet:: clientPath:null serverPath:null finished:false header:: 7,4  replyHeader:: 7,1108,0  request:: '/hbase/meta-region-server,F  response:: #ffffffff0001a726567696f6e7365727665723a35393633392410ffffffd342ffffffce75323350425546a1aad4c4150544f502d54414f54414f10fffffff7ffffffd1318ffffff9dffffffa9ffffffccffffffb9ffffff972d100183,s{1080,1080,1552483046311,1552483046311,0,0,0,0,67,0,1080}

    DEBUG [main-SendThread(127.0.0.1:2181)] - Reading reply sessionid:0x16977330f120007, packet:: clientPath:null serverPath:null finished:false header:: 8,8  replyHeader:: 8,1108,0  request:: '/hbase,F  response:: v{'replication,'meta-region-server,'rs,'splitWAL,'backup-masters,'table-lock,'flush-table-proc,'region-in-transition,'online-snapshot,'master,'running,'recovering-regions,'draining,'namespace,'hbaseid,'table}

    插入数据条id1成功!!!

    插入数据条id1成功!!!

    第三步:从HBase的命令行查看新建的表数据:

    第四部:从图形界面查看:

  • 相关阅读:
    路由器配置——基于链路的OSPF简单口令认证
    路由器配置——基于区域的OSPF,MD5认证
    将博客搬至CSDN
    爬虫入门【11】Pyspider框架入门—使用HTML和CSS选择器下载小说
    爬虫入门【10】Pyspider框架简介及安装说明
    【Python基础】装饰器的解释和用法
    爬虫入门【9】Python链接Excel操作详解-openpyxl库
    爬虫实战【13】获取自己的动态代理ip池
    爬虫实战【12】使用cookie登陆豆瓣电影以及获取单个电影的所有短评
    爬虫实战【11】Python获取豆瓣热门电影信息
  • 原文地址:https://www.cnblogs.com/taoweizhong/p/10526999.html
Copyright © 2020-2023  润新知