定时任务,将数据库中某张表中的数据写入到文件中

这周有个业务需要将数据库中某张表中的数据导出到文件里面,开始说使用shell脚本将数据库中数据导出,但是不会写shell脚本呀!后面说将这个写成定时任务。然后就写定时任务,但是突然发现很久没有写文件流了;于是东拼西凑写下了以下代码。shell脚本虽说我们作java开发不常使用,但并不是不用,找个时间学下shell。

package com.utstar.ucs.indexservice;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.utstar.ucs.dao.impl.BaseDaoImpl;
import com.utstar.ucs.model.Subscriber;

/**
 * 定时任务,将uc_subscriber中的数据导出到文件中
 * @author utsc1243
 * @date 2019年7月13日
 */
public class ExportUcSubscriber extends BaseDaoImpl<Subscriber> {
    private Properties pathProp = new Properties();
    private Logger logger = Logger.getLogger(ExportUcSubscriber.class);

    // 初始化
    public void init() {
        try {
            pathProp.load(ExportUcSubscriber.class.getResourceAsStream("/dataSource.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void exportUcSubscriberRun() throws Exception {
        boolean isRunnable = "true".equals(pathProp.getProperty("exportUcSubscriber.switch"));
        if (isRunnable) {
            subscriberIndex();
        }
    }

    public ExportUcSubscriber() {
        init();
    }

    /**
     * 同步数据操作
     * @throws Exception 
     */
    private void subscriberIndex() throws Exception {
        String fileNamePrefix = pathProp.getProperty("exportUcSubscriber.fileName.prefix");
        String fileNameSuffix = pathProp.getProperty("exportUcSubscriber.fileName.suffix");
        long begin = System.currentTimeMillis();
        StringBuilder path = new StringBuilder();
        path.append(getPath());
        mkdir(path.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        path.append(fileNamePrefix).append(sdf.format(new Date())).append(fileNameSuffix);
        createfile(path.toString());
        String hql = " from  Subscriber";
        List<Subscriber> subscriberList = find(hql, 0, 100,new ArrayList<Object>());
        BufferedWriter writer = new BufferedWriter(new FileWriter(path.toString()));
        
        for (Subscriber subscriber : subscriberList) {
            writer.write(subscriber.toString());
            writer.flush();
        }
        writer.close();
        logger.info("total media count : " + subscriberList.size());
        // 将数据同步到Solr中
        // SolrUtils.getInstance().scheduleIndex("idp_schedule", viewScheduleList);
        long end = System.currentTimeMillis();
        logger.info("build index used time : " + (end - begin) + "ms");
    }
    
    /**
     * 创建文件
     * @param path
     */
    private void createfile(String path) {
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 创建文件夹
     * @param path
     */
    public void mkdir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    
    /**
     * 获取路径
     * @return
     */
    public String getPath(){
        String os = System.getProperty("os.name");
        String basePath = "";
        if(os.toLowerCase().startsWith("win")){
            basePath = pathProp.getProperty("exportUcSubscriber.winPath");
        } else {
            basePath = pathProp.getProperty("exportUcSubscriber.linuxPath");
        }
        return basePath;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值