Hadoop学习笔记--HBase相关操作指令

Posted 官乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hadoop学习笔记--HBase相关操作指令相关的知识,希望对你有一定的参考价值。

文章目录


引言

本篇文章主要记录Hadoop分布式数据库HBase相关操作,记录了相关操作的Shell指令和通过API进行操作的相关代码,便于日后查找。


0.HBase的启动与停止

Shell指令:
启动HDFS集群:start-dfs.sh
停止HDFS集群:stop-dfs.sh

启动HBase集群:start-hbase.sh
停止HBase集群:stop-hbase.sh

进入HBase命令行:bin/hbase shell (hbase目录下的bin目录)
退出shell:exit

1.创建表

Shell指令: CREATE 表名,列族1,列族2,…
例:create ‘student’,‘college’,‘profile’

API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes; 
import java.io.IOException;

/*
记录使用API创建表操作
 */

public class CreateTable 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        //创建命名空间
        hBaseAdmin.createNamespace(NamespaceDescriptor.create("my_ns").build());
        //表名:student
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("my_ns:student"));
        //添加列族->转化为字节数组
        HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("college"));
        hTableDescriptor.addFamily(hcd);
        hcd = new HColumnDescriptor(Bytes.toBytes("profile"));
        hTableDescriptor.addFamily(hcd);
        //创建表
        hBaseAdmin.createTable(hTableDescriptor);
        hBaseAdmin.close();
    

2.删除表

首先禁用表,再删除
Shell指令:
DISABLE 表名
DROP 表名
例:
disable ‘student’
drop ‘student’

API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;

/*
    记录删除表操作
 */

public class DeleteTable 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        if(hBaseAdmin.tableExists(tableName))
            //先禁用表
            hBaseAdmin.disableTable(tableName);
            //再删除表
            hBaseAdmin.deleteTable(tableName);
        
    

3.表的其他操作

3.1 修改表结构

首先设置表为不可用状态,再进行操作,最后设置为可用状态
Shell指令:
设置表为不可用状态: disable ‘student’
添加列族‘info’ 操作: alter ‘student’,name=>‘info’,versions=>5
删除列族‘info’操作: alter ‘student’,name=>‘info’,method=>‘delete’
启用表:enable ‘student’

3.2 查看表结构

Shell指令: describe ‘student’

3.3 显示所有用户定义的表

Shell指令: list

3.4 查询表是否存在

Shell指令: exists ‘student’

3.5 查询表是否可用

Shell指令: is_enabled ‘student’

3.6 查询表中记录数

Shell指令: count ‘student’

4.插入数据

4.1 插入单条数据

Shell指令:
PUT 表名,行键,列族:列名,值
例:
put ‘sudent’,‘19052002’,‘profile:height’,‘173’

API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class PutSingleRow 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        HTable table = new HTable(conf, tableName);
        
        //创建新put示例,由行键绑定,表示一个学生
        Put put = new Put(Bytes.toBytes("19052002"));
        //添加一个单元值,分别为列族,列名,值
        put.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
        put.add(Bytes.toBytes("profile"),Bytes.toBytes("name"),Bytes.toBytes("zhaosi"));
        table.put(put);
        table.close();
    

4.2 插入多行数据

API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

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

public class PutRows 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        HTable table = new HTable(conf, tableName);
        
        List<Put> listPuts = new ArrayList<Put>();
        //第一个对象
        Put put1 = new Put(Bytes.toBytes("19052002"));
        //添加一个单元值,分别为列族,列名,值
        put1.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
        listPuts.add(put1);
        
        //第二个对象
        Put put2 = new Put(Bytes.toBytes("19052003"));
        //添加一个单元值,分别为列族,列名,值
        put2.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
        listPuts.add(put2);
        
        //直接将list中的对象一次插入表中
        table.put(listPuts);
        table.close();
    

5.删除数据

5.1 删除单行数据

Shell指令:
delete 表名,行键,列族:列名
例:
delete ‘student’,‘19052006’,‘profile:height’
API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class DeleteSingleRow 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        HTable table = new HTable(conf, tableName);

		//指定行键删除
        Delete delete = new Delete(Bytes.toBytes("19052002"));
        table.delete(delete);
        table.close();
    

5.2 删除表内所有数据

Shell指令:
truncate 表名
例:
truncate ‘student’

6.修改数据

Shell指令:
put 表名,行键,列族:列名,新的值
例:
put ‘stuednt’,19052006’,‘profile:weight’,‘135’
API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class UpdateRow 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        HTable table = new HTable(conf, tableName);
        
        //指定行键
        Put put = new Put(Bytes.toBytes("19952003"));
        //列族,列名,新的值
        put.addColumn(Bytes.toBytes("college"), Bytes.toBytes("school"),Bytes.toBytes("Math"));
        put.addColumn(Bytes.toBytes("profile"), Bytes.toBytes("height"),Bytes.toBytes("182"));
        table.put(put);
        table.close();
    

7.查询数据

7.1 查询单行数据

Shell指令:
获取一个对象的数据:get 表名,行键
例:get ‘student’,‘19052003’

获取某行数据一个列族的所有数据:get 表名,行键,列族
例:get ‘student’,‘19052003’,‘profile’

获取某行数据一个列族中一个列的所有数据:get 表名,行键,列族:列名
例:get ‘student’,‘19052003’,‘profile:name’

API操作代码:
注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Result;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
/*
指定行键、列族、列
*/
public class GetRow 
    public static void main(String[] args) throws IOException 
        Configuration conf = HBaseConfiguration.create();
        //分布式场景下设置ZooKeeper地址
        conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        String tableName = "student";
        HTable table = new HTable(conf, tableName);

        //获取指定行键的数据
        Get get = new Get(Bytes.toBytes("19052003"));
        Result rs = table.get(get);
        //获取指定列族和列
        byte[] value = rs.getValue("profile".getBytes(), "name".getBytes());
        System.out.println("student: "+"19052003 "+"name "+Bytes.toString(value));

        table.close();
    

注意不要导错包!!!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Result;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.大数据学习笔记:利用JAVA项目操作HBase

Hadoop学习笔记—15.HBase框架学习(基础知识篇)

Hadoop学习笔记—15.HBase框架学习(基础实践篇)

学习HBase笔记

hadoop-hbase学习笔记

从零自学Hadoop(21):HBase数据模型相关操作下