那最简单的IO说吧 在本地文件系统和HDFS文件系统中的IO操作
下面这个例子是合并本地文件系统的目录下的文件 到HDFS文件系统
public static void main(String[] args) throws IOException, URISyntaxException {
//Hadoop resource configuration class
Configuration conf=new Configuration();
//get FileSystem's instance
//call factory method FileSystem.get(Configuration conf)
FileSystem hdfs=FileSystem.get(new URI("hdfs://127.0.0.1:9000"),conf);
//local FileSystem's instance
FileSystem local =FileSystem.getLocal(conf);
//Path Object encode file and direcotory
Path inputDir=new Path("/home/hadoop/Documents"); //(args[0]);
Path hdfsFile=new Path("/mytest_example2.txt");
//use the FileSystem's listStatus()method() to get a list of files in a directory
FileStatus[] inputFiles=local.listStatus(inputDir);
//create FSDataOutputStream
FSDataOutputStream out=hdfs.create(hdfsFile);
//loop local files and merge a new file
for(int i=0;i<inputFiles.length;i++)
{
//user the Path to request an FSDataInputStream object for reading in the file
System.err.println(inputFiles[i].getPath().getName()+"-----------------");
FSDataInputStream in =local.open(inputFiles[i].getPath());
byte [] buffer=new byte[256];
int bytesRead=0;
while ((bytesRead=in.read(buffer))>0)
{
out.write(buffer, 0, bytesRead);
}
in.close();
}
out.close();
}
以上的代码是可行的没有什么错误。但是如果是一下这样,就会报错前提呢在你的项目的classpath下也就是bin目录下没有hadoop的HDFS配置文件(core-site.xml和hdfs-site.xml)
FileSystem hdfs=FileSystem.get(/*new URI("hdfs://127.0.0.1:9000"),*/conf);
Path hdfsFile=new Path("hdfs://127.0.0.1:9000/mytest_example2.txt");
错误提示:
log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
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://127.0.0.1:9000/, expected: file:///
at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:646)
at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:82)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirsWithOptionalPermission(RawLocalFileSystem.java:513)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:499)
at org.apache.hadoop.fs.ChecksumFileSystem.mkdirs(ChecksumFileSystem.java:594)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:448)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:435)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:909)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:890)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:787)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:776)
at com.liweichao.PutMerge.main(PutMerge.java:40)
解决途径一个是在初始化Filesystem的时候
1 : Configuration conf=new Configuration();
//get FileSystem's instance
//call factory method FileSystem.get(Configuration conf)
//就会自动加载到hadoop的资源配置文件
FileSystem hdfs=FileSystem.get(new URI("hdfs://127.0.0.1:9000"),conf);
在有Path对象定位的具体目录
Path hdfsFile=new Path("/mytest_example2.txt");
2:在你的项目的classpath下也就是bin目录下没有hadoop的HDFS配置文件(core-site.xml和hdfs-site.xml)
相关推荐
hadoop环境搭建和eclipse开发
Hadoop Eclipse是Hadoop开发环境的插件,用户在创建Hadoop程序时,Eclipse插件会自动导入Hadoop编程接口的jar文件,这样用户就可以在Eclipse插件的图形界面中进行编码、调试和运行Hadop程序,也能通过Eclipse插件...
hadoop2x-eclipse-plugin
eclipse配置hadoop,并且如何在eclipse中进行mapreduce的开发
目的很简单,为进行研究与学习,部署一个hadoop运行环境,并搭建一个hadoop开发与测试环境。 具体目标是: 1.在ubuntu系统上部署hadoop ...3.只需要在eclipse中“Run on Hadoop",不需要自己打包上传运行。
Hadoop在eclipse上面安装插件,以及对Hadoop jar包和hadoop-common jar包的安装,环境变量的配置
hadoop2.7.7版本 for eclipse-photon开发插件 。
包含hadoop2.6.0源码和eclipse开发hadoop所用插件,下载即可用。
Eclipse集成Hadoop2.10.0的插件,使用`ant`对hadoop的jar包进行打包并适应Eclipse加载,所以参数里有hadoop和eclipse的目录. 必须注意对于不同的hadoop版本,` HADDOP_INSTALL_PATH/share/hadoop/common/lib`下的jar包...
Hadoop安全分布式的Eclipse开发环境配置,附图片讲解。
windows下搭建hadoop开发环境(Eclipse)
适用用hadoop2 ,eclipse 集成插件。!可用在eclipse 开发hadoop程序
Windows下Cygwin环境的Hadoop安装- 在Eclipse中重新编译hadoop的jar包
用来配置myeclipse或eclipse对应的hadoop 插件,方便开发
他是可以在eclipse上显示和hadoop webui一样的功能并且在下载的时候也是很方便的!
hadoop-eclipse-plugin-2.8.3.jar hadoop eclipse插件,hadoop版本2.8.3,eclipse版本oxygen,亲测可以,用问题请留言:http://blog.csdn.net/familyshizhouna/article/details/78892500
hadoop-0.20.2-eclipse-plugin.jar, hadoop0.20.2版本 for eclipse