HBASE实例开发学习

1、开发环境

在进行Hbase开发前,需要安装JDK、Hadoop和HBase

注:以下代码中的版本信息需根据自己的安装环境进行修改。

 

使用Maven构建项目,在pom.xml中添加hbase的依赖如下:

  1.   <repositories>  
  2.  
  3.       <repository>  
  4.         <id>cloudera</id>  
  5.         <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>  
  6.       </repository>  
  7.   </repositories>  
  8.   
  9.   <dependencies>  
  10.       <dependency>  
  11.           <groupId>junit</groupId>  
  12.           <artifactId>junit</artifactId>  
  13.           <version>3.8.1</version>  
  14.           <scope>test</scope>  
  15.       </dependency>  
  16.       <dependency>    
  17.           <groupId>org.apache.hadoop</groupId>    
  18.           <artifactId>hadoop-common</artifactId>    
  19.           <version>2.6.0-cdh5.7.1</version>    
  20.       </dependency>    
  21.       <dependency>    
  22.           <groupId>org.apache.hadoop</groupId>    
  23.           <artifactId>hadoop-hdfs</artifactId>    
  24.           <version>2.6.0-cdh5.7.1</version>    
  25.       </dependency>  
  26.       <dependency>    
  27.           <groupId>org.apache.hbase</groupId>    
  28.           <artifactId>hbase-client</artifactId>    
  29.           <version>1.2.0-cdh5.7.1</version>    
  30.       </dependency>  
  31. <dependency>    
  32.           <groupId>org.apache.hbase</groupId>    
  33.           <artifactId>hbase-server</artifactId>    
  34.           <version>1.2.0-cdh5.7.1</version>    
  35.       </dependency>  
  36.   </dependencies>  
  37.  ​

2、初始化配置

首先需要设置HBase的配置,如ZooKeeper的地址、端口号等等。可以通过org.apache.hadoop.conf.Configuration.set方法手工设置HBase的配置信息,也可以直接将HBase的hbase-site.xml配置文件引入项目即可。下面给出配置代码:

  1. // 声明静态配置  
  2.  
  3.   private static Configuration conf = null;  
  4.   static {  
  5.       conf = HBaseConfiguration.create();  
  6.       conf.set("hbase.zookeeper.quorum", "localhost");  
  7.       conf.set("hbase.zookeeper.property.clientPort", "2181");  
  8.   }  
  9. 

3、常见API的使用

HBase的常用操作包括建表、插入表数据、删除表数据、获取一行数据、表扫描、删除列族、删除表等等,下面给出具体代码。

3.1 创建数据库表

  1.  // 创建数据库表  
  2. public static void createTable(String tableName, String[] columnFamilys) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 创建一个数据库管理员  
  6.     HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();  
  7.     if (hAdmin.tableExists(tableName)) {  
  8.         System.out.println(tableName + "表已存在");  
  9.         conn.close();  
  10.         System.exit(0);  
  11.     } else {  
  12.         // 新建一个表描述  
  13.         HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));  
  14.         // 在表描述里添加列族  
  15.         for (String columnFamily : columnFamilys) {  
  16.             tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
  17.         }  
  18.         // 根据配置好的表描述建表  
  19.         hAdmin.createTable(tableDesc);  
  20.         System.out.println("创建" + tableName + "表成功");  
  21.     }  
  22.     conn.close();  
  23. }  
  24. 

3.2 添加一条数据

  1.  // 添加一条数据  
  2. public static void addRow(String tableName, String rowKey, String columnFamily, String column, String value)   
  3.         throws IOException {  
  4.     // 建立一个数据库的连接  
  5.     Connection conn = ConnectionFactory.createConnection(conf);  
  6.     // 获取表  
  7.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  8.     // 通过rowkey创建一个put对象  
  9.     Put put = new Put(Bytes.toBytes(rowKey));  
  10.     // 在put对象中设置列族、列、值  
  11.     put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));  
  12.     // 插入数据,可通过put(List<Put>)批量插入  
  13.     table.put(put);  
  14.     // 关闭资源  
  15.     table.close();  
  16.     conn.close();  
  17. }  
  18. 

3.3 获取一条数

  1. // 通过rowkey获取一条数据  
  2. public static void getRow(String tableName, String rowKey) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 获取表  
  6.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  7.     // 通过rowkey创建一个get对象  
  8.     Get get = new Get(Bytes.toBytes(rowKey));  
  9.     // 输出结果  
  10.     Result result = table.get(get);  
  11.     for (Cell cell : result.rawCells()) {  
  12.         System.out.println(  
  13.                 "行键:" + new String(CellUtil.cloneRow(cell)) + "\t" +  
  14.                 "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" +   
  15.                 "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +   
  16.                 "值:" + new String(CellUtil.cloneValue(cell)) + "\t" +  
  17.                 "时间戳:" + cell.getTimestamp());  
  18.     }  
  19.     // 关闭资源  
  20.     table.close();  
  21.     conn.close();  
  22. }  
  23. 

3.4 全表扫描

  1. // 全表扫描  
  2.     public static void scanTable(String tableName) throws IOException {  
  3.         // 建立一个数据库的连接  
  4.         Connection conn = ConnectionFactory.createConnection(conf);  
  5.         // 获取表  
  6.         HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  7.         // 创建一个扫描对象  
  8.         Scan scan = new Scan();  
  9.         // 扫描全表输出结果  
  10.         ResultScanner results = table.getScanner(scan);  
  11.         for (Result result : results) {  
  12.             for (Cell cell : result.rawCells()) {  
  13.                 System.out.println(  
  14.                         "行键:" + new String(CellUtil.cloneRow(cell)) + "\t" +  
  15.                         "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" +   
  16.                         "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +   
  17.                         "值:" + new String(CellUtil.cloneValue(cell)) + "\t" +  
  18.                         "时间戳:" + cell.getTimestamp());  
  19.             }  
  20.         }  
  21.         // 关闭资源  
  22.         results.close();  
  23.         table.close();  
  24.         conn.close();  
  25. }  
  26. 

3.5 删除一条数据

  1. // 删除一条数据  
  2. public static void delRow(String tableName, String rowKey) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 获取表  
  6.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  7.     // 删除数据  
  8.     Delete delete = new Delete(Bytes.toBytes(rowKey));  
  9.     table.delete(delete);  
  10.     // 关闭资源  
  11.     table.close();  
  12.     conn.close();  
  13. }  
  14. 

3.6 删除多条数据

  1. // 删除多条数据  
  2. public static void delRows(String tableName, String[] rows) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 获取表  
  6.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  7.     // 删除多条数据  
  8.     List<Delete> list = new ArrayList<Delete>();  
  9.     for (String row : rows) {  
  10.         Delete delete = new Delete(Bytes.toBytes(row));  
  11.         list.add(delete);  
  12.     }  
  13.     table.delete(list);  
  14.     // 关闭资源  
  15.     table.close();  
  16.     conn.close();  
  17. }  
  18. 

3.7 删除列族

  1. // 删除列族  
  2. public static void delColumnFamily(String tableName, String columnFamily) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 创建一个数据库管理员  
  6.     HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();  
  7.     // 删除一个表的指定列族  
  8.     hAdmin.deleteColumn(tableName, columnFamily);  
  9.     // 关闭资源  
  10.     conn.close();  
  11. }  
  12. 

3.8 删除数据库表

  1. // 删除数据库表  
  2. public static void deleteTable(String tableName) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 创建一个数据库管理员  
  6.     HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();  
  7.     if (hAdmin.tableExists(tableName)) {  
  8.         // 失效表  
  9.         hAdmin.disableTable(tableName);  
  10.         // 删除表  
  11.         hAdmin.deleteTable(tableName);  
  12.         System.out.println("删除" + tableName + "表成功");  
  13.         conn.close();  
  14.     } else {  
  15.         System.out.println("需要删除的" + tableName + "表不存在");  
  16.         conn.close();  
  17.         System.exit(0);  
  18.     }  
  19. }  
  20. 

3.9 追加插入

  1. // 追加插入(将原有value的后面追加新的value,如原有value=a追加value=bc则最后的value=abc)  
  2. public static void appendData(String tableName, String rowKey, String columnFamily, String column, String value)   
  3.         throws IOException {  
  4.     // 建立一个数据库的连接  
  5.     Connection conn = ConnectionFactory.createConnection(conf);  
  6.     // 获取表  
  7.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  8.     // 通过rowkey创建一个append对象  
  9.     Append append = new Append(Bytes.toBytes(rowKey));  
  10.     // 在append对象中设置列族、列、值  
  11.     append.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));  
  12.     // 追加数据  
  13.     table.append(append);  
  14.     // 关闭资源  
  15.     table.close();  
  16.     conn.close();  
  17. }  
  18. 

3.10 符合条件后添加数据

  1. // 符合条件后添加数据(只能针对某一个rowkey进行原子操作)  
  2. public static boolean checkAndPut(String tableName, String rowKey, String columnFamilyCheck, String columnCheck, String valueCheck, String columnFamily, String column, String value) throws IOException {  
  3.     // 建立一个数据库的连接  
  4.     Connection conn = ConnectionFactory.createConnection(conf);  
  5.     // 获取表  
  6.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  7.     // 设置需要添加的数据  
  8.     Put put = new Put(Bytes.toBytes(rowKey));  
  9.     put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));  
  10.     // 当判断条件为真时添加数据  
  11.     boolean result = table.checkAndPut(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamilyCheck),   
  12.             Bytes.toBytes(columnCheck), Bytes.toBytes(valueCheck), put);  
  13.     // 关闭资源  
  14.     table.close();  
  15.     conn.close();  
  16.       
  17.     return result;  
  18. }  
  19. 

3.11 符合条件后删除数据

  1. // 符合条件后刪除数据(只能针对某一个rowkey进行原子操作)  
  2. public static boolean checkAndDelete(String tableName, String rowKey, String columnFamilyCheck, String columnCheck,   
  3.         String valueCheck, String columnFamily, String column) throws IOException {  
  4.     // 建立一个数据库的连接  
  5.     Connection conn = ConnectionFactory.createConnection(conf);  
  6.     // 获取表  
  7.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  8.     // 设置需要刪除的delete对象  
  9.     Delete delete = new Delete(Bytes.toBytes(rowKey));  
  10.     delete.addColumn(Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck));  
  11.     // 当判断条件为真时添加数据  
  12.     boolean result = table.checkAndDelete(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamilyCheck), Bytes.toBytes(columnCheck),   
  13.             Bytes.toBytes(valueCheck), delete);  
  14.     // 关闭资源  
  15.     table.close();  
  16.     conn.close();  
  17.   
  18.     return result;  
  19. }  
  20. 

3.12 计数器

  1. // 计数器(amount为正数则计数器加,为负数则计数器减,为0则获取当前计数器的值)  
  2. public static long incrementColumnValue(String tableName, String rowKey, String columnFamily, String column, long amount)   
  3.         throws IOException {  
  4.     // 建立一个数据库的连接  
  5.     Connection conn = ConnectionFactory.createConnection(conf);  
  6.     // 获取表  
  7.     HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  8.     // 计数器  
  9.     long result = table.incrementColumnValue(Bytes.toBytes(rowKey), Bytes.toBytes(columnFamily), Bytes.toBytes(column), amount);  
  10.     // 关闭资源  
  11.     table.close();  
  12.     conn.close();  
  13.       
  14.     return result;  
  15. }  
  16. 

4、内置过滤器的使用

HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中数据的多个维度(行、列、数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行键、列名、时间戳定位)。通常来说,通过行键、值来筛选数据的应用场景较多。需要说明的是,过滤器会极大地影响查询效率。所以,在数据量较大的数据表中,应尽量避免使用过滤器。

下面介绍一些常用的HBase内置过滤器的用法:

1、RowFilter:筛选出匹配的所有的行。使用BinaryComparator可以筛选出具有某个行键的行,或者通过改变比较运算符(下面的例子中是CompareFilter.CompareOp.EQUAL)来筛选出符合某一条件的多条数据,如下示例就是筛选出行键为row1的一行数据。
  1. // 筛选出匹配的所有的行  
  2. Filter rf = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));

2、PrefixFilter:筛选出具有特定前缀的行键的数据。这个过滤器所实现的功能其实也可以由RowFilter结合RegexComparator来实现,不过这里提供了一种简便的使用方法,如下示例就是筛选出行键以row为前缀的所有的行。

  1. // 筛选匹配行键的前缀成功的行  
  2. Filter pf = new PrefixFilter(Bytes.toBytes("row"));  

3、KeyOnlyFilter:这个过滤器唯一的功能就是只返回每行的行键,值全部为空,这对于只关注于行键的应用场景来说非常合适,这样忽略掉其值就可以减少传递到客户端的数据量,能起到一定的优化作用。

  1. // 返回所有的行键,但值全是空  
  2. Filter kof = new KeyOnlyFilter();

4、RandomRowFilter:按照一定的几率(<=0会过滤掉所有的行,>=1会包含所有的行)来返回随机的结果集,对于同样的数据集,多次使用同一个RandomRowFilter会返回不同的结果集,对于需要随机抽取一部分数据的应用场景,可以使用此过滤器。

  1. // 随机选出一部分的行  
  2. Filter rrf = new RandomRowFilter((float) 0.8);

5、InclusiveStopFilter:扫描的时候,我们可以设置一个开始行键和一个终止行键,默认情况下,这个行键的返回是前闭后开区间,即包含起始行,但不包含终止行。如果我们想要同时包含起始行和终止行,那么可以使用此过滤器。

  1. // 包含了扫描的上限在结果之内  
  2. Filter isf = new InclusiveStopFilter(Bytes.toBytes("row1"));

6、FirstKeyOnlyFilter:如果想要返回的结果集中只包含第一列的数据,那么这个过滤器能够满足要求。它在找到每行的第一列之后会停止扫描,从而使扫描的性能也得到了一定的提升。

  1. // 筛选出每行的第一个单元格  
  2. Filter fkof = new FirstKeyOnlyFilter();

7、ColumnPrefixFilter:它按照列名的前缀来筛选单元格,如果我们想要对返回的列的前缀加以限制的话,可以使用这个过滤器。

  1. // 筛选出前缀匹配的列  
  2. Filter cpf = new ColumnPrefixFilter(Bytes.toBytes("qual1"));

8、ValueFilter:按照具体的值来筛选单元格的过滤器,这会把一行中值不能满足的单元格过滤掉,如下面的构造器,对于每一行的一个列,如果其对应的值不包含ROW2_QUAL1,那么这个列就不会返回给客户端。

  1. // 筛选某个(值的条件满足的)特定的单元格  
  2. Filter vf = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("ROW2_QUAL1"));

9、ColumnCountGetFilter:这个过滤器在遇到一行的列数超过我们所设置的限制值的时候,结束扫描操作。

  1. // 如果突然发现一行中的列数超过设定的最大值时,整个扫描操作会停止  
  2. Filter ccf = new ColumnCountGetFilter(2);

10、SingleColumnValueFilter:用一列的值决定这一行的数据是否被过滤,可对它的对象调用setFilterIfMissing方法,默认的参数是false。其作用是,对于咱们要使用作为条件的列,如果参数为true,这样的行将会被过滤掉,如果参数为false,这样的行会包含在结果集中。

  1. // 将满足条件的列所在的行过滤掉  
  2.  
  3. SingleColumnValueFilter scvf = new SingleColumnValueFilter(    
  4.          Bytes.toBytes("colfam1"),     
  5.          Bytes.toBytes("qual2"),     
  6.          CompareFilter.CompareOp.NOT_EQUAL,     
  7.          new SubstringComparator("BOGUS"));    
  8. scvf.setFilterIfMissing(true);  
  9. 

11、SingleColumnValueExcludeFilter:这个过滤器与第10种过滤器唯一的区别就是,作为筛选条件的列,其行不会包含在返回的结果中。

12、SkipFilter:这是一种附加过滤器,其与ValueFilter结合使用,如果发现一行中的某一列不符合条件,那么整行就会被过滤掉。

  1. // 发现某一行中的一列需要过滤时,整个行就会被过滤掉  
  2. Filter skf = new SkipFilter(vf);

13、WhileMatchFilter:使用这个过滤器,当遇到不符合设定条件的数据的时候,整个扫描结束。

  1. // 当遇到不符合过滤器rf设置的条件时,整个扫描结束  
  2. Filter wmf = new WhileMatchFilter(rf);

14. FilterList:可以用于综合使用多个过滤器。其有两种关系: Operator.MUST_PASS_ONE表示关系AND,Operator.MUST_PASS_ALL表示关系OR,并且FilterList可以嵌套使用,使得我们能够表达更多的需求。

  1. // 综合使用多个过滤器,AND和OR两种关系  
  2. List<Filter> filters = new ArrayList<Filter>();    
  3. filters.add(rf);    
  4. filters.add(vf);    
  5. FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);

下面给出一个使用RowFilter过滤器的完整示例:

  1. public class HBaseFilter {  
  2.  
  3.       
  4.     private static final String TABLE_NAME = "table1";  
  5.   
  6.     public static void main(String[] args) throws IOException {  
  7.         // 设置配置  
  8.         Configuration conf = HBaseConfiguration.create();  
  9.         conf.set("hbase.zookeeper.quorum", "localhost");  
  10.         conf.set("hbase.zookeeper.property.clientPort", "2181");  
  11.         // 建立一个数据库的连接  
  12.         Connection conn = ConnectionFactory.createConnection(conf);  
  13.         // 获取表  
  14.         HTable table = (HTable) conn.getTable(TableName.valueOf(TABLE_NAME));  
  15.         // 创建一个扫描对象  
  16.         Scan scan = new Scan();  
  17.         // 创建一个RowFilter过滤器  
  18.         Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("abc")));  
  19.         // 将过滤器加入扫描对象  
  20.         scan.setFilter(filter);  
  21.         // 输出结果  
  22.         ResultScanner results = table.getScanner(scan);  
  23.         for (Result result : results) {  
  24.             for (Cell cell : result.rawCells()) {  
  25.                 System.out.println(  
  26.                         "行键:" + new String(CellUtil.cloneRow(cell)) + "\t" +  
  27.                         "列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" +   
  28.                         "列名:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +   
  29.                         "值:" + new String(CellUtil.cloneValue(cell)) + "\t" +  
  30.                         "时间戳:" + cell.getTimestamp());  
  31.             }  
  32.         }  
  33.         // 关闭资源  
  34.         results.close();  
  35.         table.close();  
  36.         conn.close();  
  37.           
  38.     }  
  39.   
  40. }  
  41. 

 

5、HBase与MapReduce

我们知道,在伪分布式模式和完全分布式模式下的HBase是架构在HDFS之上的,因此完全可以将MapReduce编程框架和HBase结合起来使用。也就是说,将HBase作为底层存储结构,MapReduce调用HBase进行特殊的处理,这样能够充分结合HBase分布式大型数据库和MapReduce并行计算的优点。

HBase实现了TableInputFormatBase类,该类提供了对表数据的大部分操作,其子类TableInputFormat则提供了完整的实现,用于处理表数据并生成键值对。TableInputFormat类将数据表按照Region分割成split,即有多少个Regions就有多个splits,然后将Region按行键分成<key,value>对,key值对应与行键,value值为该行所包含的数据。

HBase实现了MapReduce计算框架对应的TableMapper类和TableReducer类。其中,TableMapper类并没有具体的功能,只是将输入的<key,value>对的类型分别限定为Result和ImmutableBytesWritable。IdentityTableMapper类和IdentityTableReducer类则是上述两个类的具体实现,其和Mapper类和Reducer类一样,只是简单地将<key,value>对输出到下一个阶段。

HBase实现的TableOutputFormat将输出的<key,value>对写到指定的HBase表中,该类不会对WAL(Write-Ahead Log)进行操作,即如果服务器发生故障将面临丢失数据的风险。可以使用MultipleTableOutputFormat类解决这个问题,该类可以对是否写入WAL进行设置。

为了能使Hadoop集群上运行HBase程序,还需要把相关的类文件引入Hadoop集群上,不然会出现ClassNotFoundException错误。其具体方法是可在hadoop的环境配置文件hadoop-env.sh中引入HBASE_HOME和HBase的相关jar包,或者直接将HBase的jar包打包到应用程序文件中。

下面这个例子是将MapReduce和HBase结合起来的WordCount程序,它首先从指定文件中搜集数据,进行统计计算,最后将结果存储到HBase中:

  1. package com.hbase.demo;  
  2. import java.io.IOException;  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.hbase.HBaseConfiguration;  
  6. import org.apache.hadoop.hbase.HColumnDescriptor;  
  7. import org.apache.hadoop.hbase.HTableDescriptor;  
  8. import org.apache.hadoop.hbase.TableName;  
  9. import org.apache.hadoop.hbase.client.Connection;  
  10. import org.apache.hadoop.hbase.client.ConnectionFactory;  
  11. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  12. import org.apache.hadoop.hbase.client.Put;  
  13. import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;  
  14. import org.apache.hadoop.hbase.mapreduce.TableReducer;  
  15. import org.apache.hadoop.io.IntWritable;  
  16. import org.apache.hadoop.io.LongWritable;  
  17. import org.apache.hadoop.io.NullWritable;  
  18. import org.apache.hadoop.io.Text;  
  19. import org.apache.hadoop.mapreduce.Job;  
  20. import org.apache.hadoop.mapreduce.Mapper;  
  21. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  22. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  23.   
  24. public class HBaseWordCount {  
  25.       
  26.     public static class hBaseMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
  27.   
  28.         private final static IntWritable ONE = new IntWritable(1);  
  29.         private Text word = new Text();  
  30.   
  31.         @Override  
  32.         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  33.             String[] words = value.toString().split(" ");  
  34.             for ( String w : words) {  
  35.                 word.set(w);  
  36.                 context.write(word, ONE);  
  37.             }  
  38.         }  
  39.     }  
  40.       
  41.     public static class hBaseReducer extends TableReducer<Text, IntWritable, NullWritable> {  
  42.   
  43.         @Override  
  44.         protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  45.             int sum = 0;  
  46.             for (IntWritable value : values) {  
  47.                 sum += value.get();  
  48.             }  
  49.               
  50.             // Put实例化,每个词存一行  
  51.             Put put = new Put(key.getBytes());  
  52.             
  53.             // 列族为content,列名为count,列值为单词的数目  
  54.             put.addColumn("content".getBytes(), "count".getBytes(), String.valueOf(sum).getBytes());  
  55.               
  56.             context.write(NullWritable.get(), put);  
  57.         }  
  58.           
  59.     }  
  60.       
  61.     // 创建HBase数据表  
  62.     public static void createHBaseTable(String tableName) throws IOException {  
  63.         // 配置HBse  
  64.         Configuration conf = HBaseConfiguration.create();  
  65.         conf.set("hbase.zookeeper.quorum", "localhost");  
  66.         conf.set("hbase.zookeeper.property.clientPort", "2181");  
  67.         // 建立一个数据库的连接  
  68.         Connection conn = ConnectionFactory.createConnection(conf);  
  69.         // 创建一个数据库管理员  
  70.         HBaseAdmin hAdmin = (HBaseAdmin) conn.getAdmin();  
  71.         // 判断表是否存在  
  72.         if (hAdmin.tableExists(tableName)) {  
  73.             System.out.println("该数据表已存在,正在重新创建");  
  74.             hAdmin.disableTable(tableName);  
  75.             hAdmin.deleteTable(tableName);  
  76.         }  
  77.         // 创建表描述  
  78.         HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));  
  79.         // 在表描述里添加列族  
  80.         tableDesc.addFamily(new HColumnDescriptor("content"));  
  81.         // 创建表  
  82.         hAdmin.createTable(tableDesc);  
  83.         System.out.println("创建" + tableName + "表成功");  
  84.     }  
  85.       
  86.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  87.           
  88.         if (args.length != 3) {  
  89.             System.out.println("args error");  
  90.             System.exit(0);  
  91.         }  
  92.           
  93.         String input = args[0];  
  94.         String jobName = args[1];  
  95.         String tableName = args[2];  
  96.           
  97.         // 创建数据表  
  98.         HBaseWordCount.createHBaseTable(tableName);  
  99.           
  100.         // 配置MapReduce(或者将hadoop和hbase的相关配置文件引入项目)  
  101.         Configuration conf = new Configuration();  
  102.         conf.set("fs.defaultFS", "localhost:9000");  
  103.        conf.set("mapred.job.tracker", "localhost:9001");  
  104.         conf.set("hbase.zookeeper.quorum", "localhost");  
  105.         conf.set("hbase.zookeeper.property.clientPort", "2181");  
  106.         conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);  
  107.           
  108.         // 配置任务  
  109.         Job job = Job.getInstance(conf, jobName);  
  110.         job.setJarByClass(HBaseWordCount.class);  
  111.         job.setMapperClass(hBaseMapper.class);  
  112.         job.setReducerClass(hBaseReducer.class);  
  113.         job.setMapOutputKeyClass(Text.class);  
  114.         job.setMapOutputValueClass(IntWritable.class);  
  115.         job.setInputFormatClass(TextInputFormat.class);  
  116.         job.setOutputFormatClass(TableOutputFormat.class);  
  117.         FileInputFormat.addInputPath(job, new Path(input));  
  118.           
  119.         //执行MR任务  
  120.         boolean result = job.waitForCompletion(true);  
  121.         System.exit(result ? 0 : 1);  
  122.     }  
  123.   
  124. }  
  125. 

6、HBase的Bulkload

HBase可以让我们随机的、实时的访问大数据,但是怎样有效的将数据导入到HBase呢?HBase有多种导入数据的方法,最直接的方法就是在MapReduce作业中使用TableOutputFormat作为输出,或者使用标准的客户端API,但是这些都不是非常有效的方法。

如果HDFS中有海量数据要导入HBase,可以先将这些数据生成HFile文件,然后批量导入HBase的数据表中,这样可以极大地提升数据导入HBase的效率。这就是HBase的Bulkload,即利用MapReduce作业输出HBase内部数据格式的表数据,然后将生成的StoreFiles直接导入到集群中。与使用HBase API相比,使用Bulkload导入数据占用更少的CPU和网络资源。两个表之间的数据迁移也可以使用这种方法。下面给出具体示例:

  1. package com.hbase.demo;  
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.hbase.HBaseConfiguration;  
  6. import org.apache.hadoop.hbase.TableName;  
  7. import org.apache.hadoop.hbase.client.Connection;  
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;  
  9. import org.apache.hadoop.hbase.client.HTable;  
  10. import org.apache.hadoop.hbase.client.Put;  
  11. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;  
  12. import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;  
  13. import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;  
  14. import org.apache.hadoop.hbase.mapreduce.PutSortReducer;  
  15. import org.apache.hadoop.hbase.util.Bytes;  
  16. import org.apache.hadoop.io.LongWritable;  
  17. import org.apache.hadoop.io.Text;  
  18. import org.apache.hadoop.mapreduce.Job;  
  19. import org.apache.hadoop.mapreduce.Mapper;  
  20. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  21. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  22. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  23.   
  24. public class HBaseBulk {  
  25.     public static class bulkMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {  
  26.         @Override  
  27.         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  28.             // 将输入数据用tab键分词  
  29.             String[] values = value.toString().split("\t");  
  30.             if (values.length == 2) {  
  31.                 // 设置行键、列族、列名和值  
  32.                 byte[] rowKey = Bytes.toBytes(values[0]);  
  33.                 byte[] family = Bytes.toBytes("content");  
  34.                 byte[] column = Bytes.toBytes("number");  
  35.                 byte[] colValue = Bytes.toBytes(values[1]);  
  36.                 // 将行键序列化作为mapper输出的key  
  37.                 ImmutableBytesWritable rowKeyWritable = new ImmutableBytesWritable(rowKey);  
  38.                 // 将put对象作为mapper输出的value  
  39.                 Put put = new Put(rowKey);  
  40.                 put.addColumn(family, column, colValue);  
  41.                 context.write(rowKeyWritable, put);  
  42.             }  
  43.         }  
  44.     }  
  45.     
  46.     @SuppressWarnings("deprecation")  
  47.     public static void main(String[] args) throws Exception {  
  48.           
  49.         if (args.length != 3) {  
  50.             System.out.println("args error");  
  51.             System.exit(0);  
  52.         }  
  53.           
  54.         String input = args[0];  
  55.         String output = args[1];  
  56.         String jobName = args[2];  
  57.         String tableName = args[3];  
  58.           
  59.         // 配置MapReduce(或者将hadoop的相关配置文件引入项目)  
  60.         Configuration hadoopConf = new Configuration();  
  61.         hadoopConf.set("fs.defaultFS", "localhost:9000");  
  62.         hadoopConf.set("mapred.job.tracker", "localhost:9001");  
  63.         Job job = Job.getInstance(hadoopConf, jobName);  
  64.         job.setJarByClass(HBaseBulk.class);  
  65.         job.setMapperClass(bulkMapper.class);  
  66.         job.setReducerClass(PutSortReducer.class);  
  67.         job.setMapOutputKeyClass(ImmutableBytesWritable.class);  
  68.         job.setMapOutputValueClass(Put.class);  
  69.         job.setInputFormatClass(TextInputFormat.class);  
  70.         job.setOutputFormatClass(HFileOutputFormat2.class);  
  71.         FileInputFormat.addInputPath(job, new Path(input));  
  72.         FileOutputFormat.setOutputPath(job, new Path(output));  
  73.           
  74.         // 配置HBase(或者将hbase的相关配置文件引入项目)  
  75.         Configuration hbaseConf = HBaseConfiguration.create();  
  76.         hbaseConf.set("hbase.zookeeper.quorum", "localhost");  
  77.         hbaseConf.set("hbase.zookeeper.property.clientPort", "2181");  
  78.           
  79.         // 生成HFile  
  80.         Connection conn = ConnectionFactory.createConnection(hbaseConf);  
  81.         HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));  
  82.         HFileOutputFormat2.configureIncrementalLoad(job, table);  
  83.           
  84.         // 执行任务  
  85.         job.waitForCompletion(true);  
  86.           
  87.         // 将HFile文件导入HBase  
  88.         LoadIncrementalHFiles loader = new LoadIncrementalHFiles(hbaseConf);  
  89.         loader.doBulkLoad(new Path(output), table);  
  90.     }  
  91. }  
  92. 

上述代码首先将HDFS中的数据文件通过MapReduce任务生成HFile文件,然后将HFile文件导入HBase数据表(该数据表已存在)。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值