lundi 21 mars 2016

How to expose hadoop commands as an api

UseCase:

I am creating a Java API which does the hadoop filesystem operations. This API is used by applications to perform the file operations.

I have created an Interface which contains the abstract methods and the corresponding implementation class which has the implementations. Now i want to expose these method to the application developer so that the developer performs the operations.

Question:

What is the best design pattern which can be used to serve the usecase?

Interface: HDFSClient

package com.hdfsoperations.filesystemapi;

import org.apache.hadoop.fs.Path;

public interface HDFSClient {

    public void createDir(Path dir) throws Exception;

    public void deleteDir(Path dir) throws Exception;

    public boolean isDir(Path path);

    public boolean isFile(Path path);

    public void copyFromLocal(Path localpath, Path hdfspath) throws Exception;

    public void copyToLocal(Path hdfspath, Path localpath) throws Exception;

    public void move(Path source, Path destination) throws Exception;

}

Implementation Class : HDFSClientImpl

package com.hdfsoperations.filesystemapi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;

public class HDFSClientImpl implements HDFSClient {

    private static final Logger LOGGER = Logger.getLogger(HDFSClientImpl.class);
    private static Configuration conf = new Configuration();

    FileSystem fs = null;

    public void createDir(Path dir) throws Exception {
        boolean res = false;
        fs = FileSystem.get(conf);
        res = fs.mkdirs(dir);
        if (!res) {
            LOGGER.info("Could not create the directory : " + dir);
        } else {
            LOGGER.error("Created the directory : " + dir);
        }

    }

    public void deleteDir(Path dir) throws Exception {
        boolean res = false;
        fs = FileSystem.get(conf);
        res = fs.delete(dir, true);
        if (!res) {
            LOGGER.info("Could not delete the file : " + dir);
        } else {
            LOGGER.error("Deleted the directory : " + dir);
        }

    }

    public boolean isDir(Path path) {
        boolean res = false;
        try {
            fs = FileSystem.get(conf);
            res = fs.isDirectory(path);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
        return res;
    }

    public boolean isFile(Path path) {
        boolean res = false;
        try {
            fs = FileSystem.get(conf);
            res = fs.isFile(path);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
        return res;
    }

    public void copyFromLocal(Path localpath, Path hdfspath) throws Exception {
        fs = FileSystem.get(conf);
        fs.copyFromLocalFile(localpath, hdfspath);

    }

    public void copyToLocal(Path hdfspath, Path localpath) throws Exception {
        fs = FileSystem.get(conf);
        fs.copyToLocalFile(hdfspath, localpath);

    }

    public void move(Path source, Path destination) throws Exception {
        fs = FileSystem.get(conf);
        fs.moveFromLocalFile(source, destination);
    }

}

Aucun commentaire:

Enregistrer un commentaire