遇到的问题

  1. 文件上传,没有报错,但是文件却没有上传成功
    最开始考虑的问题可能是电脑防火墙没有关闭

同时需要检查上传文件的相关参数是否配置

Configuration conf = new Configuration();
 conf.set(“fs.defaultFS”, “hdfs://nameservice1”);
 conf.set(“dfs.nameservices”, “nameservice1”);
 conf.set(“dfs.ha.namenodes.nameservice1”, “nn1,nn2”);
 conf.set(“dfs.namenode.rpc-address.nameservice1.nn1”,这里是HDFS地址);
 conf.set(“dfs.namenode.rpc-address.nameservice1.nn2”, 这里是HDFS地址);
 conf.set(“dfs.client.failover.proxy.provider.nameservice1”,

上述为配置方式示例

2、关闭防火墙,上传文件依然失败,fs.copyFromLocalFile(newPath, p)方法报空指针异常 和InvocationTargetException
出现这样异常的原因 (转载)
①远程代码ip加端口获取远程跨服拒绝连接和服务器未启动所导致的,是由调用方法或构造方法所抛出异常的受检查的异常.
②有可能web项目下的jar包缺失导致,详细检查控制台报出的异常日志信息,再对应查找相关jar包.或重复包
③在本机电脑测试出没有问题,打出war包确在war包运行服上报此异常, 请检查tomcat服务器版本,做好一致,或者war包服高于本服.
④开发jdk和部署jdk版本不一致,导致InvocationTargetException异常信息返回一个空值,调用不上重写的方法
这里出现fs.copyFromLocalFile(newPath, p)方法抛出异常,上传失败。

public String upFileToHDFS(String localPath, String targetPath) {
 String message = “ok”;
 String rootPath = HDFSUrl;
 System.setProperty(“HADOOP_USER_NAME”, hiveUser);
 Path newPath = new Path(localPath);
 String fileString = newPath.getName();
 if (fileString.contains("."))
 fileString = fileString.split("\.")[0];
 Path p = new Path(rootPath + targetPath + “/” + fileString);
 //出现上传失败的原因,可能这里参数未设置,考虑第一步参数设置
 Configuration conf = new Configuration();
 FileSystem fs;
 try {
 fs = p.getFileSystem(conf);
 if (!fs.exists§)
 fs.mkdirs§;
 String targetFilePathString = p.toString() + “/”
 + newPath.getName();
 Path targetFilePath = new Path(targetFilePathString);
 boolean is = fs.exists(targetFilePath);
 if (is) {
 message = “exist”;
 return message;
 }
 try {
 //如果这里出现无法读取文件的问题,抛出异常,那么请考虑上面配置参数是否已经设置
 fs.copyFromLocalFile(newPath, p);
 } catch (IOException e) {
 System.out.println(" 上传文件 “+e.getMessage());
 e.printStackTrace();
 }
 fs.close();
 } catch (IOException e) {
 System.out.println(” 上传文件 "+e.getMessage());
 e.printStackTrace();
 }
 return message;
 }

这里换了一种方式,使用IO流上传,同样提示上传成功,文件未上传。也没有报任何错误

private void uploadFile(String inURL、, String outURL) throws Exception{
 try {
 /** 省略部分不重要逻辑 包括访问用户名等**/
 /** 配置相关 **/
 Configuration conf = new Configuration();
 conf.set(“fs.defaultFS”, “hdfs://nameservice1”);
 conf.set(“dfs.nameservices”, “nameservice1”);
 conf.set(“dfs.ha.namenodes.nameservice1”, “nn1,nn2”);
 conf.set(“dfs.namenode.rpc-address.nameservice1.nn1”,这里是HDFS地址);
 conf.set(“dfs.namenode.rpc-address.nameservice1.nn2”, 这里是HDFS地址);
 conf.set(“dfs.client.failover.proxy.provider.nameservice1”,
 “org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider”);
/** hdfs 常见的获取文件系统 **/
    FileSystem fs = null;
    fs= FileSystem.get(conf);

    /** 查看目的路径是否存在 **/
    if(!(fs.exists(new Path(directory)))) {
        /** 如果路径不存在,则创建路径 **/
        fs.mkdirs(new Path(directory));
    }
/** 文件输入流**/
 FileInputStream in = new FileInputStream(new File(outURL));
 /** 获取文件输出流,配置文件输出地址 /
 OutputStream out = fs.create(new Path(outURL), new Progressable() {
 @Override
 public void progress() {
 System.out.println(“上传完一个文件”);
 }
 });
 / 复制文件 **/
 IOUtils.copyBytes(in,out,4096,true); //上传文件
in.close();
    out.close();
} catch (IOException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}
}

最终解决方案,jar导入错误问题,需要用到的jar包,同时需要保证参数配置

hadoop 上传文件工作原理 读取 hadoop上传文件到hdfs不成功_上传

部分Jar包导入的maven依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.2.0-transwarp</version>
</dependency>

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>2.2.0-transwarp</version>
</dependency>

上述添加jar包后,测试上传文件成功。