一、

1.掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。

2.掌握安装和配置HBase基本方法。

3.掌握HBase shell的常用命令。

4.使用HBase shell命令进行表的创建,增加删除修改操作。

5.使用Java API进行表的创建,增加删除修改操作。

二、

1.完成Zookeeper和HBase的搭建。

2.使用HBase shell命令进行表的创建,增加删除修改操作。

3.使用Java API进行表的创建,增加删除修改操作。

一:

  1. 掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。
  1. 掌握安装和配置HBase基本方法。

Hbase的安装与配置已在实验一上传

  1. 掌握HBase shell的常用命令。

4.使用HBase shell命令进行表的创建,增加删除修改操作。

1. HBase Shell基本操作

(1)使用HBase Shell创建表、查看表结构、增加列族、删除列族;

(2)使用HBase Shell添加数据、查看数据、查看表中的记录总数;

(3)使用HBase Shell删除数据、清空表里的内容、删除表;

(4)使用HBase Shell对表的列族可配置参数进行调整,使其可以查看多个版本的数据。

status 查询服务器状态

version 查看版本

(1)使用HBase Shell创建表、查看表结构、增加列族、删除列族;

创建表

create ‘scores’,’grade’,’course’

查看表结构

desc ‘scores’

增加列族

disable ‘scores’

Alter ‘scores’,{NAME=>’school’}

删除列族(disable后alter)

disable ‘scores’

alter 'scores',{NAME=>'school',METHOD=>'delete'}

(2)使用HBase Shell添加数据、查看数据、查看表中的记录总数;

1、添加数据

put ‘表名’,’行键’,’列族:列限定符’,’值’

put 'scores','Tom','grade:','5'

put 'scores','Tom','course:math','97'

put 'scores','Tom','course:art','87'

put 'scores','Tom','course:english','80'

put 'scores','Jim','grade:','4'

put 'scores','Jim','course:chinese','89'

put 'scores','Jim','course:english','80'

2、查看数据

scan ‘scores’

3、查看表的记录总数

count ‘scores’

(3)使用HBase Shell删除数据、清空表里的内容、删除表;

1、删除数据

  1. 删除一个单元格

delete 'scores','Jim','grade:'

delete 'scores','Jim','course:chinese'

或:

deleteall 'scores','Tom','course:english'

b.删除一行

deleteall 'scores','Tom'

2、清空表内容

表的内容清空,但表的结构还在

truncate 'scores'

scan 'scores' 内容清空

desc 'scores' 结构存在

3、删除表

删除一个表(先禁用表,再删除表)

disable 'scores'

drop 'scores'

  1. 使用Java API进行表的创建,增加删除修改操作。

package putfile;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

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

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.KeyValue;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

import org.apache.hadoop.hbase.client.Delete;

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

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Column;

public class Hbase {

static Configuration conf = null;

static{

conf = HBaseConfiguration.create();

conf.set("hbase.rootdir", "hdfs://10.49.23.127:9000/hbase");

conf.set("hbase.master", "hdfs://10.49.23.127:60000");

conf.set("hbase.zookeeper.property.clientPort", "2181");

conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");

}

//实现了list功能

public static List<String> getAllTables() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

List<String> list = null;

//HBaseAdmin可以创建表、删除表、查看表

HBaseAdmin admin = new HBaseAdmin(conf);

if(admin != null){

//HTableDescriptor表的相关信息

HTableDescriptor[] tables = admin.listTables();

for(HTableDescriptor table : tables){

System.out.println(table.getNameAsString());

list = new ArrayList<String>();

list.add(table.getNameAsString());

}

}

admin.close();

return list;

}

public static int createTabble(String tableName, String[] family) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));

// HTableDescriptor table = new HTableDescriptor(tableName);

//HColumnDescriptor列的相关信息

for(String str : family){

HColumnDescriptor cloumn = new HColumnDescriptor(str);

cloumn.setMaxVersions(3);

table.addFamily(cloumn);

}

if(admin.tableExists(tableName)){

System.out.println(tableName + "已经存在");

return -1;

}

admin.createTable(table);

admin.close();

return 1;

}

public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

HBaseAdmin admin = new HBaseAdmin(conf);

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println(tableName + "成功删除");

admin.close();

}

public static void insert(String tableName, String rowkey, String family, String column, String cell) throws IOException{

Put put = new Put(rowkey.getBytes());

//HTable负责表的get put delete操作

HTable table = new HTable(conf, tableName);

put.add(family.getBytes(), column.getBytes(), cell.getBytes());

table.put(put);

}

public static Result queryByRow(String tableName, String rowkey) throws IOException{

Get get = new Get(rowkey.getBytes());

HTable table = new HTable(conf, tableName);

return table.get(get);

}

public static Result queryByColumn(String tableName, String rowkey, String family, String column) throws IOException{

Get get = new Get(rowkey.getBytes());

HTable table = new HTable(conf, tableName);

get.addColumn(family.getBytes(), column.getBytes());

return table.get(get);

}

public static Result queryByRowByVersions(String tableName, String rowkey) throws IOException{

Get get = new Get(rowkey.getBytes());

get.setMaxVersions(3);

HTable table = new HTable(conf, tableName);

return table.get(get);

}

public static ResultScanner queryByScan(String tableName) throws IOException{

Scan scan = new Scan();

// scan.setStartRow(startRow);

// scan.setStopRow(stopRow);

// scan.addColumn(family, qualifier);

HTable table = new HTable(conf, tableName);

return table.getScanner(scan);

}

public static void deleteByColumn(String tableName, String rowkey, String family, String column) throws IOException{

Delete  delete = new Delete(rowkey.getBytes());

HTable table = new HTable(conf, tableName);

delete.deleteColumn(family.getBytes(), column.getBytes());

table.delete(delete);

System.out.println("成功删除");

}

public static void deleteByRow(String tableName, String rowkey) throws IOException{

Delete  delete = new Delete(rowkey.getBytes());

HTable table = new HTable(conf, tableName);

table.delete(delete);

System.out.println("成功删除");

}

public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

getAllTables();

//1.创建book表

// createTabble("book", new String[]{"author", "info"});

// getAllTables();

//2.删除book表

// deleteTable("book");

// getAllTables();

//3.创建book表

// createTabble("book", new String[]{"author", "info"});

// getAllTables();

//4.book表插入数据

// insert("book", "rw001", "author", "name", "zhangsan");

// insert("book", "rw001", "author", "age", "65");

// insert("book", "rw001", "info", "name", "database");

// insert("book", "rw001", "info", "price", "35.6");

//5.按行查询数据(旧方法)

// Result result = queryByRow("book", "rw001");

// List<KeyValue> values = result.list();

// System.out.println("COLUMN\t\t\tCELL ");

// for(KeyValue value : values){

// System.out.print(new String(value.getFamily()) + ":");

// System.out.print(new String(value.getQualifier()) + "\t\t");

// System.out.print("value = " + new String(value.getValue()) + ",");

// System.out.println("timestamp = " + value.getTimestamp());

// }

//5.按行查询数据(新方法)

// Result result = queryByRow("book", "rw001");

// Cell[] cells = result.rawCells();

// System.out.println("COLUMN\t\t\tCELL ");

// for(Cell cell : cells){

// System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

// System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");

// System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ",");

// System.out.println("timestamp = " + cell.getTimestamp());

// }

//6.按列查询数据(新方法)

// Result result = queryByColumn("book", "rw001", "author", "name");

// Cell[] cells = result.rawCells();

// System.out.println("COLUMN\t\t\tCELL ");

// for(Cell cell : cells){

// System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

// System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");

// System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ",");

// System.out.println("timestamp = " + cell.getTimestamp());

// }

//7.scan

// ResultScanner scanner = queryByScan("book");

// System.out.println("COLUMN\t\t\tCELL ");

// for(Result result : scanner){

// Cell[] cells = result.rawCells();

//

// for(Cell cell : cells){

// System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

// System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");

// System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ",");

// System.out.println("timestamp = " + cell.getTimestamp());

// }

// }

//8.删除某一列

// deleteByColumn("book", "rw001", "author", "name");

//9.删除某一行数据

// deleteByRow("book", "rw001");

//10. 按行查询多版本数据(新方法)

// Result result = queryByRowByVersions("book", "rw001");

// Cell[] cells = result.rawCells();

// System.out.println("COLUMN\t\t\tCELL ");

// for(Cell cell : cells){

// System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

// System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");

// System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ",");

// System.out.println("timestamp = " + cell.getTimestamp());

// }

}

}

HBase环境搭建、shell操作及Java API编程的更多相关文章

  1. HDFS shell操作及HDFS Java API编程

    HDFS shell操作及HDFS Java API编程 1.熟悉Hadoop文件结构. 2.进行HDFS shell操作. 3.掌握通过Hadoop Java API对HDFS操作. 4.了解Had ...

  2. elasticsearch 5.0 获取 TransportClient 操作客户端java API

    本文转载自:http://blog.csdn.net/likui1314159/article/details/53233881 elasticsearch 5.0 获取 TransportClien ...

  3. 使用JAVA API编程实现简易Habse操作

    使用JAVA API编程实现下面内容: 1.创建<王者荣耀>游戏玩家信息表gamer,包含列族personalInfo(个人信息).recordInfo(战绩信息).assetsInfo( ...

  4. 利用SparkLauncher 类以JAVA API 编程的方式提交Spark job

    一.环境说明和使用软件的版本说明: hadoop-version:hadoop-2.9.0.tar.gz spark-version:spark-2.2.0-bin-hadoop2.7.tgz jav ...

  5. 大数据学习(16)—— HBase环境搭建和基本操作

    部署规划 HBase全称叫Hadoop Database,它的数据存储在HDFS上.我们的实验环境依然基于上个主题Hive的配置,参考大数据学习(11)-- Hive元数据服务模式搭建. 在此基础上, ...

  6. HBase学习(二) 基本命令 Java api

    一.Hbase shell 1.Region信息观察 创建表指定命名空间 在创建表的时候可以选择创建到bigdata17这个namespace中,如何实现呢? 使用这种格式即可:'命名空间名称:表名' ...

  7. 《OD大数据实战》HBase环境搭建

    一.环境搭建 1. 下载 hbase-0.98.6-cdh5.3.6.tar.gz 2. 解压 tar -zxvf hbase-0.98.6-cdh5.3.6.tar.gz -C /opt/modul ...

  8. 大数据学习系列之二 ----- HBase环境搭建(单机)

    引言 在上一篇中搭建了Hadoop的单机环境,这一篇则搭建HBase的单机环境 环境准备 1,服务器选择 阿里云服务器:入门型(按量付费) 操作系统:linux CentOS 6.8 Cpu:1核 内 ...

  9. Hadoop + ZK + HBase 环境搭建

    Hadoop 环境搭建 参考资料: http://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-common/ClusterSetu ...

随机推荐

  1. 将 Graphviz .dot 文件转换为其他格式的图像

    参考: Graphviz: How to go from .dot to a graph? 将 Graphviz .dot 文件转换为其他格式的图像 在Linux系统下,使用以下命令: dot -Tp ...

  2. Javascript 高级程序设计(第3版) - 第01章

    2017-05-10 js简介 一个叫“不难登”的人发明的.js的流行是因为 ajax 的关系. js分为三个部分: 核心: ECMAScript 文档对象模型: DOM 浏览器对象模型: BOM 核 ...

  3. Runnable、Callable、Executor、Future、FutureTask关系解读

    在再度温习Java5的并发编程的知识点时发现,首要的就是把Runnable.Callable.Executor.Future等的关系搞明白,遂有了下述小测试程序,通过这个例子上述三者的关系就一目了然了 ...

  4. sqlserver 中常见的函数 数学函数

    create table testnum( ID int identity(1,1), num float) insert testnum values (1) insert testnum valu ...

  5. php中if(\$a==\$b)和if(\$a=\$b)什么区别?

    <?php // if($a==$b)和if($a=$b)什么区别? $a = 1; $b = 1; if ($a == $b) { // 通过 echo '通过'.PHP_EOL; } if ...

  6. 设计模式(二)策略模式(Strategy)

    1.分析项目中变化部分与不变部分 2.多用组合,少用继承:用行为类组合,而不是行为的继承 案例: 第一步,把行为抽象为接口 package top.littlepage.designPattern.S ...

  7. django认证系统 Authentication

    Django自带一个用户认证系统,用于处理用户账户.群组.许可和基于cookie的用户会话. Django的认证系统包含了身份验证和权限管理两部分.简单地说,身份验证用于核实某个用户是否合法,权限管理 ...

  8. java正则常用记录

    1.   java中 字符串的某个字母是否有某个指定字符N  : for(int i=0;i<temp.length();i++){ if( temp.get(i).substring(0,1) ...

  9. 字符串--C++系列

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是 ...

  10. 牛客小白月赛7 CSL的校园卡

    CSL的校园卡 思路: bfs,用状压表示走过的区域,然后和x1,y1,x2,y2构成所有的状态,然后标记一下就可以了 代码: #pragma GCC optimize(2) #pragma GCC ...