如何使用webhdfs api访问azuredatalake

cetgtptt  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(341)

我们刚刚开始评估azure的datalake服务。我们创建了我们的湖,通过门户我们可以看到服务的两个公共URL(一个是https://scheme,另一个是adl://scheme)
datalake文档声明确实有两个接口:webhdfs restapi和adl。***但是,我在azure上找不到有关使用此接口的更多信息。
我试着用web浏览器和curl戳给定的https://url。服务正在响应。回复是json,这和预期的一样,因为datalake是hadoop的一个示例。然而,我似乎无法访问我的文件[我通过门户网站上传到我们的湖中]。
例如,如果我执行get to“/foo.txt”,则回复是一个错误,resourcenotfound。
如果我使用典型的hadoophdfs语法执行get,“/webhdfs/v1/foo.txt”,则回答是一个错误,authenticationfailed。其他文本表示缺少访问令牌。这似乎更有希望。但是,找不到有关生成此类访问令牌的任何信息。
有一些关于使用adl接口,以及.net和visualstudio的文档,但这不是我最初想要的。
非常感谢您的帮助!

v09wglhw

v09wglhw1#

我很感激这个论坛的马修希克斯职位概述了如何做到这一点 curl . 我把它包在powershell里。我相信有很多方法可以做到这一点,但这里有一个可行的方法。
首先设置一个aad应用程序,以便您可以填写下面提到的client\u id和client\u secret(假设您希望自动执行此操作,而不是交互式登录。如果您想要交互式登录,那么在上面的论坛帖子中会有一个指向该方法的链接。)
然后在前5行中填写设置并运行以下powershell脚本:

$client_id = "<client id>";
$client_secret = "<secret>";
$tenant = "<tenant>";
$adlsAccount = "<account>";
cd D:\path\to\curl

# authenticate

$cmd = { .\curl.exe -X POST https://login.microsoftonline.com/$tenant/oauth2/token  -F grant_type=client_credentials       -F resource=https://management.core.windows.net/       -F client_id=$client_id       -F client_secret=$client_secret };
$responseToken = Invoke-Command -scriptblock $cmd;
$accessToken = (ConvertFrom-Json $responseToken).access_token;

# list root folders

$cmd = {.\curl.exe -X GET -H "Authorization: Bearer $accessToken" https://$adlsAccount.azuredatalakestore.net/webhdfs/v1/?op=LISTSTATUS };
$foldersResponse = Invoke-Command -scriptblock $cmd;

# loop through directories directories

(ConvertFrom-Json $foldersResponse).FileStatuses.FileStatus | ForEach-Object { $_.pathSuffix }

# list files in one folder

$cmd = {.\curl.exe -X GET -H "Authorization: Bearer $accessToken" https://$adlsAccount.azuredatalakestore.net/webhdfs/v1/weather/?op=LISTSTATUS };
$weatherResponse = Invoke-Command -scriptblock $cmd;
(ConvertFrom-Json $weatherResponse).FileStatuses.FileStatus | ForEach-Object { $_.pathSuffix }

# download one file

$cmd = {.\curl.exe -L "https://$adlsAccount.azuredatalakestore.net/webhdfs/v1/weather/2007small.csv?op=OPEN" -H "Authorization: Bearer $accessToken" -o d:\temp\curl\2007small.csv };
Invoke-Command -scriptblock $cmd;

# upload one file

$cmd = {.\curl.exe -i -X PUT -L "https://$adlsAccount.azuredatalakestore.net/webhdfs/v1/weather/new2007small.csv?op=CREATE" -T "D:\temp\weather\smallcsv\new2007small.csv" -H "Authorization: Bearer $accessToken" };
Invoke-Command -scriptblock $cmd;

相关问题