java - 使用webHdfs创建文件

标签 java spring hadoop hdfs webhdfs

我想用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;
    }
在上次打印中,我看不到我的文件...
任何的想法 ?

最佳答案

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();

}

关于java - 使用webHdfs创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63021886/

相关文章:

hadoop - oozie java.io.IOException:方案:hdfs没有文件系统

java - NullPointerException 尝试加载文件

java - 使用异或将颜色与像素分开

java - Lambda 表达式和变量捕获

java - 是否有任何与 Spring 集成的 Java PDF 库?

java - 让 Broadleaf 在单元测试中运行

java - 如何在现有的 java web 应用程序中使用 Node.js 效率

java - 如何正确使用Spring Security中的hasRole?

hadoop - 资源管理器没有节点

azure - Spark-Shell错误: No FileSystem for scheme: wasb