20,808
社区成员
发帖
与我相关
我的任务
分享
package pchour;
import java.io.IOException;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
public class PCHourMapper extends TableMapper<ImmutableBytesWritable, ImmutableBytesWritable> {
public void map(ImmutableBytesWritable row, Result values, Context context) throws IOException, InterruptedException {
ImmutableBytesWritable value = null;
ImmutableBytesWritable key = null;
for (KeyValue kv : values.list()) {
if ("m".equals(Bytes.toString(kv.getFamily())) && "city_id".equals(Bytes.toString(kv.getQualifier()))) {
key = row;
value = new ImmutableBytesWritable(kv.getValue());
context.write(key, value);
}
}
}
}
package pchour;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
public class PCHourReducer extends TableReducer<ImmutableBytesWritable, Iterable<ImmutableBytesWritable>, ImmutableBytesWritable> {
public void reduce(ImmutableBytesWritable key, Iterable<ImmutableBytesWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
for (ImmutableBytesWritable val : values) {
count += Bytes.toInt(val.get());
}
Put put = new Put(key.get());
put.add(Bytes.toBytes("m"), Bytes.toBytes("city_id_all"), Bytes.toBytes(count));
context.write(key, put);
}
}
package pchour;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.mapreduce.Job;
public class PCHourAnalysis {
protected Configuration configure;
protected int taskCount;
public PCHourAnalysis(String zookeeperClientPort, String zookeeperQuoRum, int taskCount) {
configure = HBaseConfiguration.create();
configure.set("hbase.zookeeper.property.clientPort", zookeeperClientPort);
configure.set("hbase.zookeeper.quorum", zookeeperQuoRum);
}
public int analysis() {
int result = 0;
Job job;
try {
job = new Job(configure);
job.setJobName("pc hour analysis job");
job.setJarByClass(getClass());
job.setMapperClass(PCHourMapper.class);
job.setReducerClass(PCHourReducer.class);
} catch (IOException e) {
result = -1;
return result;
}
Scan scan = new Scan();
scan.setCaching(500);
scan.setCacheBlocks(false);
try {
TableMapReduceUtil.initTableMapperJob("newnetworkbenchSpeedDataRaw", scan, PCHourMapper.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job);
TableMapReduceUtil.initTableReducerJob("newnetworkbenchhourresult", PCHourReducer.class, job);
} catch (IOException e) {
result = -2;
return result;
}
job.setNumReduceTasks(taskCount);
try {
if(!job.waitForCompletion(true)) {
result = -3;
}
} catch (ClassNotFoundException | IOException | InterruptedException e) {
result = -4;
}
return result;
}
}
package pchour;
public class PCHour {
public static void main(String[] args) {
String zookeeperClientPort = "8023";
String zookeeperQuoRum = "tc-op-uaq07.tc,yf-uaq-jsl-ruby00.yf01,tc-uaq-jsl-ruby00.tc,tc-op-uaq17.tc";
PCHourAnalysis hourAnalysis = new PCHourAnalysis(zookeeperClientPort, zookeeperQuoRum, 8);
hourAnalysis.analysis();
}
}