创建文件使用webHdfs。

huangapple go评论41阅读模式
英文:

create file with 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;
}

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

英文:

I would like to create a file to hdfs with webhdfs, I wrote the function below

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

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

In the last print I don't see my file...
Any idea ?

答案1

得分: 0

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

}

英文:
public ResponseEntity createFile(MultipartFile f) throws URISyntaxException, RestClientException, IOException {
	URI uriPut = new URI(webhdfsBaseurl + &quot;/gateway/default/webhdfs/v1/dev/ep/flux_entrant/pg6/&quot;
			+ f.getOriginalFilename() + &quot;?op=CREATE&quot; + &quot;&amp;overwrite=true&amp;data=true&quot;);

	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 + &quot;?op=CREATE&quot; + &quot;&amp;overwrite=true&amp;data=true&quot;);
		ResponseEntity eFnal = restTemplate.exchange(uriPutFnal, HttpMethod.PUT,
				new HttpEntity&lt;Resource&gt;(f.getResource()), Map.class);

		if (eFnal.getStatusCode().is2xxSuccessful()) {
			URI uri = new URI(
					webhdfsBaseurl + &quot;/gateway/default/webhdfs/v1/dev/ep/flux_entrant/pg6&quot; + &quot;?op=LISTSTATUS&quot;);
			String test = restTemplate.getForObject(uri, String.class);
			System.out.println(test);

			return eFnal;
		}
	}

	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

}

huangapple
  • 本文由 发表于 2020年7月22日 03:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定