복붙노트

[HADOOP] webHDFS REST API를 통해 HDFS에 이미지를 업로드와 문제

HADOOP

webHDFS REST API를 통해 HDFS에 이미지를 업로드와 문제

나는 webHDFS REST API를 통해 HDFS에 파일을 작성하는 MultiPartEntity와 HttpPut을하고있는 중이 야. 요청 자체는 통과 내게 적절한 응답 (307) 아래 도시 및 유효한 화상 검색하고 열면 화상도의 일부로서 기입 다중 헤더를 갖는 단 (201)을 제공한다.

--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps 내용 - 처리 : 폼 데이터; = "파일을"이름; 파일 이름 = "advert.jpg" 콘텐츠 형식 : 응용 프로그램 / octet-stream을 ÿØÿàJFIFHHÿÛC 이미지 내용의 // 나머지 --8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps

이미지 파일에서 다중 헤더를 제거하면, 그것은 올바른 이미지 만들지 만 나는으로 시작하는 그것을 피할 수있는 방법을 모르겠습니다. 나는 webHDFS 실제로 파일을 작성 할 책임이 있기 때문에 나는이 제어 할 수있는 경우도 모르겠습니다.

여기에 내 코드입니다. 내가 일을해야 다른 뭔가가 있나요?

final String LOCATION = "Location";
final String writeURI = "http://<ip>:50070/webhdfs/v1/user/hadoop/advert.jpg"; 

HttpPut put = new HttpPut(writeURI);
HttpClient client = HttpClientBuilder.create().build();        
HttpResponse response = client.execute(put);
put.releaseConnection();

String redirectUri = null;
Header[] headers = response.getAllHeaders();
for(Header header : headers)
{
    if(LOCATION.equalsIgnoreCase(header.getName()))
    {
         redirectUri = header.getValue();
    }                    
}

HttpPut realPut = new HttpPut(redirectUri);
realPut.setEntity(buildMultiPartEntity("advert.jpg"));
HttpResponse response2 = client.execute(realPut);


private HttpEntity buildMultiPartEntity(String fileName)
{
   MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
   multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
   multipartEntity.addPart("file", new FileBody(new File(fileName)));
   return multipartEntity.build();
}    

어떤 도움에 감사드립니다.

해결법

  1. ==============================

    1.나는 파이썬 요청과 같은 문제를 만났다. 내가 마지막으로 그것을 해결하기 위해 한 일은 그것을 보내기 전에 메모리에 이미지를 읽는 것입니다. 대신 두의 webhdfs API에 한 단계 호출을 사용하여. 이 약간의 도움이 될 수 있기를 바랍니다.

    나는 파이썬 요청과 같은 문제를 만났다. 내가 마지막으로 그것을 해결하기 위해 한 일은 그것을 보내기 전에 메모리에 이미지를 읽는 것입니다. 대신 두의 webhdfs API에 한 단계 호출을 사용하여. 이 약간의 도움이 될 수 있기를 바랍니다.

    host_url = current_app.config.get('HDFS_URL', '')
    adx_img_path = current_app.config.get('ADX_CUSTOMER_IMAGE', '')
    real_path = adx_img_path + remotefile
    hdfs_username = current_app.config.get('HDFS_USERNAME', 'xdisk')
    parameters = '?user.name=' + hdfs_username + '&op=CREATE&data=true'
    img = open(localfile, 'rb').read()
    url = host_url + real_path + parameters
    r = requests.put(url, data=img, headers={"Content-Type": "application/octet-stream"})
    

    그것은 바이너리 / 바이트로 이미지를 읽는 것, 이상한 헤더 파일 헤더에 추가되지 않습니다. HttpClient를 사용중인 경우, 난 당신이 InputStreamBody 또는 ByteArrayBody를 시도 제안했다.

  2. ==============================

    2.콘텐츠 형식 "응용 프로그램 / octet-stream을"과 FileEntity, ByteArrayEntity 또는 InputStreamEntity로 이미지를 추가합니다.

    콘텐츠 형식 "응용 프로그램 / octet-stream을"과 FileEntity, ByteArrayEntity 또는 InputStreamEntity로 이미지를 추가합니다.

  3. ==============================

    3.이것은 허용 대답에 나 기지에 근무하는 코드입니다 :

    이것은 허용 대답에 나 기지에 근무하는 코드입니다 :

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.entity.FileEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    
    import java.io.File;
    import java.io.IOException;
    
    public class Test {
    
        public void Test(){
            try {
    
                final String writeURI = "http://<IP>:50075/webhdfs/v1/user/sample.xml?op=CREATE&user.name=istvan&namenoderpcaddress=quickstart.cloudera:8020&overwrite=true";
    
                HttpClient client = HttpClientBuilder.create().build();
    
                HttpPut put = new HttpPut(writeURI);
                put.setEntity(buildFileEntity("C:\\sample.xml"));
                put.setHeader("Content-Type", "application/octet-stream");
                HttpResponse response = client.execute(put);
    
                System.out.println(response);
    
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    
    
        private static FileEntity buildFileEntity (String fileName)
        {
            FileEntity inputData = new FileEntity(new File(fileName));
    
            return inputData;
        }
    
        public static void main(String[] args) {
            new Test().Test();
        }
    }
    

    메이븐 :

            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.4</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.3.1</version>
            </dependency>
    
  4. from https://stackoverflow.com/questions/23248890/issues-with-uploading-an-image-to-hdfs-via-webhdfs-rest-api by cc-by-sa and MIT license