The data is written to HDFS HBase

Ready to work

  1. Create a file called fruit.tsv in / input HDFS directory, as follows:
    Here Insert Picture Description
  2. Create a table named fruit in HBase as follows:
    Here Insert Picture Description

Creating Mapper

public class FruitMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		//从HDFS中读取的数据
		String lineValue = value.toString();
		//读取出来的每行数据使用\t进行分割,存于String数组
		String[] values = lineValue.split("\t");
		
		//根据数据中值的含义取值
		String rowKey = values[0];
		String name = values[1];
		String color = values[2];
		
		//初始化rowKey  
		ImmutableBytesWritable rowKeyWritable = new ImmutableBytesWritable(Bytes.toBytes(rowKey));
		
		//初始化put对象
		Put put = new Put(Bytes.toBytes(rowKey));
		
		//参数分别:列族、列、值  
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),  Bytes.toBytes(name));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("color"),  Bytes.toBytes(color));

        //写数据
        context.write(rowKeyWritable, put); //ImmutableBytesWritable类型一般作为RowKey的类型;
	}

}

Creating Reducer

public class FruitReducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable> {

	@Override
	protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
		//读出来的每一行数据写入到fruit表中
		for(Put put: values){
			context.write(NullWritable.get(), put);
		}
	}
}

Creating Runner

public class FruitRunner {

    public static void main(String[] arg) throws Exception {
        Configuration conf = HBaseConfiguration.create();

        String[] args = {"hdfs://hcmaster:8020/input/fruit.tsv"};

        //创建Job任务
        Job job = Job.getInstance(conf, FruitRunner.class.getSimpleName());
        //设置主类
        job.setJarByClass(FruitRunner.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));

        //设置Mapper
        job.setMapperClass(FruitMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(Put.class);

        //设置Reducer
        TableMapReduceUtil.initTableReducerJob("fruit", FruitReducer.class, job);

        //设置Reduce数量,最少1个
        job.setNumReduceTasks(1);

        boolean isSuccess = job.waitForCompletion(true);
        if (!isSuccess) {
            throw new IOException("Job running with error");
        }

        int status = isSuccess ? 0 : 1;

        System.exit(status);
    }

}

run

  1. The jar packaging and uploaded to Linux
  2. Perform jar package
    Here Insert Picture Description
    3. View Results
    Here Insert Picture Description
Published 374 original articles · won praise 777 · views 70000 +

Guess you like

Origin blog.csdn.net/lianghecai52171314/article/details/104801847