Search in sources :

Example 1 with WebHdfsParamsBuilder

use of com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder in project azure-tools-for-java by Microsoft.

the class WebHDFSDeploy method getArtifactUploadedPath.

@Nullable
public String getArtifactUploadedPath(String rootPath) throws URISyntaxException {
    final List<NameValuePair> params = new WebHdfsParamsBuilder("OPEN").build();
    final URIBuilder uriBuilder = new URIBuilder(rootPath);
    uriBuilder.addParameters(params);
    return uriBuilder.build().toString();
}
Also used : NameValuePair(org.apache.http.NameValuePair) WebHdfsParamsBuilder(com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder) URIBuilder(org.apache.http.client.utils.URIBuilder) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable)

Example 2 with WebHdfsParamsBuilder

use of com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder in project azure-tools-for-java by Microsoft.

the class JobUtils method deployArtifact.

public static Observable<String> deployArtifact(@NotNull SparkBatchSubmission submission, @NotNull String destinationRootPath, @NotNull String artifactPath) {
    return Observable.fromCallable(() -> {
        final File file = new File(artifactPath);
        final String webHdfsUploadPath = destinationRootPath + file.getName();
        final String redirectUri;
        List<NameValuePair> params = new WebHdfsParamsBuilder("CREATE").setOverwrite("true").build();
        URIBuilder uriBuilder = new URIBuilder(webHdfsUploadPath);
        uriBuilder.addParameters(params);
        HttpUriRequest req = RequestBuilder.put(uriBuilder.build()).build();
        final CloseableHttpClient httpclient = submission.getHttpClient();
        try (final CloseableHttpResponse response = httpclient.execute(req)) {
            // two steps to upload via webhdfs
            // 1.put request the get 307 redirect uri from response
            // 2.put redirect request with file content as setEntity
            redirectUri = response.getFirstHeader("Location").getValue();
            if (StringUtils.isBlank(redirectUri)) {
                throw new UnknownServiceException("can not get valid redirect uri using webhdfs");
            }
        } catch (final Exception ex) {
            throw new UnknownServiceException("using webhdfs encounter problem:" + ex.toString());
        }
        final InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(true);
        final BufferedHttpEntity reqEntityBuf = new BufferedHttpEntity(reqEntity);
        // setup url with redirect url and entity ,config 100 continue to header
        req = RequestBuilder.put(redirectUri).setEntity(reqEntityBuf).setConfig(RequestConfig.custom().setExpectContinueEnabled(true).build()).build();
        // execute put request
        try (final CloseableHttpResponse putResp = httpclient.execute(req)) {
            params = new WebHdfsParamsBuilder("OPEN").build();
            uriBuilder = new URIBuilder(webHdfsUploadPath);
            uriBuilder.addParameters(params);
            // return get file uri
            return uriBuilder.build().toString();
        }
    });
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) UnknownServiceException(java.net.UnknownServiceException) WebHdfsParamsBuilder(com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder) URISyntaxException(java.net.URISyntaxException) AuthenticationException(com.microsoft.azure.hdinsight.sdk.common.AuthenticationException) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) UnknownServiceException(java.net.UnknownServiceException) HDIException(com.microsoft.azure.hdinsight.sdk.common.HDIException) ExecutionException(java.util.concurrent.ExecutionException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) URIBuilder(org.apache.http.client.utils.URIBuilder) InputStreamEntity(org.apache.http.entity.InputStreamEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

WebHdfsParamsBuilder (com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 AuthenticationException (com.microsoft.azure.hdinsight.sdk.common.AuthenticationException)1 HDIException (com.microsoft.azure.hdinsight.sdk.common.HDIException)1 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)1 Nullable (com.microsoft.azuretools.azurecommons.helpers.Nullable)1 URISyntaxException (java.net.URISyntaxException)1 UnknownServiceException (java.net.UnknownServiceException)1 ExecutionException (java.util.concurrent.ExecutionException)1 NameValuePair (org.apache.http.NameValuePair)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1