20,810
社区成员
发帖
与我相关
我的任务
分享
package ps2package;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class ps2onlinetime
{
public static class ps2onlinetimeCombiner extends Reducer<Text, Text, Text, Text>
{
Text outputkey = new Text();
Text outputvalue = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String v = "";
for (Text val : values) {
if (val.toString().length() == 23)
if (v.equals(""))
v += val.toString();
else
v += "\t" + val.toString();
}
String[] v1 = v.split("\t");
Arrays.sort(v1);
v = "";
loginTime beginT = new loginTime();
loginTime endT = new loginTime();
String fbstr = "", ebstr = "";
int onlineT = 0, v1type = 0;
for (int i = 0; i < v1.length; i++) {
String[] vx = v1[i].split("_");
if (vx.length != 2)
continue;
v1type = Integer.parseInt(vx[1]);
if (v1type >= 102) {
endT = new loginTime(v1[i]);
if (fbstr.equals("")) {
beginT = endT;
fbstr = beginT.getvStr();
}
if (beginT.getTimeType() == 102) {
try {
onlineT += loginTime.compTime(beginT.getTime(), endT.getTime());
}
catch (Exception ex) {
;
}
}
ebstr = endT.getvStr();
beginT = endT;
}
}
if (fbstr.length() == 23 && ebstr.length() == 23 && onlineT >= 0) {
v = fbstr + ";" + String.valueOf(onlineT) + ";" + ebstr;
outputvalue.set(v);
context.write(key, outputvalue);
}
}
}
public static class ps2onlinetimeReducer extends Reducer<Text, Text, Text, Text>
{
Text outputvalue = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int j = 0;
String v = "";
for (Text val : values) {
if (v.equals("")) v += val.toString();
else v += "\t" + val.toString();
}
String[] v1 = v.split("\t");
Arrays.sort(v1);
v = "";
loginTime beginT = new loginTime();
loginTime endT = new loginTime();
String fbstr = "", ebstr = "";
int onlineT = 0;
for (int i = 0; i < v1.length; i++) {
String[] v2 = v1[i].split(";");
if (v2.length != 3) {
// context.write(key, new Text(v1[i]));
continue;
}
endT = new loginTime(v2[0]);
onlineT += Integer.parseInt(v2[1]);
if (i == 0) {
fbstr = endT.getvStr();
}
else if (beginT.getTimeType() == 102) {
try {
onlineT += loginTime.compTime(beginT.getTime(), endT.getTime());
}
catch (Exception ex) {}
}
beginT = new loginTime(v2[2]);
ebstr = beginT.getvStr();
}
if (fbstr.length() == 23 && ebstr.length() == 23 && onlineT >= 0) {
v = fbstr + "\t" + ebstr + "\t" + String.valueOf(onlineT);
outputvalue.set(v);
context.write(key, outputvalue);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "master.nj.hadoop:9001");
conf.set("mapred.job.reuse.jvm.num.tasks", "-1");
conf.set("mapred.child.java.opts", "-xmx512m");
String[] dfsPath = new String[2];
String _LOGTIME = loginTime.getStringDate();
String __LOGTIME = loginTime.getStringDate(true);
dfsPath[0] = "/ps2tt1/loginlog/CharacterLogins_20G_1.log";
// CharacterLogins_all.log
// CharacterLogins20121027.log
// CharacterLogins_20G.log
// CharacterLogins_20G_1.log
dfsPath[1] = "/ps2tt1/outdata/";
String outpath = dfsPath[1];
dfsPath[1] = "/ps2tt1/outdata/" + "onlinetime_" + _LOGTIME;
MyHDFS myfs = new MyHDFS(conf, dfsPath);
myfs.setRedCount(1);
myfs.setInputSize(myfs.getMyBlockSize());
System.out.println("block size:" + myfs.getMyBlockSize());
System.out.println("input size:" + myfs.getMyInputSize());
Job job = new Job(myfs.getMyConf(), "OnlineTime");
System.out.println("OnlineTime 1.40.15");
System.out.println(outpath);
FileSystem hdfs = FileSystem.get(myfs.getMyConf());
Path dst = myfs.getMyInPath();
job.setJarByClass(ps2onlinetime.class);
job.setMapperClass(ps2ccu.ps2loginMaper.class);
job.setCombinerClass(ps2onlinetimeCombiner.class);
job.setReducerClass(ps2onlinetimeReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(myfs.getMyRedCount());
FileInputFormat.addInputPath(job, new Path(myfs.getMyInPath().toString()));
FileInputFormat.setMinInputSplitSize(job, myfs.getMyInputSize() * 1024 * 1024);
FileInputFormat.setMaxInputSplitSize(job, myfs.getMyInputSize() * 1024 * 1024);
FileOutputFormat.setOutputPath(job, myfs.getMyOutPath());
System.out.println("Input Split:" + myfs.getMyInputSize());
System.out.println("Reduce Count:" + myfs.getMyRedCount());
boolean jobrun = false;
jobrun = job.waitForCompletion(true);
if (jobrun) {
_LOGTIME = loginTime.getStringDate();
__LOGTIME = loginTime.getStringDate(true);
dst = new Path(myfs.getMyOutPath().toString());
FileStatus outfiles[] = hdfs.listStatus(dst);
for (FileStatus file : outfiles) {
if (file.isDir() == false && file.getPath().toString().indexOf("part-r-") >= 0) {
String mvinpath = file.getPath().toString();
String mvoutpath = outpath + "/onlinetime/onlinetime_" + _LOGTIME;
new MoveFile(mvinpath, mvoutpath, conf);
new DeleteFile(myfs.getMyOutPath().toString(), conf);
}
}
}
}
}