
Hbase的实践操作(虚拟机Linux系统)
(一).Hbase的shell操作1.进入hbase shell在启动HDFS和Hbase之后,在Linux客户端输入“hbase shell”命令将进入Hbase Shell。打开Hbase Shell之后,首先输入“help”命令,Hbase Shell会显示Hbase所提供的所有Shell命令。2.创建、查看、删除命名空间建立一个test的命名空间,具体的命令如下通过如下describe命令
(一).Hbase的shell操作
1.进入hbase shell
在启动HDFS和Hbase之后,在Linux客户端输入“hbase shell”命令将进入Hbase Shell。
打开Hbase Shell之后,首先输入“help”命令,Hbase Shell会显示Hbase所提供的所有Shell命令。
2.创建、查看、删除命名空间
建立一个test的命名空间,具体的命令如下
通过如下describe命令来查看所建立的命名空间的详细信息
我们也可以通过如下命令来查看当前Hbase中所有的命名空间
Default:默认的命名空间
Hbase:系统命名空间,包含了所有内部表
通过drop_namespace ‘test’命令来删除一个命名空间,删除之前不需要将其Disabled
3.创建、查看、删除表以及使表有效和无效
建立一个usr_beha表,有两个列族,一个是beha,一个是attr,具体的命令如下
Describe ‘usr_beha’ 查看表的信息。Enabled表示当我们创建一个表之后,该表已经enabled
describe命令用于显示一个表的结构与设置信息
可以让我们看到关于一个表的一些默认的设置
删除一个表使用drop命令,比如删除我们创建的usr_beha表,具体的命令格式如下:
在创建表时指定表所属的命名空间,则可以使用如下命令
因为test命名空间已经删掉,所以要重新创建
查看一个命名空间下的所有表格信息,可以使用list_namespace_tables ‘test命令
4.添加、获取、删除单元格中的数据
首先创建usr__beha表
向表usr_beha中写入ID为38932423的用户张三的姓名
通过如下命令来查看刚刚写入的数据
通过scan命令来查看整个表的信息
删除上述单元格的数据,则可以使用delete命令
5,修改表的结构
在刚创建的表usr_beha中添加一个新的列族ecf1
用alter命令
删除一个表中的列族
用alter,METHOD=>’delete’
当添加或删除一个新的列族时,Hbase要更新所有的Region
6.退出Hbase shell
退出Hbase Shell使用exit命令
7. 查看Hbase集群的状态
使用status命令,并可以根据summary、simple和detailed这几个关键词来获取不同详细程度的状态信息
状态:
(二).Hbase的java 接口
示例说明:
视频网站记录用户行为数据的任务。
基于maven构建一个项目来展示如何基于Java API操作Hbase。
建立一个usr_beha的表来存储用户在视频网站上的各种行为数据,这个表包含了两个列族:beha和attr,而每个列族下只有一个列。
Beha列族包含了代表用户行为的各个列,下面将只有一个列watch来表示用户的观影记录。
attr包含了代表用户属性的各个列,下面将只有一个列username来表示用户的姓名。
Pom.xml文件
- User类文件
封装用户行为和属性数据的User类文件:
public class User {
private String id=null;
private String username=null;
private String watch=null;
public User(String id, String username, String watch) {
this.id = id;
this.username = username;
this.watch = watch;
}
public User(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getWatch() {
return watch;
}
public void setWatch(String watch) {
this.watch = watch;
}
//这个方法是将user对象的信息打印出来,方便查看
public String toString() {
return "User_info:" + "id='" + id + '\'' +
", username='" + username + '\'' +
", watch='" + watch + '\'' ;
}
}
HbaseApp类文件
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
public class HbaseApp {
public static Admin admin;//admin对象
public static Connection connection;//连接hbase的对象
//main函数
public static void main(String[] args) {
init();//建立连接
try {
createTable("usr_beha", new String[] {"beha","attr"});//建表
User user1=new User("38932423","zhangsan","356");
insertData("usr_beha",user1);//插入数据
User user2=new User("34234278","lisi","237");
insertData("usr_beha",user2); //插入数据
List<User> list = getAllData("usr_beha");//查询数据
for (User user : list){
System.out.println(user.toString());//打印输出
}
User user3 = getDataByRowKey("usr_beha", "38932423");//根据行键查询
System.out.println(user3.toString());
//获取某单元格的数据
String username=getDatafromCell("usr_beha", "38932423","attr","username");
System.out.println("username: "+username);
//删除某单元格的数据
deleteDataofCell("usr_beha","38932423","attr","username");
//查看删除之后的结果
User user4 = getDataByRowKey("usr_beha", "38932423");
System.out.println(user4.toString());
} catch (IOException e) {
e.printStackTrace();
}
close()//关闭连接
}
//初始化,建立与hbase的连接,并基于所建立的连接获取admin对象
public static void init(){
Configuration configuration = HBaseConfiguration.create();
//设置hbase.rootdir目录,这个目录是用来持久化hbase的目录
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
//获取admin对象
admin=connection.getAdmin();
}catch(IOException e){
e.printStackTrace();
}
}
//根据输入的列族名字的集合,创建表
public static void createTable(String strTableName, String[] strColFamilies)
throws IOException{
TableName tableName = TableName.valueOf(strTableName);
//首先通过admin对象判断同名的表是否已经存在
if (admin.tableExists(tableName)) {
System.out.println(strTableName+" exists!");
} else {
//创建一个封装表的描述信息的对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
//创建封装表列族描述信息的对象,并将它们添加到HtableDescriptor对象中
for (String col : strColFamilies) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
hTableDescriptor.addFamily(hColumnDescriptor);
}
//基于HtableDescriptor对象,通过admin对象的create方法来创建表
admin.createTable(hTableDescriptor);
}
}
//插入数据,数据封装在User对象中
public static void insertData(String tableName, User user) throws IOException {
//根据表名获取表的实例
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(user.getId().getBytes());//用户ID作为行键,创建一个Put对象
//如下为将User对象中的信息写入到对应列族、列下的单元格中
//可以将同一个行中多个单元格的写入操作都封装到同一个Put对象中
put.addColumn("attr".getBytes(),"username".getBytes(),user.getUsername()
.getBytes());
put.addColumn("beha".getBytes(),"watch".getBytes(),user.getWatch().getBytes());
table.put(put);//调用table对象的put方法来完全写入数据
}
//根据行键来查询一行数据,这里返回的数据通过User对象进行了封装
public static User getDataByRowKey(String tableName, String rowKey)
throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));//获取表的实例
Get get = new Get(rowKey.getBytes());//基于行键,创建一个Get对象
User user = new User();
user.setId(rowKey);
Result result = table.get(get);//通过table对象的get方法来获取数据
for (Cell cell : result.rawCells()){//result对象包含了所有单元格的数据
String colName = Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength());
if(colName.equals("username")){//将读取的数据转成String赋值给user对象
user.setUsername(value);
}
if (colName.equals("watch")){
user.setWatch(value);
}
}
return user;
}
//给定行键、列族和列的名字,从一个单元格中获取数据
public static String getDatafromCell(String tableName, String rowKey,
String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));//获取表的实例
Get get = new Get(rowKey.getBytes());//基于行键,创建一个Get对象
get.addColumn(colFamily.getBytes(),col.getBytes());//设置单元格列族和列的名字
if (table.exists(get)){//通过表的实例判断将要读取的数据是否存在
Result result=table.get(get);
String strResult=new
String(result.getValue(colFamily.getBytes(),col.getBytes()));
return strResult;
}else{
System.out.print("The cell is empty!");
return null;
}
}
//查询指定表名中所有的数据
public static List<User> getAllData(String tableName){
Table table = null;
List<User> list = new ArrayList<User>();
try {
table = connection.getTable(TableName.valueOf(tableName));
ResultScanner results = table.getScanner(new Scan());//创建了一个scan对象
User user = null;
for (Result result : results){//从结果中读取数据,并封装在user对象中
String id = new String(result.getRow());
user = new User();
for(Cell cell : result.rawCells()){
//注意从Result中读取一个单元格的行键、列族、列和值所用的方法是不同的
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(),
cell.getRowLength());
String family = Bytes.toString(cell.getFamilyArray(),
cell.getFamilyOffset(),
cell.getFamilyLength());
String colName = Bytes.toString(cell.getQualifierArray(),
cell.getQualifierOffset(),
cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(),
cell.getValueOffset(),
cell.getValueLength());
user.setId(row);
if(colName.equals("username")){
user.setUsername(value);
}
if (colName.equals("watch")){
user.setWatch(value);
}
}
list.add(user);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//给定行键、列族和列的名字,删除一个单元格中的数据
public static void deleteDataofCell(String tableName,String rowKey,
String colFamily, String col) throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));//获取表的实例
Delete delete = new Delete(rowKey.getBytes());//根据行键,创建一个delete对象
//设置删除单元格的列族和列的名字
delete.addColumns(colFamily.getBytes(), col.getBytes());
table.delete(delete);//调用table实例的delete方法删除数据
}
//删除一个表
public static void deleteTable(String tableName){
try {
TableName tablename = TableName.valueOf(tableName);
admin.disableTable(tablename);//通过admin对象先使表失效
admin.deleteTable(tablename);//通过admin对象删除表
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if (admin!=null){
admin.close();//调用admin实例的close方法关闭
}
if (connection != null){
connection.close();//调用connection实例的close方法关闭
}
}catch(IOException e){
e.printStackTrace();
}
}
}
- 运行截图
在控制端用list和scan 命令查看结果。
在localhost:50070查看:
出现的问题:
第一次运行程序时,报错显示usr_beha表已经存在,需要把原先建立的usr_beha表删除后,重新运行。
更多推荐
所有评论(0)