首页 > 编程语言 >hbase - [04] java访问hbase

hbase - [04] java访问hbase

时间:2024-03-28 14:34:59浏览次数:30  
标签:UTF String 04 cf table java hbase null getBytes

 

需要导入jar包

  • $HBASE_HOME/lib下的所有jar包
  • $HADOOP_HOME/share/hadoop/common的所有jar包

 

package com.harley.hbase.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class HbaseTest{

    protected Connection connection = null;
    protected Admin admin = null;

    /**
     * 创建表所需参数
     */
    protected String tableName = "mydata:psn2";
    protected String cf = "cf";

    @Before
    public void init()
    {
        System.out.println("====init....====");
        System.setProperty("hadoop.home.dir","D:/Java/bigdata/bigdata/hadoop-3.2.1");
        try {
            Configuration conf = new Configuration();
            conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * get
     */
    @Test
    public void get()
    {
        Table table = null;

        try {
            table = this.connection.getTable(TableName.valueOf(this.tableName));
            /**
             * 参数为 rowkey
             */
            Get get = new Get("student".getBytes("UTF-8"));
            Result result = table.get(get);

            String rowkey = new String(result.getRow(),"UTF-8");
            String name = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"name".getBytes("UTF")).getValueArray());
            String age = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"age".getBytes("UTF")).getValueArray());
            String gender = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF")).getValueArray());
            System.out.println("---rowkey--->"+rowkey+"----name--->"+name+"---age---->"+age+"----gender--->"+gender);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if(table!=null){
                    table.close();
                    table=null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    /**
     * scan
     */
    @Test
    public void scan()
    {
        Table table = null;
        try {
            table = this.connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner scanner = table.getScanner(scan);

            int count = 1;

            for (Result result : scanner) {
                String rowkey = new String(result.getRow(), "UTF-8");
                String name = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8")).getValueArray());
                String age = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8")).toString());
                String gender = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8")).toString());

                System.out.println("--条数:"+count+"---rowkey=>"+rowkey+"----name=>"+name+"----age=>"+age+"----gender=>"+gender);
                count++;
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(table!=null){
                    table.close();
                    table=null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * put
     */
    @Test
    public void put()
    {
        Table table = null;
        try {
            table = this.connection.getTable(TableName.valueOf(tableName));
            /**
             * rowkey
             */
            Put put = new Put("student01".getBytes("UTF-8"));

            /**
             * 添加键和值
             */
//            put.addColumn(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8"),"老白".getBytes("UTF-8"));
//            put.addColumn(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8"),"32".getBytes("UTF-8"));
//            put.addColumn(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8"),"男".getBytes("UTF-8"));

            put.addColumn(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8"),"Tom".getBytes("UTF-8"));
            put.addColumn(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8"),"20".getBytes("UTF-8"));
            put.addColumn(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8"),"man".getBytes("UTF-8"));


            /**
             * 相当于事务中的commit
             */
            table.put(put);
            System.out.println("————————put success——————");



        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if(table!=null)
                table.close();
                table=null;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    /**
     * create
     */
    @Test
    public void create()
    {
        System.out.println("====create....====");
        try {
            TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
            ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(this.cf.getBytes("UTF-8")).build();
            TableDescriptor tableDescriptor = tdb.setColumnFamily(columnFamilyDescriptor).build();
            this.admin.createTable(tableDescriptor);
            System.out.println("-------create success-------");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * list_namespace
     */
    @Test
    public void list_namespace()
    {

        try {
            NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

            int count = 1;

            for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {

                System.out.println(count+"----------"+namespaceDescriptor.getName());
                count++;
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * list
     */
    @Test
    public void list()
    {
        System.out.println("——————————list....———————————");

        try {

            List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
            int count=1;
            for (TableDescriptor tableDescriptor : tableDescriptors) {

                System.out.println(count+"====="+tableDescriptor.getTableName());
                count++;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @After
    public void after()
    {
        System.out.println("====destroy....====");
        try {
            if(admin!=null){
                admin.close();
                admin=null;
            }
            if(connection!=null)
            {
                connection.close();
                connection=null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

 

 

— 要养成终生学习的习惯 —

标签:UTF,String,04,cf,table,java,hbase,null,getBytes
From: https://www.cnblogs.com/houhuilinblogs/p/18101602

相关文章

  • 前端学习-UI框架学习-Bootstrap5-004-文字排版
    菜鸟教程链接Bootstrap5默认设置Bootstrap5默认的font-size为16px,line-height为1.5默认的font-family为"HelveticaNeue",Helvetica,Arial,sans-serif此外,所有的元素margin-top:0、margin-bottom:1rem(16px)h1-h6<template><divclass="con......
  • hbase - [03] 客户端常用命令(hbase shell)
     1、列出所有namespacelist_namespace2、创建namespacecreate_namespace'ns_name'3、修改namespace属性alter_namespace'nsname',{METHOD=>'set','PROPERTY_NAME'=>'PROPERTY_VALUE'}4、删除namespace属性alter_namesp......
  • hbase - [02] 分布式安装部署
      一、角色规划主机名node01node02node03node04Zookeeper○○○ NameNode○○  JournalNode○○○ DataNode○○○○ResourceManager○○  NodeManager○○○○Master○○○ RegionServer○○○○......
  • JavaWeb学习笔记——第七天
    MySQL(二)数据库查询-DQLDQL英文全称是DataQueryLanguage(数据查询语言),用来查询数据库表中的记录。关键字:SELECT语法关键字和参数说明select字段列表基本查询from表名列表where条件列表条件查询groupby分组字段列表分组查询having分组后条件列......
  • Java的Collections工具类
    Java的Collections工具类位于java.util包中,提供了一系列静态方法用于操作集合(List、Set、Map等)。以下是Collections类中一些常用方法的列表和使用实例:常用方法列表sort(List<T>list):对列表进行升序排序。sort(List<T>list,Comparator<?superT>c):根据指定的比较器对列表......
  • ubuntu16.04server版本安装网卡
    设置用户开机进入BIOS里取消勾选SecureBootEnableci30#输入用户名和密码登陆sudopasswdroot#如果是首次登陆,需要设置root密码(密码统一设置123456)su#切换root用户,输入root用户密码安装make工具:fdisk-l#查看安装磁盘为/dev/sdb1mount/dev/sdb1/opt......
  • Java基础语法(五)| 方法
    1.方法概述1.1什么是方法方法(method)完成某一个特定功能的代码块。1.2方法基本使用将资料中给大家提供的打怪物发射炮弹重复的代码,把一些重复代码进行抽取(封闭)思想. 调用格式方法名();注意:1.方法必须先创建才可以使用,该过程成为方法定义。2.方法创建后并......
  • COMP2045Goofspiel游戏的程序实现
    COMP2045课程2024介绍这门课相当于模块分数的40%。它要求你写C++程序和报告,以解决下面描述的任务。这个的最后期限演习时间为2024年4月29日星期一晚上23:55。在开始练习之前,请阅读整个文档。如果你对这个练习有任何问题,请在问答论坛上提问Moodle,在讲座后,在实验室,或在广告中的办......
  • L2-044 大众情人
    测试点4一开始没过去,以为是数据范围的问题。找半天才发现是floyd三层循环k那个写反了,害。#include<bits/stdc++.h>usingnamespacestd;vector<int>male,female;intdis[510][510];intmain(){ memset(dis,0x3f,sizeof(dis)); intn; cin>>n; for(inti=1;i......
  • 安装 allure 时踩到的坑:ERROR: JAVA_HOME is set to an invalid directory
    报错内容是:安装allure时ERROR:JAVA_HOMEissettoaninvaliddirectory:F:\soft\jdkPleasesettheJAVA_HOMEvariableinyourenvironmenttomatchthelocationofyourJavainstallation。这个问题的原因是java环境的环境变量不能运行也就是在cmd中javac这......