package tag;
import hdfs.HdfsFile;
import hive.HiveConnection;
import util.DateUtil;
import util.MyShell;
import java.io.File;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by isu on 15-10-15.
*/
public class FileThreadPool {
private static int produceTaskSleepTime = 30*1000;
private static final int produceTaskMaxNumber = 10;
private static final int corePoolSize = 7;
public void start(String type,int stardDay,int endDay) {
//加载字典表
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, produceTaskMaxNumber, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),new ThreadPoolExecutor.DiscardOldestPolicy());
HdfsFile hdfsFile = new HdfsFile();
int threadTotal = 0;
for(int i=stardDay;i<=endDay;i = Integer.valueOf(DateUtil.date2String(DateUtil.addDay(DateUtil.string2Date(String.valueOf(i)), 1)))){
DictionaryTable.deviceIdSet.clear();
DictionaryTable dt = new DictionaryTable();
dt.init();
BloomFilterContainer.init();
List fileList = hdfsFile.getDirectoryFromHdfs(type,i);
//删除可能存在的文件
String userAttribute = "/data/tag/AttributeEquipmentTag/"+i +".txt";
String appTagFile = "/data/tag/AppTag/"+i +".txt";
beforeProcess(userAttribute);
beforeProcess(appTagFile);
//因为for中启动了多线程,
System.out.println(" inner for begin ");
threadTotal+=fileList.size();
for(Object file :fileList){
try {
String task = "task@ " + i;
System.out.println("创建任务并提交到线程池中:" + task);
while(threadPool.getActiveCount()>corePoolSize){
Thread.sleep(produceTaskSleepTime);
}
threadPool.execute(new FileData(type,i,(String) file ));
} catch (Exception e) {
e.printStackTrace();
}
}
while (threadPool.getCompletedTaskCount()<threadTotal){
System.out.println(threadPool.getCompletedTaskCount());
try {
Thread.sleep(5*100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(type.equals("appopen")){
endProcess(String.valueOf(i));
}
//close()
close();
System.out.println(" inner for complete");
}
// new MqData().run();
}
public boolean beforeProcess(String fileName){
File file = new File(fileName);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return false;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
file.delete();
return true;
}
}
return false;
}
public void endProcess(String day){
//把本地文件加载到hive表中
HiveConnection.runHiveCommand("alter table tag.user_attribute_equipment_tag drop partition(day=" + day + ")");
HiveConnection.runHiveCommand("load data local inpath '/data/tag/AttributeEquipmentTag/" + day + ".txt' into table tag.user_attribute_equipment_tag partition(day=" + day + ")");
HiveConnection.runHiveCommand("alter table tag.user_app_tag drop partition(day="+day+")");
HiveConnection.runHiveCommand("load data local inpath '/data/tag/AppTag/"+day+".txt' into table tag.user_app_tag partition(day="+day+")");
MyShell shell = new MyShell();
String[] command1 = new String[]{
"/usr/bin/hadoop",
"jar",
"/data/IsuTag/IsuTag.jar",
"mapreduce.Combine30AppTag",
day,
day
};
shell.run(command1);
String[] command2 = new String[]{
"/usr/bin/hadoop",
"jar",
"/data/IsuTag/IsuTag.jar",
"mapreduce.Combine30AttributeEquipmentTag",
day,
day
};
shell.run(command2);
}
public static void close(){
HdfsFile.closeFileSystem();
BloomFilterContainer.clear();
DictionaryTable.clear();
}
public static void main(String[] args){
if(args.length != 3){
System.out.println("args is error .eg: [device|appopen] 20150820 20150820");
System.exit(1);
}
FileThreadPool t = new FileThreadPool();
t.start(args[0],Integer.valueOf(args[1]),Integer.valueOf(args[2]));
//关闭
close();
System.exit(0);
}
}
import hdfs.HdfsFile;
import hive.HiveConnection;
import util.DateUtil;
import util.MyShell;
import java.io.File;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by isu on 15-10-15.
*/
public class FileThreadPool {
private static int produceTaskSleepTime = 30*1000;
private static final int produceTaskMaxNumber = 10;
private static final int corePoolSize = 7;
public void start(String type,int stardDay,int endDay) {
//加载字典表
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, produceTaskMaxNumber, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),new ThreadPoolExecutor.DiscardOldestPolicy());
HdfsFile hdfsFile = new HdfsFile();
int threadTotal = 0;
for(int i=stardDay;i<=endDay;i = Integer.valueOf(DateUtil.date2String(DateUtil.addDay(DateUtil.string2Date(String.valueOf(i)), 1)))){
DictionaryTable.deviceIdSet.clear();
DictionaryTable dt = new DictionaryTable();
dt.init();
BloomFilterContainer.init();
List fileList = hdfsFile.getDirectoryFromHdfs(type,i);
//删除可能存在的文件
String userAttribute = "/data/tag/AttributeEquipmentTag/"+i +".txt";
String appTagFile = "/data/tag/AppTag/"+i +".txt";
beforeProcess(userAttribute);
beforeProcess(appTagFile);
//因为for中启动了多线程,
System.out.println(" inner for begin ");
threadTotal+=fileList.size();
for(Object file :fileList){
try {
String task = "task@ " + i;
System.out.println("创建任务并提交到线程池中:" + task);
while(threadPool.getActiveCount()>corePoolSize){
Thread.sleep(produceTaskSleepTime);
}
threadPool.execute(new FileData(type,i,(String) file ));
} catch (Exception e) {
e.printStackTrace();
}
}
while (threadPool.getCompletedTaskCount()<threadTotal){
System.out.println(threadPool.getCompletedTaskCount());
try {
Thread.sleep(5*100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(type.equals("appopen")){
endProcess(String.valueOf(i));
}
//close()
close();
System.out.println(" inner for complete");
}
// new MqData().run();
}
public boolean beforeProcess(String fileName){
File file = new File(fileName);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return false;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
file.delete();
return true;
}
}
return false;
}
public void endProcess(String day){
//把本地文件加载到hive表中
HiveConnection.runHiveCommand("alter table tag.user_attribute_equipment_tag drop partition(day=" + day + ")");
HiveConnection.runHiveCommand("load data local inpath '/data/tag/AttributeEquipmentTag/" + day + ".txt' into table tag.user_attribute_equipment_tag partition(day=" + day + ")");
HiveConnection.runHiveCommand("alter table tag.user_app_tag drop partition(day="+day+")");
HiveConnection.runHiveCommand("load data local inpath '/data/tag/AppTag/"+day+".txt' into table tag.user_app_tag partition(day="+day+")");
MyShell shell = new MyShell();
String[] command1 = new String[]{
"/usr/bin/hadoop",
"jar",
"/data/IsuTag/IsuTag.jar",
"mapreduce.Combine30AppTag",
day,
day
};
shell.run(command1);
String[] command2 = new String[]{
"/usr/bin/hadoop",
"jar",
"/data/IsuTag/IsuTag.jar",
"mapreduce.Combine30AttributeEquipmentTag",
day,
day
};
shell.run(command2);
}
public static void close(){
HdfsFile.closeFileSystem();
BloomFilterContainer.clear();
DictionaryTable.clear();
}
public static void main(String[] args){
if(args.length != 3){
System.out.println("args is error .eg: [device|appopen] 20150820 20150820");
System.exit(1);
}
FileThreadPool t = new FileThreadPool();
t.start(args[0],Integer.valueOf(args[1]),Integer.valueOf(args[2]));
//关闭
close();
System.exit(0);
}
}