ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

intellij创建hbase工程

2019-11-24 17:56:20  阅读:335  来源: 互联网

标签:intellij 创建 Bytes TableName toBytes put import 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方法,输出如下信息:

 

标签:intellij,创建,Bytes,TableName,toBytes,put,import,hbase
来源: https://www.cnblogs.com/lucas-zhao/p/11923328.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有