java.io.IOException: No FileSystem for scheme: hdfs
2017-12-26 11:07
176 查看
最近在做一个大数据项目,用到hadoop(2.7.4)、spark(2.11),在做一个数据分片的时候,单独将程序打包提交任务到spark上执行没有任何问题,但是集成到web项目中后,就来问题了
这个问题有两种解决方法,第一种就是修改集群配置文件
core-site.xml,添加属性
这种方法有个缺点就是集群都跑起来了,还不知道有多少任务正在跑着,你跟人家停了,那不歇菜了!!!
另一种方法,那就是修改自己的代码:
在使用FileSystem的地方,设置
需要知道为什么这样做,参考这位大兄弟的博客, 大致意思就是。。。呃也没什么说的就是找不到配置项
下面贴一个操作hadoop的常用方法
Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
这个问题有两种解决方法,第一种就是修改集群配置文件
core-site.xml,添加属性
<property> <name>fs.hdfs.impl</name> <value>org.apache.hadoop.hdfs.DistributedFileSystem</value> <description>The FileSystem for hdfs: uris.</description> </property>
这种方法有个缺点就是集群都跑起来了,还不知道有多少任务正在跑着,你跟人家停了,那不歇菜了!!!
另一种方法,那就是修改自己的代码:
configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
在使用FileSystem的地方,设置
org.apache.hadoop.hdfs.DistributedFileSystem
需要知道为什么这样做,参考这位大兄弟的博客, 大致意思就是。。。呃也没什么说的就是找不到配置项
fs.hdfs.impl
下面贴一个操作hadoop的常用方法
public static void append(String path, String line) throws IOException { BufferedWriter writer = null; FSDataOutputStream fsout = null; try { conf = new Configuration(); conf.setBoolean("dfs.support.append", true);// 允许追加 conf.set("hadoop.user", "hadoop");//设置用户 conf.setBoolean("mapreduce.app-submission.cross-platform", true);//设置跨平台提交 conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");//设置使用hdfs分布式文件系统 FileSystem fs = FileSystem.get(URI.create(path), conf); Path pathpath = new Path(path); if (!fs.exists(pathpath)) { fs.create(pathpath).close();// 创建完成后关闭 } fsout = fs.append(pathpath);// 追加 writer = new BufferedWriter(new OutputStreamWriter(fsout)); writer.write(line); writer.newLine(); writer.flush(); } catch (IllegalArgumentException | IOException e) { System.err.println("追加文件失败\t" + e.getMessage()); } finally { if (fsout != null) fsout.close(); if (writer != null) writer.close(); } } /** * 向hdfs中上传本地文件 * * @param dst * hdfs目录 * @param src * 本地文件 * @return 上传是否成功,成功true,删除本地数据,否则失败;如果 */ public static boolean put(String dst, String... src) { try { conf = new Configuration(); conf.set("hadoop.user", "hadoop"); conf.setBoolean("mapreduce.app-submission.cross-platform", true); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs = FileSystem.get(URI.create(dst), conf); Path[] srcs = new Path[src.length]; for (int i = 0; i < src.length; i++) { srcs[i] = new Path(src[i]); } fs.copyFromLocalFile(true, false, srcs, new Path(dst)); } catch (IllegalArgumentException | IOException e) { System.err.println("文件上传失败\t" + e.getMessage()); return false; } return true; } /** * 判断要给文件或者目录是否存在 * * @param dir * 文件或目录 * @return 是否存在 */ public static boolean exist(String dir) { if (StringUtils.isBlank(dir)) { return false; } if (!dir.startsWith(P_HDFS)) { dir = HDFS_URI + dir; } Configuration conf = new Configuration(); conf.set("hadoop.user", "hadoop"); conf.setBoolean("mapreduce.app-submission.cross-platform", true); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); try { FileSystem fs = FileSystem.get(URI.create(dir), conf); return fs.exists(new Path(dir)); } catch (IllegalArgumentException | IOException e) { System.err.println("错误\t" + e.getMessage()); return false; } } public static boolean createDir(String dir) { if (StringUtils.isBlank(dir)) { return false; } if (!dir.startsWith(P_HDFS)) { dir = HDFS_URI + dir; } Configuration conf = new Configuration(); conf.set("hadoop.user", "hadoop"); conf.setBoolean("mapreduce.app-submission.cross-platform", true); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); try { FileSystem fs = FileSystem.get(URI.create(dir), conf); return fs.mkdirs(new Path(dir)); } catch (IllegalArgumentException | IOException e) { System.err.println("错误\t" + e.getMessage()); return false; } } /** * 删除一个hdfs目录 * * @param dir * hdfs目录 * @return 删除是否成功 */ public static boolean deleteDir(String dir) { if (StringUtils.isBlank(dir)) { return false; } if (!dir.startsWith(P_HDFS)) { dir = HDFS_URI + dir; } Configuration conf = new Configuration(); conf.set("hadoop.user", "hadoop"); conf.setBoolean("mapreduce.app-submission.cross-platform", true); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); boolean ret = false; try { FileSystem fs = FileSystem.get(URI.create(dir), conf); ret = fs.delete(new Path(dir), true); fs.close(); } catch (IOException e) { System.err.println("错误\t" + e.getMessage()); ret = false; } return ret; }
相关文章推荐
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs异常解决
- spark运行java-jar:Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs异常解决
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- 解决:java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- spark程序异常:Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: hdfs
- Java.io.IOException: No FileSystem for scheme: hdfs
- java.io.IOException: No FileSystem for scheme: file spark hadoop
- maven将hadoop依赖打包成jar,运行jar后报错 java.io.IOException: No FileSystem for scheme
- 【甘道夫】HBase开发环境搭建过程中可能遇到的异常:No FileSystem for scheme: hdfs
- No FileSystem for scheme: hdfs