thrift 文件如下

namespace java com.pera.file.transform

struct  File{

    1:string path ,

    2:string content,

}



service FileTransform {

    bool exists(1:string path),

    void mkdir(1:string path ),

    void store(1:File file),

    set<string> ls(1:string path),

    bool isDirectory(1:string path),

    string getParentFile(1:string path),

    void createNewFile(1:string path),

    void deleteFile(1:string path),

    void deleteOnExit(1:string path),

    bool isFile(1:string path),

    void rename(1:string srcPath ,2:string destPath),

    string getName(1:string path),

    i64 totalSpace(1:string path),

    i64 usedSpace(1:string path),

    string read(1:string path)

}

客户端:

public class FileTransFormPair {

    

    private TTransport transport ;

    private Client  client  ;

    

    public FileTransFormPair(){}

    

    public FileTransFormPair( TTransport transport ,Client  client ){

        this.transport=transport ;

        this.client =client;

    }

    

    public TTransport getTransport() {

        return transport;

    }

    public void setTransport(TTransport transport) {

        this.transport = transport;

    }

    public Client getClient() {

        return client;

    }

    public void setClient(Client client) {

        this.client = client;

    }

}



public class FileTransFormClient implements Iface {



    public static  String SERVER_IP = "localhost";

    public static  int SERVER_PORT = 8090;

    public static  int TIMEOUT = 30000;

    

    public FileTransFormClient(String host,int port ,int timeout){

        SERVER_IP=host;

        SERVER_PORT=port;

        TIMEOUT=timeout;

    }

    

    private static   FileTransFormPair getClient() throws TTransportException{

        TTransport transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);

        TProtocol protocol = new TBinaryProtocol(transport);

        Client  client =new Client(protocol);

        transport.open();

        return new FileTransFormPair(transport, client);

    }

    

    @Override

    public boolean exists(String path) throws TException {

        boolean result =false;

        FileTransFormPair parameter =getClient();

        result =parameter.getClient().exists(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public void mkdir(String path) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().mkdir(path);

        parameter.getTransport().close();

        

    }





    @Override

    public void store(File file) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().store(file);

        parameter.getTransport().close();

        

    }





    @Override

    public Set<String> ls(String path) throws TException {

        FileTransFormPair parameter =getClient();

        Set<String> result =parameter.getClient().ls(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public boolean isDirectory(String path) throws TException {

        boolean result =false ;

        FileTransFormPair parameter =getClient();

        result =parameter.getClient().isDirectory(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public String getParentFile(String path) throws TException {

        FileTransFormPair parameter =getClient();

        String result =parameter.getClient().getParentFile(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public void createNewFile(String path) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().createNewFile(path);

        parameter.getTransport().close();

    }





    @Override

    public void deleteFile(String path) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().deleteFile(path);

        parameter.getTransport().close();

        

    }





    @Override

    public void deleteOnExit(String path) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().deleteOnExit(path);

        parameter.getTransport().close();

        

    }





    @Override

    public boolean isFile(String path) throws TException {

        FileTransFormPair parameter =getClient();

        boolean result =parameter.getClient().isFile(path);

        parameter.getTransport().close();

        return result ;

    }





    @Override

    public void rename(String srcPath, String destPath) throws TException {

        FileTransFormPair parameter =getClient();

        parameter.getClient().rename(srcPath, destPath);

        parameter.getTransport().close();

        

    }





    @Override

    public String getName(String path) throws TException {

        FileTransFormPair parameter =getClient();

        String result =parameter.getClient().getName(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public long totalSpace(String path) throws TException {

        FileTransFormPair parameter =getClient();

        long result =parameter.getClient().totalSpace(path);

        parameter.getTransport().close();

        return result;

    }





    @Override

    public long usedSpace(String path) throws TException {

        FileTransFormPair parameter =getClient();

        long result =parameter.getClient().usedSpace(path);

        parameter.getTransport().close();

        return result;

    }



    @Override

    public String read(String path) throws TException {

        FileTransFormPair parameter =getClient();

        String result =parameter.getClient().read(path);

        parameter.getTransport().close();

        return result;

    }

}

服务端:

public class ThriftServer {



    public static  int SERVER_PORT ;



    private static TServer server =null;

    

    private  static Log  LOG =LogFactory.getLog(ThriftServer.class);

    

    public void init(){

        SERVER_PORT = 9090;

        LOG.info("init the thriftServer");

    }

    

    public void start() throws TTransportException {

        TProcessor tprocessor = new FileTransform.Processor<FileTransform.Iface>(

                new FileStoreService());

        TServerSocket serverTransport = new TServerSocket(SERVER_PORT);

        TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(

                serverTransport);

        ttpsArgs.processor(tprocessor);

        ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());

        server = new TThreadPoolServer(ttpsArgs);

        server.serve();

        LOG.info("start the thriftServer");

    }



    public void stop() {

        if(null!=server){

            server.stop();

            LOG.info("stop the thriftServer");

        }

    }



    public static void main(String[] args) {

        ThriftServer server =new ThriftServer();

        server.init();

        try {

            server.start();

        } catch (TTransportException e) {

            e.printStackTrace();

        }

    }



}

public class FileStoreService implements Iface{



    private Configuration getConfiguration(){

        Configuration  conf =new Configuration();

        Properties  pro = new Properties();

        try {

            pro.load(new FileInputStream(new File("conf/filetransform-site.properties")));

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

//        conf.set("dfs.default.name", pro.getProperty("dfs.default.name"));

        

        return conf;

    }

    @Override

    public boolean exists(String path) throws TException {

        Configuration  conf =getConfiguration();

        FileSystem fs=null;

        try {

            fs = FileSystem.get(conf);

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }

        boolean exists =false ;

        try {

            exists  = fs.exists(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return exists;

    }



    @Override

    public void mkdir(String path) throws TException {

        Configuration  conf =getConfiguration();

        FileSystem fs=null;

        try {

            fs = FileSystem.get(conf);

            fs.mkdirs(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

    }



    @Override

    public void store(com.pera.file.transform.gen.File file) throws TException {

        String path =file.getPath();

        String content =file.getContent();

        Configuration conf  =getConfiguration();

        FileSystem  fs =null;

        FSDataOutputStream out=null;

        try {

            Path p =new Path(path);

            fs =FileSystem.get(conf);

            if(!fs.exists(p.getParent())){

                throw new TException( path+" is not exists");

            }

            out =fs.create(p);

            if(null!=content){

                byte   [] tmp =content.getBytes();

                out.write(tmp);

                out.flush();

                out.close();

            }

        } catch (Exception e) {

            throw new TException(e.getMessage());

        }finally{

            try {

                if(null!=out){

                    out.close();

                }

            } catch (IOException e) {

                throw new TException(e.getMessage());

            }

            try {

                if(null!=fs){

                    fs.close();

                }

            } catch (IOException e) {

                throw new TException(e.getMessage());

            }

            

        }

    }



    @Override

    public Set<String> ls(String path) throws TException {

        Configuration  conf =getConfiguration();

        FileSystem fs =null;

        Set<String>  result =new HashSet<String>();

        try {

            fs=FileSystem.get(conf);

            FileStatus   [] fstatus=fs.listStatus(new Path(path));

            int length =fstatus==null?0:fstatus.length;

            for (int i = 0; i < length; i++) {

                String url =fstatus[i].getPath().toString();

                result.add(url);

            }

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return result;

    }



    

    @Override

    @SuppressWarnings("deprecation")

    public boolean isDirectory(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        boolean result =false ;

        try {

            fs =FileSystem.get(conf);

            result =fs.isDirectory(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return result;

    }



    @Override

    public String getParentFile(String path) throws TException {

        Path parentPath =new Path(path).getParent();

        return parentPath.toString();

    }



    @Override

    public void createNewFile(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        try {

            fs =FileSystem.get(conf);

            fs.createNewFile(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

    }



    @Override

    public void deleteFile(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        try {

            fs =FileSystem.get(conf);

            fs.deleteOnExit(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        

    }



    @Override

    public void deleteOnExit(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        try {

            fs =FileSystem.get(conf);

            fs.deleteOnExit(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        

    }



    @Override

    public boolean isFile(String path) throws TException {

        

        boolean isFile =false ;

        

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        try {

            fs =FileSystem.get(conf);

            isFile =fs.isFile(new Path(path));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return isFile;

    }



    @Override

    public void rename(String srcPath, String destPath) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        try {

            fs =FileSystem.get(conf);

            fs.rename(new Path(srcPath), new Path(destPath));

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        

    }



    @Override

    public String getName(String path) throws TException {

        String name  =new Path(path).getName();

        

        return name;

    }



    @Override

    public long totalSpace(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        long length =0 ;

        try {

            fs =FileSystem.get(conf);

            length =fs.getFileStatus(new Path(path)).getLen();

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return length;

    }



    @Override

    public long usedSpace(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        long used =0 ;

        try {

            fs =FileSystem.get(conf);

            used =fs.getUsed();

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return used;

    }

    

    @Override

    public String read(String path) throws TException {

        Configuration conf =getConfiguration();

        FileSystem fs =null;

        String result   ="" ;

        try {

            fs =FileSystem.get(conf);

            FSDataInputStream  in =fs.open(new Path(path));

            int length =in.available();

            byte  [] tmp = new byte  [length] ;

            in.readFully(tmp);

            result  =new String(tmp);

        } catch (IOException e) {

            throw new TException(e.getMessage());

        }finally{

            if(null!=fs){

                try {

                    fs.close();

                } catch (IOException e) {

                    throw new TException(e.getMessage());

                }

            }

        }

        return result;

    }

}

thrift实现HDFS文件操作的更多相关文章

  1. Hadoop之HDFS文件操作常有两种方式(转载)

    摘要:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式.本文介绍如何利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件    命令行     Java API HD ...

  2. hadoop的hdfs文件操作实现上传文件到hdfs

    这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上.从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下hdfs文件操作操作示例,包括上传文件到 ...

  3. HDFS文件操作(命令行)

    HDFS是一种分布式文件系统,为MapReduce这种框架下的海量数据分布式处理而设计. Hadoop之HDFS文件操作常有两种方式,一种是命令行方式,即Hadoop提供了一套与Linux文件命令类似 ...

  4. Hadoop之HDFS文件操作

    摘要:Hadoop之HDFS文件操作常有两种方式.命令行方式和JavaAPI方式.本文介绍怎样利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件    命令行     Java API HD ...

  5. JAVA API 实现hdfs文件操作

    java api 实现hdfs 文件操作会出现错误提示: Permission denied: user=hp, access=WRITE, inode="/":hdfs:supe ...

  6. HDFS文件操作

    hadoop装好后,文件系统中没有任何目录与文件 1. 创建文件夹 hadoop fs -mkdir -p /hkx/learn 参数-p表示递归创建文件夹 2. 浏览文件 hadoop fs -ls ...

  7. eclipse 对 hadoop1.2.1 hdfs 文件操作

    package com.hdfs; import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io ...

  8. Hadoop HDFS文件操作

    1.创建目录 import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.ha ...

  9. [bigdata] 使用Flume hdfs sink, hdfs文件未关闭的问题

    现象: 执行mapreduce任务时失败 通过hadoop fsck -openforwrite命令查看发现有文件没有关闭. [root@com ~]# hadoop fsck -openforwri ...

随机推荐

  1. Java并发框架——什么是AQS框架

    什么是AQS框架 1995年sun公司发布了第一个java语言版本,可以说从jdk1.1到jdk1.4期间java的使用主要是在移动应用和中小型企业应用中,在此类领域中基本不用设计大型并发场景,当然也 ...

  2. Python optparser库详解

    一直以来对optparser不是特别的理解,今天就狠下心,静下心研究了一下这个库.当然了,不敢说理解的很到位,但是足以应付正常的使用了.废话不多说,开始今天的分享吧. 简介 optparse模块主要用 ...

  3. shell test和find命令实例解析

    shell test和find命令实例解析 下面以\build\core\product.mk相关部分来学习 define _find-android-products-files $(shell t ...

  4. XMPP(一)-openfire服务端的安装和搭建

    XMPP全称:可扩展通讯和表示协议 简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯.表示和需求响应服务中的XML数据元流式传输.XMPP以Jabber协议为基础,而Jabber是即时通 ...

  5. 关于AndroidSDK配置时的tools目录下找不到adb.exe的错误

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在配置android SDK的时候,有时会发现在android-sdk-windows\tools目录下并没有adb.exe,这 ...

  6. Xcode7.2中如何添加一个Empty Application模板

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Xcode 6.0正式版之后已经没有所谓的Empty Appl ...

  7. UNIX环境高级编程——信号(API)

    一.信号在内核中的表示     实际执行信号的处理动作称为信号递达(Delivery),信号从产生到递达之间的状态,称为信号未决(Pending).进程可以选择阻塞(Block)某个信号.被阻塞的信号 ...

  8. RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)

            在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇 ...

  9. java反射机制--reflection

    反射,reflection,听其名就像照镜子一样,可以看见自己也可以看见别人的每一部分.在java语言中这是一个很重要的特性.下面是来自sun公司官网关于反射的介绍:    Reflection is ...

  10. 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM

    http://blog.csdn.net/pipisorry/article/details/49445387 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...