通过CURL将文件上传到HDFS

时间:2015-11-24 08:10:10

标签: php hadoop curl hdfs

我仍然坚持如何通过cURL将文件上传到HDFS。我之前在cURL CLI上尝试了这个并且它有效,但当我尝试制作运行相同命令的PHP代码时,它并不想上传。

这是命令:

curl -i -X PUT -T "c:/xampp/htdocs/experiment/csv.php" "http://123.123.78.9:14000/webhdfs/v1/user/flume/test2?op=CREATE&buffersize=1024&user.name=flume&blocksize=1048576&overwrite=false&data=true&permission=755" --header "Content-Type: application/octet-stream"

这是PHP代码:

function call_curl($headers, $method, $url, $data)
{           
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

    switch($method) {
            case 'GET':
                break;
            case 'POST':
                curl_setopt($handle, CURLOPT_POST, true);
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
            case 'PUT': 
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
            case 'DELETE':
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
           }

    $response = curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);
    return $response;
}

$header = array('Content-Type: application/octet-stream');
$method = "PUT";
$url =  "http://123.123.78.9:14000/webhdfs/v1/user/flume/filter/".$target_file."? op=CREATE&overwrite=true&blocksize=1048576&permission=777&buffersize=1024&user.name=flume"; 
$data = array('@c:/xampp/htdocs/experiment/'.$target_file);
$call_2 = call_curl($header, $method, $url, $data);

0 个答案:

没有答案