常用HBase操作

  HBase是一个分布式、面向列的数据库,可以用来存储非结构化和半结构化的松散数据,具有高可靠、高性能、面向列、可伸缩的特性。作为一种键值数据库,通过行键(RowKey)、列族(ColumnFamily)、(Column)、时间戳(TimeTape)四个维度对数据进行定位。

首先启动Hadoop:切换目录,cd  /usr/local/hadoop,启动命令:  ./sbin/start-dfs.sh

    启动HBase:切换目录,cd  /usr/local/hbase,启动命令: ./bin/start-hbase.sh

(若配置了PATH环境变量,可直接输入start-dfs.shstart-hbase.sh 启动HBase)

Shell命令操作HBase

  进入Shell界面:bin/hbase  shell

  创建表:

    create  'Student' , 'name' , 'sex' , 'age' :student为表名,name、sex、age为列族

  查看表:

    list :列出HBase中有哪些表

    describe  ‘Student’ :查看表的具体信息

  添加数据:

    put  'Student' , '2017001' , 'name' , 'ZJ':向Student表添加学号为2017001,姓名为ZJ的一个数据,其行键为2017001。(HBase表中会有一个系统默认的属性作为行键,无需自行创建,默认为put命令操作中表名后的第一个数据。注意:一次只能为一个表的一行数据的一个列,也就是一个单元格添加一个数据所以直接用shell命令插入数据效率很低,一般通过编程操作数据)

  查看数据:

    scan  'Student':查看Student表的全部数据

    get  'Student' , '2017001':查看行键为2017001的一行数据

  删除数据:

    delete  'Student' ,'2017001' , 'name':删除Student表中行键为2017001的姓名(删除一个数据) 

    deleteall  'Student' , '2017001':删除一行数据

    truncate  'Student':清除Student表的全部数据

  删除表:有两步,第一步先让该表不可用,第二步删除表

    ①disable  'Student'      ②drop  'Student'

  退出对数据库表的操作:exit

Java API编程操作数据库

建立连接:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class Connect 
{
    public static Configuration conf;
    public static Connection conn;
    public Admin admin;
    public void init() throws IOException
    {
        conf=HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
        conn=ConnectionFactory.createConnection(conf);
        admin=conn.getAdmin();
    }
    public void close() throws IOException
    {
        if(admin!=null)
            admin.close();
        if(conn!=null)
            conn.close();
    }
}
Connect

创建表: (create

public void createTable() throws IOException
{
    init();
    TableName tName=TableName.valueOf(name);
    if(admin.tableExists(tName))
        System.out.println("table is exists!!!");
    else
    {
        HTableDescriptor descriptor=new HTableDescriptor(tName);
        for(String str:colFamily)
        {
            HColumnDescriptor col=new HColumnDescriptor(str);
            descriptor.addFamily(col);
        }
        admin.createTable(descriptor);
        System.out.println("Create table success!");
    }
    close();
}
createTable

 

查看Hbase中的表:list

public void list() throws IOException
{
    init();
    HTableDescriptor descriptors[]=admin.listTables();
    for(HTableDescriptor des:descriptors)
        System.out.println(des.getNameAsString());
    close();
}
list

添加一个数据:put

public void insertRow(String name,String rowKey,String colFamily, String col,String value) throws IOException
{
    init();
    Table table=conn.getTable(TableName.valueOf(name));
    Put put=new Put(rowKey.getBytes());
    put.addColumn(colFamily.getBytes(), col.getBytes(), value.getBytes());
    table.put(put);
    table.close();
    close();
}
insertRow

查看表中的全部数据:scan

public void getData(String name) throws IOException
{
    init();
    Table table=conn.getTable(TableName.valueOf(name));
    Scan scan=new Scan();
    ResultScanner scanner=table.getScanner(scan);
    for(Result result:scanner)
    {
        showCell(result);
        System.out.println();
    }
    table.close();
    close();
}
public void showCell(Result result)
{
    Cell[] cells=result.rawCells();
    for(Cell cell:cells)
    {
        System.out.println("rowKey(行键):"+new String(CellUtil.cloneRow(cell)));
        System.out.println("column Family(列族):"+new String(CellUtil.cloneFamily(cell)));
        System.out.println("column Name(列限定符):"+new String(CellUtil.cloneQualifier(cell)));
        System.out.println("Value(值):"+new String(CellUtil.cloneValue(cell)));
        System.out.println("timeTamp(时间戳):"+cell.getTimestamp());
    }
}
getData

删除表:disable、drop

public void deleteTable(String name) throws IOException
{
    init();
    TableName tName=TableName.valueOf(name);
    if(admin.tableExists(tName))
    {
        admin.disableTable(tName);
        admin.deleteTable(tName);
    }
    close();
}
deleteTable

猜你喜欢

转载自www.cnblogs.com/ZJdiem/p/11318794.html