核心代码:
1.只有mapper
public void map(LongWritable key, Text value,
OutputCollector<K, V> output, Reporter reporter)throws IOException {
if (isValidLine(value.toString())) {
byte[] rowKey = makeRowKey(getFields(value.toString()));
Put p = new Put(rowKey);
String family = "fam1";
String columnName = "col1";
String columnValue = "val1";
p.add(Bytes.toBytes(family), Bytes.toBytes(columnName), Bytes.toBytes(columnValue));
table.put(p);
}
}
2.mapper和reduce
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
context.write(new ImmutableBytesWritable(Bytes.toBytes(key.get())), value);
}
public void reduce(ImmutableBytesWritable key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
String valueCon=null;
for(Text text:values){
valueCon+=text.toString();
}
Put put = new Put(key.get());
put.add(family, qualifier, Bytes.toBytes(valueCon));
context.write(key, put);
}