FileThreadPool

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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小海_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值