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

文章目录

  • 引言
  • 0.HBase的启动与停止
  • 1.创建表
  • 2.删除表
  • 3.表的其他操作
    • 3.1 修改表结构
    • 3.2 查看表结构
    • 3.3 显示所有用户定义的表
    • 3.4 查询表是否存在
    • 3.5 查询表是否可用
    • 3.6 查询表中记录数
  • 4.插入数据
    • 4.1 插入单条数据
    • 4.2 插入多行数据
  • 5.删除数据
    • 5.1 删除单行数据
    • 5.2 删除表内所有数据
  • 6.修改数据
  • 7.查询数据
    • 7.1 查询单行数据
    • 7.2 查看指定时间戳范围的数据
    • 7.3 查询多行数据
  • 总结


引言

本篇文章主要记录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.IOException;
/*
指定行键
*/
public class GetRows {
    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);
        for (Cell cell : rs.rawCells()) {
            System.out.println("列族:"+new String(CellUtil.cloneFamily(cell)));
            System.out.println("列:"+new String(CellUtil.cloneQualifier(cell)));
            System.out.println("值:"+new String(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:"+cell.getTimestamp());
        }
        table.close();
    }
}

7.2 查看指定时间戳范围的数据

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

import org.apache.commons.lang.math.NumberUtils;
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.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class GetRowsByTimeStamp {
    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);

        Scan scan = new Scan();
        //指定时间戳范围
        scan.setTimeRange(NumberUtils.toLong("141387822100"), NumberUtils.toLong("1418627712342"));
        ResultScanner scanner = table.getScanner(scan);
        //得到指定时间戳范围内的若干行
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            int i = 0;
            int len = cells.length;
            //按行遍历
            System.out.println("行键:"+ Bytes.toString(CellUtil.cloneRow(cells[i])));
            for(;i<len;i++){
                System.out.println("列族:"+new String(CellUtil.cloneFamily(cells[i])));
                System.out.println("列:"+new String(CellUtil.cloneQualifier(cells[i])));
                System.out.println("值:"+new String(CellUtil.cloneValue(cells[i])));
                System.out.println("时间戳:"+cells[i].getTimestamp());
            }
            System.out.println();
        }
        table.close();
    }
}

7.3 查询多行数据

Shell指令:
全表扫描:scan 表名

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

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.IOException;

public class GetRows {
    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);
        for (Cell cell : rs.rawCells()) {
            System.out.println("列族:"+new String(CellUtil.cloneFamily(cell)));
            System.out.println("列:"+new String(CellUtil.cloneQualifier(cell)));
            System.out.println("值:"+new String(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:"+cell.getTimestamp());
        }
        table.close();
    }
}

总结

以上就是HBase的相关指令操作。

参考《Hadoop大数据原理与运用》徐鲁辉

你可能感兴趣的:(大数据学习笔记,hbase,hadoop,学习)