【问题标题】:create file with webHdfs使用 webHdfs 创建文件
【发布时间】:2020-11-11 06:27:06
【问题描述】:

我想用webhdfs创建一个文件到hdfs,我写了下面的函数

public ResponseEntity createFile(MultipartFile f) throws URISyntaxException {
        URI uriPut = new URI(
                webhdfsBaseurl+
                "/gateway/default/webhdfs/v1/__MY_PATH/"+f.getName()+ 
                "?op=CREATE" + "&overwrite=true&data=true");
        ResponseEntity e = restTemplate.exchange(uriPut, HttpMethod.PUT,
                new HttpEntity<Resource>(f.getResource()), Map.class);
        URI location = e.getHeaders().getLocation();
        System.out.println(location.toString());

        URI uriPutFnal = new URI(
                location+
                "?op=CREATE" + "&overwrite=true&data=true");
        ResponseEntity eFnal = restTemplate.exchange(uriPutFnal, HttpMethod.PUT,
                new HttpEntity<Resource>(f.getResource()), Map.class);
        
        URI uri = new URI(webhdfsBaseurl+
                "/gateway/default/webhdfs/v1/_PATH_"+"?op=LISTSTATUS");
        String test = restTemplate.getForObject(uri, String.class);
        System.out.println(test);
        
        return eFnal;
    }

在最后一次打印中,我没有看到我的文件... 有什么想法吗?

【问题讨论】:

    标签: java spring hadoop hdfs webhdfs


    【解决方案1】:
    public ResponseEntity createFile(MultipartFile f) throws URISyntaxException, RestClientException, IOException {
        URI uriPut = new URI(webhdfsBaseurl + "/gateway/default/webhdfs/v1/dev/ep/flux_entrant/pg6/"
                + f.getOriginalFilename() + "?op=CREATE" + "&overwrite=true&data=true");
    
        ResponseEntity e = restTemplate.exchange(uriPut, HttpMethod.PUT, null, Map.class);
    
        if (e.getStatusCode().is3xxRedirection()) {
            URI location = e.getHeaders().getLocation();
            System.out.println(location.toString());
            URI uriPutFnal = new URI(location + "?op=CREATE" + "&overwrite=true&data=true");
            ResponseEntity eFnal = restTemplate.exchange(uriPutFnal, HttpMethod.PUT,
                    new HttpEntity<Resource>(f.getResource()), Map.class);
    
            if (eFnal.getStatusCode().is2xxSuccessful()) {
                URI uri = new URI(
                        webhdfsBaseurl + "/gateway/default/webhdfs/v1/dev/ep/flux_entrant/pg6" + "?op=LISTSTATUS");
                String test = restTemplate.getForObject(uri, String.class);
                System.out.println(test);
    
                return eFnal;
            }
        }
    
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 2015-05-13
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      相关资源
      最近更新 更多