第二个想法,再看回原始数据,可以发现如果我们是做用户推荐的话,那么数据很可能每条记录的长度都相差不大,如果这样的话,那么是否可以用记录的偏移值除以这条记录的长度,最后再四舍五入,那么得到的row值就基本按照1递增了。
具体代码如下:
- package org.fansy.date1023;
- import java.io.IOException;
- 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 org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
- import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
- import org.apache.hadoop.hbase.util.Bytes;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.util.GenericOptionsParser;
- public class HDFSToHBase {
- /**
- * import data from hdfs to hbase
- */
- public static class MapperClass extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put>{
- private Put put;
- private byte[] family,qulifier1,qulifier2,qulifier3;
- public void setup(Context context){
- family=Bytes.toBytes(context.getConfiguration().get("FAMILY"));
- qulifier1=Bytes.toBytes(context.getConfiguration().get("QULIFIER1"));
- qulifier2=Bytes.toBytes(context.getConfiguration().get("QULIFIER2"));
- qulifier3=Bytes.toBytes(context.getConfiguration().get("QULIFIER3"));
- }
- public void map(LongWritable key,Text line,Context context)throws IOException,InterruptedException{
- String[] values=line.toString().split(",");
- if(values.length!=3){ // if there are not three args,then return
- return ;
- }
- // set the row value
- int tempRow=Math.round(Float.parseFloat(key.toString())/(line.toString().length()));
- byte [] row=Bytes.toBytes(tempRow);
- byte [] qulifier1_value = Bytes.toBytes(values[0]);
- byte [] qulifier2_value = Bytes.toBytes(values[1]);
- byte [] qulifier3_value = Bytes.toBytes(values[2]);
- put=new Put(row);
- put.add(family,qulifier1,qulifier1_value); // user
- context.write(new ImmutableBytesWritable(row),put);
- put=new Put(row);
- put.add(family,qulifier2,qulifier2_value); // item
- context.write(new ImmutableBytesWritable(row),put);
- put=new Put(row);
- put.add(family,qulifier3,qulifier3_value); // item
- context.write(new ImmutableBytesWritable(row),put);
- }
- }
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- //check if there exist the table then delete the data and alter the family
- if(args.length<3){
- System.out.println("Wrong number of arguments "+args.length);
- System.out.println("Usage: <input> <tablename> <family> <qulifier1> <qulifier2> <qulifier3>");
- System.exit(-1);
- }
- String tableName=args[1];
- String family=args[2];
- if(existTable(tableName)){
- alterTable(tableName);
- }
- createTable(tableName,family);
- // import data to hbase
- HDFSToHBase.runMyJob(args);
- }
- public static void runMyJob(String[] args) {
- Configuration conf = HBaseConfiguration.create();
- try{
- String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
- if(otherArgs.length != 6) {
- System.out.println("Wrong number of arguments: " + otherArgs.length);
- System.out.println("Usage: <input> <tablename> <family> <qulifier1> <qulifier2> <qulifier3>");
- System.exit(-1);
- }
- String family=otherArgs[2];
- String qulifier1=otherArgs[3];
- String qulifier2=otherArgs[4];
- String qulifier3=otherArgs[5];
- conf.set("FAMILY", family);
- conf.set("QULIFIER1", qulifier1);
- conf.set("QULIFIER2", qulifier2);
- conf.set("QULIFIER3", qulifier3);
- Job job=new Job(conf,"import data to hbase");
- job.setJarByClass(HDFSToHBase.class);
- job.setMapperClass(MapperClass.class);
- job.setMapOutputKeyClass(ImmutableBytesWritable.class);
- job.setMapOutputValueClass(Put.class);
- TableMapReduceUtil.initTableReducerJob(otherArgs[1], null, job);
- job.setNumReduceTasks(0);
- FileInputFormat.setInputPaths(job, otherArgs[0]);
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- // check whether the given table exists if and delete the data and create a new one else create a new one
- public static boolean existTable(String tableName) throws IOException{
- boolean flag=false;
- Configuration conf = HBaseConfiguration.create();
- HBaseAdmin admin=new HBaseAdmin(conf);
- flag=admin.isTableAvailable(Bytes.toBytes(tableName));
- admin.close();
- return flag;
- }
- // if the table exists then delete the data and alter the family
- public static void alterTable(String tableName ) throws IOException{
- System.out.println("table "+tableName+" exists ,delete it ...");
- Configuration conf = HBaseConfiguration.create();
- HBaseAdmin admin=new HBaseAdmin(conf);
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- admin.close();
- }
- // if the table does not exist then create the table
- public static void createTable( String tableName,String family) throws IOException{
- System.out.println("create the new table :"+tableName+" ...");
- Configuration conf = HBaseConfiguration.create();
- HBaseAdmin admin=new HBaseAdmin(conf);
- HTableDescriptor desc=new HTableDescriptor(Bytes.toBytes(tableName));
- HColumnDescriptor coldef=new HColumnDescriptor(Bytes.toBytes(family));
- desc.addFamily(coldef);
- admin.createTable(desc);
- admin.close();
- }
- }
上面的代码同时包含了先判断输入的参数(即表名)是否已存在HBase中,如果存在则删除,否则新建一个表,最后把数据通过MR导入HBase.其中的44行就是第二种想法的实现。
导入到HBase中的数据如下:

疑问:(1)如果数据文件是多个的话,那么第二种想法就会导致有些数据被覆盖。多个数据的话是否可以用一个MR 把所有的数据先放入一个文件然后在调用上面的代码?
(2)是否有其他的比较好的做法呢?
