1.org.apache.hadoop.hbase.client.HBaseAdmin
HBaseAdmin类主要用于管理HBase数据库的表信息,包括创建或删除表、列出表项、使表有效或无效、添加或删除表的列族成员、检查HBase的运行状态等。HBaseAdmin类的主要方法如下所示。
2.org.apache.hadoop.hbase.HBaseConfiguration
该类主要用于管理HBase的配置信息。HBaseConfiguration主要方法如下表所示。
3. org.apache.hadoop.hbase.client.HTable
该类用于与HBase进行通信。如果多个线程对一个HTable对象进行put或者delete操作的话,则写缓冲器可能会崩溃。因此,在多线程环境下,建议使用HTablePool。HTable类的主要方法如下表所示。
4.org.apache.hadoop.hbase.HTableDescriptor
HTableDescriptor包含了HBase中表格的详细信息,如表中的列族、该表的类型(-ROOT-,.META.)、该表是否只读、MemStore的最大空间、Region什么时候应该分裂等。该类的主要方法如下表所示。
5.org.apache.hadoop.hbase.HColumnDescriptor
HColumnDescriptor包含了列族的详细信息,如列族的版本号、压缩设置等。HColumnDescriptor通常在添加列族或者创建表的时候使用。列族一旦建立就不能被修改,只能通过删除列族,再创建新的列族来间接修改列族。一旦列族被删除了,该列族包含的数据也随之被删除。HColumnDescriptor类主要方法如下表所示。
6.org.apache.hadoop.hbase.client.Put
该类用来对单元格执行添加数据操作。Put类的主要方法如下表所示。
7.org.apache.hadoop.hbase.client.Get
该类用来获取单行的信息。Get类的主要方法如下表所示。
8.org.apache.hadoop.hbase.client.Result
该类用于存放Get或Scan操作后的查询结果,并以
9.org.apache.hadoop.hbase.client.ResultScanner
该类为客户端获取值的接口,该接口的主要方法如下表所示。
10.org.apache.hadoop.hbase.client.Scan
可以利用Scan类来限定需要查找的数据,如限定版本号、起始行号、列族、列限定符、返回数量的上限等。该类的主要方法如下表所示。
本文的实践主要是建表,插入数据,删除数据,查询等功能。以下根据不同功能编写不同的类。目标就是建立一个如下所示的表。
name |
Info |
score |
|||
Id |
grade |
Math |
Chinese |
English |
|
Carson |
10101 |
4 |
97 |
90 |
80 |
Amy |
10102 |
3 |
90 |
70 |
89 |
Bob |
10103 |
5 |
95 |
99 |
86 |
Jason |
10104 |
3 |
88 |
93 |
95 |
具体实现类如下所示。
1.创建类,主要用于创建表。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;
/**
* @author carson0408
* 该类主要用于建表,涉及到配置的读取,以及建表
*/
public class CreateDemo {
/**
* 创建表
* @param tableName
* @param columnFamily
* @throws IOException
*/
public static void create(String tableName,String[] columnFamily) throws IOException {
//利用HBaseConfiguration的静态方法create(),生成Configuration对象
Configuration cfg = HBaseConfiguration.create();
//利用Configuration对象生成HBaseAdmin对象,主要用于管理HBase数据库的表信息,包括创建或删除表
HBaseAdmin admin = new HBaseAdmin(cfg);
//创建表,根据数据库的建表习惯,都是先判断表是否存在,如果存在先删除后建表,但是HBase中删除表之前需要使表先无效再删除
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
//创建表,HBaseAdmin对象的createTable(HTableDescriptor desc)方法创建表,输入参数是HTableDescriptor对象
//HTableDescriptor对象则是包含了HBase中表格的详细信息,通过tableName作为参数建立对象,并将列族添加进去。
//添加列族时通过HTableDescriptor对象的addFamily(HColumnDescriptor hcd)添加的
//HColumnDescriptor对象则是以列族名作为参数创建的。
HTableDescriptor htd = new HTableDescriptor(tableName);
for(String column:columnFamily){
htd.addFamily(new HColumnDescriptor(column));
}
admin.createTable(htd);
}
}
2.插入数据类
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import java.io.IOException;
public class InsertDemo {
/**
*
* @param tableName
* @param row
* @param columnFamily
* @param column
* @param data
* @throws IOException
*/
public static void insertData(String tableName,String row,String columnFamily,String column,String data) throws IOException {
Configuration cfg = HBaseConfiguration.create();
//HTable对象用于与HBase进行通信。
HTable table = new HTable(cfg,tableName);
//通过Put对象为已存在的表添加数据
Put put = new Put(row.getBytes());
if(column==null)//判断列限定符是否为空,如果为空,则直接添加列数据
put.add(columnFamily.getBytes(),null,data.getBytes());
else
put.add(columnFamily.getBytes(),column.getBytes(),data.getBytes());
//table对象的put输入参数是put对象,而put对象则表示每一单元格数据。
table.put(put);
}
}
3.查询数据类
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.HTable;
import org.apache.hadoop.hbase.client.Result;
import java.io.IOException;
/**
* 该类用于获取数据
*/
public class GetDemo {
public static void getData(String tableName,String row,String columnFamily,String column) throws IOException {
Configuration cfg = HBaseConfiguration.create();
HTable table = new HTable(cfg,tableName);
//根据行键建立Get对象
Get get = new Get(row.getBytes());
//通过get对象获取Result对象,Result对象表示一行的记录
Result result = table.get(get);
//再通过列族和列来确定单元格的数据。
System.out.println(new String(result.getValue(columnFamily.getBytes(),column==null?null:column.getBytes())));
}
}
4.删除数据类
5.测试类
a.首先建表,代码如下所示
import com.carson.HBase.create.CreateDemo;
import com.carson.HBase.insertData.InsertDemo;
import java.io.IOException;
public class TestDemo {
public static void main(String[] args) throws IOException {
//先创建一个scores的表,列族有info,score
String[] columnFamily = {"info","score"};
String tableName = "scores";
CreateDemo.create(tableName,columnFamily);
//插入数据
//插入Carson的信息和成绩
InsertDemo.insertData("scores","Carson","info","Id","10101");
InsertDemo.insertData("scores","Carson","info","grade","4");
InsertDemo.insertData("scores","Carson","score","Math","97");
InsertDemo.insertData("scores","Carson","score","Chinese","90");
InsertDemo.insertData("scores","Carson","score","English","80");
//插入Amy的信息和成绩
InsertDemo.insertData("scores","Amy","info","Id","10102");
InsertDemo.insertData("scores","Amy","info","grade","3");
InsertDemo.insertData("scores","Amy","score","Math","90");
InsertDemo.insertData("scores","Amy","score","Chinese","70");
InsertDemo.insertData("scores","Amy","score","English","89");
//Bob
InsertDemo.insertData("scores","Bob","info","Id","10103");
InsertDemo.insertData("scores","Bob","info","grade","5");
InsertDemo.insertData("scores","Bob","score","Math","95");
InsertDemo.insertData("scores","Bob","score","Chinese","99");
InsertDemo.insertData("scores","Bob","score","English","86");
//Jason
InsertDemo.insertData("scores","Jason","info","Id","10104");
InsertDemo.insertData("scores","Jason","info","grade","3");
InsertDemo.insertData("scores","Jason","score","Math","88");
InsertDemo.insertData("scores","Jason","score","Chinese","93");
InsertDemo.insertData("scores","Jason","score","English","95");
}
}
运行之后,在HBase shell中输入scan "scores",得到如下结果,可以看出,建立了之前说明的表,而且是根据行键的字典序排列的,与插入顺序无关。
b.查询数据
import com.carson.HBase.create.CreateDemo;
import com.carson.HBase.getData.GetDemo;
import com.carson.HBase.insertData.InsertDemo;
import java.io.IOException;
public class TestDemo {
public static void main(String[] args) throws IOException {
/**
//先创建一个scores的表,列族有info,score
String[] columnFamily = {"info","score"};
String tableName = "scores";
CreateDemo.create(tableName,columnFamily);
//插入数据
//插入Carson的信息和成绩
InsertDemo.insertData("scores","Carson","info","Id","10101");
InsertDemo.insertData("scores","Carson","info","grade","4");
InsertDemo.insertData("scores","Carson","score","Math","97");
InsertDemo.insertData("scores","Carson","score","Chinese","90");
InsertDemo.insertData("scores","Carson","score","English","80");
//插入Amy的信息和成绩
InsertDemo.insertData("scores","Amy","info","Id","10102");
InsertDemo.insertData("scores","Amy","info","grade","3");
InsertDemo.insertData("scores","Amy","score","Math","90");
InsertDemo.insertData("scores","Amy","score","Chinese","70");
InsertDemo.insertData("scores","Amy","score","English","89");
//Bob
InsertDemo.insertData("scores","Bob","info","Id","10103");
InsertDemo.insertData("scores","Bob","info","grade","5");
InsertDemo.insertData("scores","Bob","score","Math","95");
InsertDemo.insertData("scores","Bob","score","Chinese","99");
InsertDemo.insertData("scores","Bob","score","English","86");
//Jason
InsertDemo.insertData("scores","Jason","info","Id","10104");
InsertDemo.insertData("scores","Jason","info","grade","3");
InsertDemo.insertData("scores","Jason","score","Math","88");
InsertDemo.insertData("scores","Jason","score","Chinese","93");
InsertDemo.insertData("scores","Jason","score","English","95");
**/
//查询数据,查询Carson的语文成绩
String tableName = "scores";
String row = "Carson";
String columnFamily = "score";
String column = "Chinese";
GetDemo.getData(tableName,row,columnFamily,column);
}
}
查询结果如下:
c.删除数据
代码如下
import com.carson.HBase.create.CreateDemo;
import com.carson.HBase.deleteData.DeleteDemo;
import com.carson.HBase.getData.GetDemo;
import com.carson.HBase.insertData.InsertDemo;
import java.io.IOException;
public class TestDemo {
public static void main(String[] args) throws IOException {
/**
//先创建一个scores的表,列族有info,score
String[] columnFamily = {"info","score"};
String tableName = "scores";
CreateDemo.create(tableName,columnFamily);
//插入数据
//插入Carson的信息和成绩
InsertDemo.insertData("scores","Carson","info","Id","10101");
InsertDemo.insertData("scores","Carson","info","grade","4");
InsertDemo.insertData("scores","Carson","score","Math","97");
InsertDemo.insertData("scores","Carson","score","Chinese","90");
InsertDemo.insertData("scores","Carson","score","English","80");
//插入Amy的信息和成绩
InsertDemo.insertData("scores","Amy","info","Id","10102");
InsertDemo.insertData("scores","Amy","info","grade","3");
InsertDemo.insertData("scores","Amy","score","Math","90");
InsertDemo.insertData("scores","Amy","score","Chinese","70");
InsertDemo.insertData("scores","Amy","score","English","89");
//Bob
InsertDemo.insertData("scores","Bob","info","Id","10103");
InsertDemo.insertData("scores","Bob","info","grade","5");
InsertDemo.insertData("scores","Bob","score","Math","95");
InsertDemo.insertData("scores","Bob","score","Chinese","99");
InsertDemo.insertData("scores","Bob","score","English","86");
//Jason
InsertDemo.insertData("scores","Jason","info","Id","10104");
InsertDemo.insertData("scores","Jason","info","grade","3");
InsertDemo.insertData("scores","Jason","score","Math","88");
InsertDemo.insertData("scores","Jason","score","Chinese","93");
InsertDemo.insertData("scores","Jason","score","English","95");
**/
//查询数据,查询Carson的语文成绩
String tableName = "scores";
String row = "Carson";
String columnFamily = "score";
String column = "Chinese";
//GetDemo.getData(tableName,row,columnFamily,column);
//删除数据Carson的info列族中的grade
DeleteDemo.deleteData(tableName,row,"info","grade");
}
}
运行之后,在HBase shell中输入scan "scores",得到如下结果