码迷,mamicode.com
首页 > 其他好文 > 详细

Hadoop创建/删除文件夹出错

时间:2015-09-28 16:09:28      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.IllegalArgumentException: Wrong FS: hdfs://sandbox.hortonworks.com:8020/user/testdir, expected: file:///
    at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:645)
    at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:80)
    at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:423)
    at org.apache.hadoop.fs.ChecksumFileSystem.mkdirs(ChecksumFileSystem.java:590)
    at com.hdfs.directory.CreateDir.main(CreateDir.java:19)
在本机运行是会出现上面的错误,改成下面的代码:

package com.hdfs.directory;

import java.io.IOException;

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

public class CreateDir {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Configuration conf=new Configuration();
        Path newPath = new Path("hdfs://sandbox.hortonworks.com:8020/user/testdir");
        //FileSystem fs=FileSystem.get(conf);
        FileSystem fs = newPath.getFileSystem(conf);
        fs.mkdirs(newPath);
        fs.close();
//        FileStatus fileStatus=fs.getFileStatus(new Path("/user/long1657/20130908"));
        
    }

}

技术分享

或者hadoop相关的配置信息,放在工程目录下:

 

技术分享

执行上面的代码后:/user/testdir目录被成功删除:

技术分享

 

在将hadoop相关的配置文件添加到工程中后,用下面的代码就可以成功创建文件夹:

package com.hdfs.directory;

import java.io.IOException;

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

public class CreateDirWithConf {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Configuration conf=new Configuration();
    
        FileSystem fs=FileSystem.get(conf);

        fs.mkdirs(new Path("hdfs://sandbox.hortonworks.com:8020/user/testdir"));
        fs.close();
//        FileStatus fileStatus=fs.getFileStatus(new Path("/user/long1657/20130908"));
        
    }

}

 

Hadoop创建/删除文件夹出错

标签:

原文地址:http://www.cnblogs.com/justinzhang/p/4844137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!