java程序连接hadoop操作hdfs文件
出现异常:
java.io.IOException: No FileSystem for scheme: hdfs at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2658) at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2665) at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:93) at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2701) at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2683) at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:372) at com.ajia.data.synchronize.service.dataAnalysis.impl.DataAnalysisImpl.dataAnalysisSparkTask(DataAnalysisImpl.java:234) at com.ajia.data.synchronize.quartz.DataAnalysisTask.run(DataAnalysisTask.java:43) at sun.reflect.GeneratedMethodAccessor183.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269) at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:322) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:112) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
pom.xml
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.7</version> </dependency>
异常原因:
jar包冲突,上面的两个包都有org.apache.hadoop.fs.FileSystem,内容被覆盖造成无法访问到FileSystem
解决方法:
添加配置:configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
Configuration configuration=new Configuration(); configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs= FileSystem.get(URI.create("hdfs://master:9000"),configuration ); Path path = new Path("/tmp/test"); if (!fs.exists(path)) {fs.mkdirs(path);} fs.close();
解决方法2:
后来发现我的项目自动引入了hadoop-core 这个jar,我也不清楚是怎么依赖引入进来的,应该是各种依赖进来的,这就导致jar冲突,删除了就可以了。