zoukankan      html  css  js  c++  java
  • intellij创建hbase工程

    打开intellj

    创建一个hbase Module模块

     右击Add_FrameWork_Support添加Maven依赖

    添加hbase的client包依赖,client的版本需要与实际的hbase版本一致。之后IDEA将会自动下载依赖包,可以在External Libraries中查看下载的依赖包。

    <dependencies>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>1.2.1</version>
            </dependency>                  
    </dependencies>

    等依赖下载好之后,在src/main/resources目录下新建一个hbase-site.xml文件,把hbase集群的配置文件内容粘贴过去。

    在src/test/java目录下新建一个java文件:

    package com.yzd.hbase;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    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;
    
    public class test {
        @Test
        public void put() throws Exception{
            //  创建conf对象
            Configuration configuration = HBaseConfiguration.create();
            //  通过连接工厂创建连接对象
            Connection conn = ConnectionFactory.createConnection(configuration);
            //  通过连接查询table对象
            TableName tname = TableName.valueOf("ns1:t1");
            //  获得表
            Table table = conn.getTable(tname);
    
            // 通过bytes类创建字节数组
            byte[] rowid = Bytes.toBytes("row3");
            //  创建put对象
            Put put = new Put(rowid);
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"), Bytes.toBytes(120));
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"),Bytes.toBytes("lucy"));
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("age"),Bytes.toBytes(20));
            put.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("hobby"),Bytes.toBytes("reading_books"));
            put.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("addr"),Bytes.toBytes("Beijing"));
    
            table.put(put);
            System.out.println("插入成功");
        }
    
        /*  查询数据  */
        @Test
        public void testGet() throws IOException {
            //  创建conf对象
            Configuration conf = HBaseConfiguration.create();
            //  通过连接工厂创建连接对象
            Connection conn = ConnectionFactory.createConnection(conf);
            //  创建TableName对象
            TableName tname = TableName.valueOf("ns1:t1");
            //  传入TableName对象获取表
            Table table = conn.getTable(tname);
            //  new一个get对象,传入rowkey
            Get get = new Get(Bytes.toBytes("row3"));
    
            //  获取数据,传入get对象
            Result result = table.get(get);
    
            byte[] id_value = result.getValue(Bytes.toBytes("f1"),Bytes.toBytes("id"));
            System.out.println(Bytes.toInt(id_value));
        }
    }

    然后运行testGet方法,输出如下信息:

  • 相关阅读:
    WyBox 7620a 启用第二个串口
    简书上关于spring boot不错的文章
    Springboot quartz集群(3) — 多节点发送邮件
    使用Gradle构建多模块SpringBoot项目
    SpringCloud的Ribbon自定义负载均衡算法
    Quartz和Spring Task定时任务的简单应用和比较
    zuul超时及重试配置
    spring cloud服务器启动之后立刻通过zuul访问其中的实例报zuul连接超时的问题
    com.netflix.zuul.exception.ZuulException:Forwarding error
    Maven项目:@Override is not allowed when implement interface method
  • 原文地址:https://www.cnblogs.com/lucas-zhao/p/11923328.html
Copyright © 2011-2022 走看看