java实现相关hdfs操作【转载】

调用相关代码,实现相关hdfs操作

package hdfs;

import java.io.InputStream;
import java.net.URL;

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;

public class App1 {
    /**
     * 异常:unknown host: chaoren 本机没有解析主机名chaoren
     * 在C:\Windows\System32\drivers\etc\hosts文件中添加192.168.80.100
     * chaoren(win10中要添加写入权限才能写入)
     */
    static final String PATH = "hdfs://chaoren:9000/hello";

    public static void main(String[] args) throws Exception {
        // 让URL能够解析hdfs协议
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        URL url = new URL(PATH);
        InputStream in = url.openStream();
        /**
         * @param in
         *            输入流
         * @param out
         *            输出流
         * @param buffSize
         *            缓冲大小
         * @param close
         *            在传输结束后是否关闭流
         */
        IOUtils.copyBytes(in, System.out, 1024, true);// 读取文件hello中的内容
    }

}
package hdfs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class App2 {
    static final String PATH = "hdfs://chaoren:9000/";
    static final String DIR = "/d1";
    static final String FILE = "/d1/hello";

    public static void main(String[] args) throws Exception {
        FileSystem fileSystem = getFileSystem();

        // 创建文件夹 hadoop fs -mkdir /d1
        mkDir(fileSystem);

        // 上传文件 hadoop fs -put src des
        putData(fileSystem);

        // 下载文件 hadoop fs -get src des
        getData(fileSystem);

        // 浏览文件夹 hadoop fs -lsr path
        list(fileSystem);

        // 删除文件夹 hadoop fs -rmr /d1
        remove(fileSystem);
    }

    private static void remove(FileSystem fileSystem) throws IOException {
        fileSystem.delete(new Path(DIR), true);
    }

    private static void list(FileSystem fileSystem) throws IOException {
        FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            String isDir = fileStatus.isDir() ? "文件夹" : "文件";
            String permission = fileStatus.getPermission().toString();
            int replication = fileStatus.getReplication();
            long len = fileStatus.getLen();
            String path = fileStatus.getPath().toString();
            System.out.println(isDir + "\t" + permission + "\t" + replication
                    + "\t" + len + "\t" + path);
        }
    }

    private static void getData(FileSystem fileSystem) throws IOException {
        FSDataInputStream inputStream = fileSystem.open(new Path(FILE));
        IOUtils.copyBytes(inputStream, System.out, 1024, true);
    }

    private static void putData(FileSystem fileSystem) throws IOException,
            FileNotFoundException {
        FSDataOutputStream out = fileSystem.create(new Path(FILE));
        FileInputStream in = new FileInputStream("C:/Users/ahu_lichang/cp.txt");// 斜杠方向跟Windows下是相反的
        IOUtils.copyBytes(in, out, 1024, true);
    }

    private static void mkDir(FileSystem fileSystem) throws IOException {
        fileSystem.mkdirs(new Path(DIR));
    }

    private static FileSystem getFileSystem() throws IOException,
            URISyntaxException {
        FileSystem fileSystem = FileSystem.get(new URI(PATH),
                new Configuration());
        return fileSystem;
    }

}

http://www.cnblogs.com/ahu-lichang/p/6641876.html#undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值