使用Java *** 作HBase(增,删,改,查 *** 作)

使用Java *** 作HBase(增,删,改,查 *** 作),第1张

目录

在这里我用的是idea *** 作的

一、配置

1.首先确保HBase集群已经开启

2.配置hosts文件

 3、创建maven项目,在pom.xml文件中添加如下依赖项

二、连接增删改查

1.连接测试类

 2.创建表

3.添加数据


在这里我用的是idea *** 作的 一、配置 1.首先确保HBase集群已经开启
[root@hadoop ~]# jps
2761 HQuorumPeer
2137 ResourceManager
7114 Jps
2858 HMaster
1691 NameNode
1789 DataNode
2237 NodeManager
2943 HRegionServer
1983 SecondaryNameNode

上面所有节点都有

2.配置hosts文件

在你所在电脑配置你的hadoop集群信息,所以需要找到并修改hosts文件:

hosts的目录:C:\Windows\System32\drivers\etc

在这里我的伪分布式下的,修改hosts文件时需要打开所有权限,才能保存

使用记事本打开,修改后并保存

在文档最后一行加上IP空格主机名

任务管理器中输入ping 主机名

C:\Users\>ping hadoop
192.168.171.151 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 1ms,平均 = 0ms
 3、创建maven项目,在pom.xml文件中添加如下依赖项


    4.0.0

    com.lenovo
    0406
    1.0-SNAPSHOT

    
        8
        8
    

    
  
     jdk.tools   
     jdk.tools  
     1.8  
     system  
     C:/Program Files/Java/jdk1.8.0_91/lib/tools.jar  
  
  
  
   
     org.apache.hadoop
     hadoop-common
     2.7.3
 
  
   
     org.apache.hbase
     hbase-it
     1.3.1
     pom
 
    

二、连接增删改查 1.连接测试类
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Test;

import java.io.IOException;

    /**
     * @Date 2022.04.20
     * @AUthor lambda-小张
     * @Description 测试连接
     *单元测试
 */
public class Demo {
    @Test
    public void test(){
        //获取配置对象
        Configuration con = new Configuration();
        con.set("hbase.zookeeper.quorum","IP地址");
        //获取连接
        try {
            Connection conn = ConnectionFactory.createConnection(con);
            System.out.println(conn);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

运行结果

 2.创建表
/**
     * @Date 2022.04.20
     * @AUthor lambda-小张
     * @Description 创建表
     * Demo类名
     */
public static void main(String[] args) {
    new Demo().createTable("user","userinfo");
    }
public void createTable(String tableName,String familyName){
        //获取配置对象
        Configuration con = new Configuration();
        con.set("hbase.zookeeper.quorum","IP地址");
        Connection conn = null;
        Admin admin = null;
        //获取连接
        try {
            conn = ConnectionFactory.createConnection(con);
            //拿到admin
            admin = conn.getAdmin();
            //获取表格描述器
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //添加列簇
            hTableDescriptor.addFamily(new HColumnDescriptor(familyName));
            //执行创建 *** 作
            admin.createTable(hTableDescriptor);            
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {//关流
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

运行结果:

  

HBase中的结果:

hbase(main):001:0> list
TABLE                                                                                                                                                          
user                                                                                                                                                                  
1 row(s) in 0.3300 seconds
=> ["user"]

3.添加数据
  /**
     * @Date 2022.04.20
     * @AUthor lambda-小张
     * @Description 添加数据
     * Demo类名
     */
public static void main(String[] args) {
        boolean result = new Demo().addOneData("student", "admin", "123456");
        System.out.println(result);
    }
public boolean addOneData(String tableName,String userName,String passWord){
        //获取配置对象
        Configuration con = new Configuration();
        con.set("hbase.zookeeper.quorum","IP地址");
        Connection conn = null;
        Table table = null;
        //获取连接
        try {
            conn = ConnectionFactory.createConnection(con);
            //获取表的信息
            table = conn.getTable(TableName.valueOf(tableName));
            //设置行键
            Put put = new Put("1001".getBytes());
            //设置列的标识 以及列值
            put.addColumn("userinfo".getBytes(),"username".getBytes(),userName.getBytes());
            put.addColumn("userinfo".getBytes(),"password".getBytes(),passWord.getBytes());
            //执行添加
            table.put(put);
            //关流
            table.close();
            conn.close();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

运行结果:

  

HBase中的结果:

hbase(main):002:0> scan 'user'
ROW                    COLUMN+CELL                                                   
 1001                  column=userinfo:password, timestamp=1650436096526, value=123456
                                                                                    
 1001                  column=userinfo:username, timestamp=1650436096526, value=admin
1 row(s) in 0.2020 seconds
4.查询rowKey为1002的数据
  /**
     * @Date 2022.04.20
     * @AUthor lambda-小张
     * @Description 添加数据
     * Demo类名
     */
public static void main(String[] args) {
        new Demo().getAllData();
    }
public void getAllData(){
        //获取配置对象
        Configuration con= new Configuration();
        con.set("hbase.zookeeper.quorum","192.168.17.151");
        Connection conn = null;
        Table user = null;
        try {
            //获取连接
            conn = ConnectionFactory.createConnection(con);
            user = conn.getTable(TableName.valueOf("user"));
            //创建get
            Get get = new Get("1002".getBytes());
            //获取所有列
            Result result = user.get(get);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                //获取行键
                byte[] rowKey = result.getRow();
                String s = new String(rowKey);
                //获取列名
                byte[] qualifier = cell.getQualifier();
                String columnName = new String(qualifier);
                //获取值
                byte[] value = cell.getValue();
                String value2 = new String(value);
                System.out.println("行键:"+s+"列名:"+columnName+"值:"+value2);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                user.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

运行结果:

HBase中的结果:

hbase(main):002:0> get 'user','1002'
COLUMN                                   CELL                                                                                                               
 userinfo:password                       timestamp=1650441304272, value=222222                                                                              
 userinfo:username                       timestamp=1650441304272, value=zhangsan                                                                            
1 row(s) in 0.1430 seconds

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/716623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存