Updated on 2022-09-14 GMT+08:00

Multi-Thread Tasks

Function

Create multi-threaded tasks and initiate multiple instances to perform file operations.

Example Codes

The following is a code snippet of file deletion. For complete codes, see the HdfsExample class in com.huawei.bigdata.hdfs.examples.

 // Service example 2: multi-thread tasks 
final int THREAD_COUNT = 2;
for (int threadNum = 0; threadNum < THREAD_COUNT; threadNum++) {
    HdfsExampleThread example_thread = new HdfsExampleThread("hdfs_example_" + threadNum);
    example_thread.start();
}


class HdfsExampleThread extends Thread {
    private final static Log LOG = LogFactory.getLog(HdfsExampleThread.class.getName());
    /**
     * 
     * @param threadName
     */
    public HdfsExampleThread(String threadName) {
        super(threadName);
    }
    public void run() {
        HdfsExample example;
        try {
            example = new HdfsExample("/user/hdfs-examples/" + getName(), "test.txt");
            example.test();
        } catch (IOException e) {
            LOG.error(e);
        }
    }
}

The example.test() method is the operation on files. The code is as follows:

/**
 * HDFS operation instance
 * 
 * @throws IOException
 * @throws ParameterException
 * 
 * @throws Exception
 *
 */
public void test() throws IOException {
    // Create a directory.
    mkdir();

    // Write data into a file.
    write();

    // Append data to a file.
    append();

    // Read a file. 
    read();

    // Delete a file.
    delete();

    // Delete a directory.
    rmdir();
}