import
java.io.IOException;
import
java.util.List;
import
org.apache.hadoop.conf.Configuration;
import
org.apache.hadoop.hbase.Cell;
import
org.apache.hadoop.hbase.CellUtil;
import
org.apache.hadoop.hbase.HBaseConfiguration;
import
org.apache.hadoop.hbase.HColumnDescriptor;
import
org.apache.hadoop.hbase.HTableDescriptor;
import
org.apache.hadoop.hbase.TableName;
import
org.apache.hadoop.hbase.client.Admin;
import
org.apache.hadoop.hbase.client.Connection;
import
org.apache.hadoop.hbase.client.ConnectionFactory;
import
org.apache.hadoop.hbase.client.Mutation;
import
org.apache.hadoop.hbase.client.Put;
import
org.apache.hadoop.hbase.client.Result;
import
org.apache.hadoop.hbase.client.Scan;
import
org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import
org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import
org.apache.hadoop.hbase.mapreduce.TableMapper;
import
org.apache.hadoop.hbase.mapreduce.TableReducer;
import
org.apache.hadoop.hbase.util.Bytes;
import
org.apache.hadoop.mapreduce.Job;
import
org.apache.hadoop.mapreduce.Mapper;
import
org.apache.hadoop.mapreduce.Reducer;
public
class
WordCountHbaseMapreduce03 {
public
static
void
main(String[] args)
throws
Exception {
System.exit(run());
}
public
static
class
MyHBaseMap03
extends
TableMapper<immutablebyteswritable, immutablebyteswritable=
""
>{
@Override
protected
void
map(ImmutableBytesWritable key, Result value,
Mapper<immutablebyteswritable, immutablebyteswritable=
""
>.Context context)
throws
IOException, InterruptedException {
ImmutableBytesWritable word=
null
;
ImmutableBytesWritable num=
null
;
List<cell> cs=value.listCells();
for
(Cell cell:cs){
word=
new
ImmutableBytesWritable(CellUtil.cloneRow(cell));
num=
new
ImmutableBytesWritable(CellUtil.cloneValue(cell));
}
context.write(word, num);
}
}
private
static
int
run()
throws
Exception {
Configuration conf =
new
Configuration();
conf = HBaseConfiguration.create(conf);
conf.set(
"hbase.zookeeper.quorum"
,
"192.168.52.140"
);
Job job = Job.getInstance(conf,
"wordcount3"
);
job.setJarByClass(WordCountHbaseMapreduce03.
class
);
Scan scan =
new
Scan();
scan.addColumn(Bytes.toBytes(
"wordcount"
), Bytes.toBytes(
"num"
));
TableMapReduceUtil.initTableMapperJob(
"word"
, scan, MyHBaseMap03.
class
,
ImmutableBytesWritable.
class
, ImmutableBytesWritable.
class
, job);
createTable(conf);
TableMapReduceUtil.initTableReducerJob(
"word2"
, MyHBaseReduce03.
class
, job);
return
job.waitForCompletion(
true
) ?
0
:
1
;
}
private
static
void
createTable(Configuration conf)
throws
IOException{
Connection con = ConnectionFactory.createConnection(conf);
Admin admin = con.getAdmin();
TableName tn = TableName.valueOf(
"word2"
);
if
(!admin.tableExists(tn)){
HTableDescriptor htd =
new
HTableDescriptor(tn);
HColumnDescriptor hcd =
new
HColumnDescriptor(
"wordcount"
);
htd.addFamily(hcd);
admin.createTable(htd);
System.out.println(
"表不存在,新创建表成功...."
);
}
}
public
static
class
MyHBaseReduce03
extends
TableReducer<immutablebyteswritable, immutablebyteswritable=
""
>{
@Override
protected
void
reduce(ImmutableBytesWritable key, Iterable<immutablebyteswritable> values,
Reducer<immutablebyteswritable, mutation=
""
>.Context context)
throws
IOException, InterruptedException {
Put put=
new
Put(key.get());
put.addColumn(Bytes.toBytes(
"wordcount"
), Bytes.toBytes(
"num"
), values.iterator().next().get());
context.write(
new
ImmutableBytesWritable(key.get()), put);
}
}