【问题标题】:Issues with Uploading an image to HDFS via webHDFS REST API通过 webHDFS REST API 将图像上传到 HDFS 的问题
【发布时间】:2014-04-23 15:28:49
【问题描述】:

我正在使用 MultiPartEntity 执行 HttpPut,以通过 webHDFS REST API 将文件写入 HDFS。请求本身通过并给了我正确的响应,307 和 201。但是,图像具有多部分标头,如下所示,它也作为其一部分写入,它不是一个有效的图像来检索和打开。

--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps
内容处置:表单数据;名称=“文件”;文件名="advert.jpg"
内容类型:应用程序/八位字节流

ÿØÿàJFIFHÿÛ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();
}    

感谢任何帮助。

【问题讨论】:

    标签: hadoop hdfs multipartform-data multipartentity webhdfs


    【解决方案1】:

    我在 python 请求中遇到了同样的问题。我最终解决它的方法是在发送之前将图像读入内存。并使用一步调用 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,我建议您尝试InputStreamBodyByteArrayBody

    【讨论】:

    • 我使用 ByteArrayEntity 来解决它。它也适用于 InputStreamEntity 和 FileEntity。
    【解决方案2】:

    将图像添加为 FileEntity、ByteArrayEntity 或 InputStreamEntity,Content-Type 为“application/octet-stream”。

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-24
        • 2021-05-09
        • 2018-05-19
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 2022-06-30
        • 2020-01-14
        相关资源
        最近更新 更多