com.alibaba.doris.client.tools.importdata.FileReadTask.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.doris.client.tools.importdata.FileReadTask.java

Source

/**
 * Project: dataserver.tools-0.1.0-SNAPSHOT
 * 
 * File Created at 2011-8-4
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.alibaba.doris.client.tools.importdata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang.StringUtils;

import com.alibaba.doris.client.tools.concurrent.ParralelTaskImpl;

/**
 * TODO Comment of ReadTask
 * 
 * @author luyi.huangly
 */
public class FileReadTask extends ParralelTaskImpl {
    private Queue<KeyPair> keyQueue;
    private String fileName;
    ReentrantLock lock = new ReentrantLock(); //?
    //??Condition
    Condition produced = lock.newCondition();
    private AtomicBoolean finish;

    /**
     * @return the finish
     */
    public AtomicBoolean getFinish() {
        return finish;
    }

    /**
     * @param finish the finish to set
     */
    public void setFinish(AtomicBoolean finish) {
        this.finish = finish;
    }

    public ReentrantLock getLock() {
        return lock;
    }

    public void setLock(ReentrantLock lock) {
        this.lock = lock;
    }

    public Condition getProduced() {
        return produced;
    }

    public void setProduced(Condition produced) {
        this.produced = produced;
    }

    public Queue<KeyPair> getKeyQueue() {
        return keyQueue;
    }

    public void setKeyQueue(Queue<KeyPair> keyQueue) {
        this.keyQueue = keyQueue;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void doRun(long index) {

        File file = new File(fileName);
        if (!file.exists() || !file.isFile()) {
            System.err.println("file path error!!");
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            System.err.println("file path error!!");
        }
        String tempString = null;
        int line = 1;
        // null?
        try {
            while ((tempString = reader.readLine()) != null) {
                String[] tempStrings = StringUtils.split(tempString, " ");
                if (tempStrings.length != 2) {
                    System.err.println(String.format("line %s format error!!", line));
                }
                try {
                    lock.lock();//?
                    keyQueue.add(new KeyPair(tempStrings[0], tempStrings[1]));
                    produced.signal();
                } finally {
                    lock.unlock();
                }
                line++;
            }
        } catch (IOException e) {
            System.err.println("file path error!!");
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finish.set(true);
        }

    }
}