Python 访问 Azure Data Lake Store 的代码

Python code to access Azure Data Lake Store

我正在查看 Microsoft 文档 here and here,我在 Azure Active Directory 中创建了 Web 应用程序以访问 Data Lake Store

从 Web 应用程序我有 对象 ID应用程序 ID 和 密钥

查看文档我看到了这个:

adlCreds = lib.auth(tenant_id = 'FILL-IN-HERE', client_secret = 'FILL-IN-HERE', client_id = 'FILL-IN-HERE', resource = 'https://datalake.azure.net/')

如何使用它来验证我的代码和 运行 Data Lake Store 上的操作?

这是我的完整测试代码:

## Use this for Azure AD authentication
from msrestazure.azure_active_directory import AADTokenCredentials

## Required for Azure Data Lake Store account management
from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
from azure.mgmt.datalake.store.models import DataLakeStoreAccount

## Required for Azure Data Lake Store filesystem management
from azure.datalake.store import core, lib, multithread

# Common Azure imports
import adal
from azure.mgmt.resource.resources import ResourceManagementClient
from azure.mgmt.resource.resources.models import ResourceGroup

## Use these as needed for your application
import logging, getpass, pprint, uuid, time


## Declare variables
subscriptionId = 'FILL-IN-HERE'
adlsAccountName = 'FILL-IN-HERE'

tenant_id = 'FILL-IN-HERE'
client_secret = 'FILL-IN-HERE'
client_id = 'FILL-IN-HERE'


## adlCreds = lib.auth(tenant_id = 'FILL-IN-HERE', client_secret = 'FILL-IN-HERE', client_id = 'FILL-IN-HERE', resource = 'https://datalake.azure.net/')
from azure.common.credentials import ServicePrincipalCredentials
adlCreds = lib.auth(tenant_id, client_secret, client_id, resource = 'https://datalake.azure.net/')


## Create a filesystem client object
adlsFileSystemClient = core.AzureDLFileSystem(adlCreds, store_name=adlsAccountName)

## Create a directory
adlsFileSystemClient.mkdir('/mysampledirectory')

当我尝试运行代码时出现错误:

[运行] python "c:....\dls.py" 追溯(最近一次通话): 文件 "c:....\dls.py",第 38 行,位于 adlCreds = lib.auth(tenant_id, client_secret, client_id, resource = 'https://datalake.azure.net/') 文件 "C:\Python36\lib\site-packages\azure\datalake\store\lib.py",第 130 行,在 auth 密码中,client_id) 文件 "C:\Python36\lib\site-packages\adal\authentication_context.py",第 145 行,在 acquire_token_with_username_password 中 return self._acquire_token(token_func) 文件 "C:\Python36\lib\site-packages\adal\authentication_context.py",第 109 行,在 _acquire_token 中 return token_func(自己) 文件 "C:\Python36\lib\site-packages\adal\authentication_context.py",第 143 行,在 token_func 中 return token_request.get_token_with_username_password(用户名, 密码) 文件 "C:\Python36\lib\site-packages\adal\token_request.py",第 280 行,在 get_token_with_username_password 中 self._user_realm.discover() 文件 "C:\Python36\lib\site-packages\adal\user_realm.py",第 152 行,在发现中 提高 AdalError(return_error_string, error_response) adal.adal_error.AdalError:用户领域发现请求 returned http 错误:404 和服务器响应:

404 - 找不到文件或目录。

服务器错误

404 - 找不到文件或目录。

您正在查找的资源可能已被删除、名称已更改或暂时不可用。

[完成] 在 1.216 秒内以代码=1 退出

有两种不同的验证方式。第一个是交互式的,适合最终用户。它甚至适用于多因素身份验证。 这是你如何做的。您需要互动才能登录。

from azure.datalake.store import core, lib, multithread
token = lib.auth()

第二种方法是使用 Azure 活动目录中的服务主体身份。此处提供了有关设置 Azure AD 应用程序、检索客户端 ID 和密码以及使用 SPI 配置访问的分步教程:https://docs.microsoft.com/en-us/azure/data-lake-store/data-lake-store-service-to-service-authenticate-using-active-directory#create-an-active-directory-application

from azure.common.credentials import ServicePrincipalCredentials
token = lib.auth(tenant_id = '<your azure tenant id>', client_secret = '<your client secret>', client_id = '<your client id>')

这里是博客 post,展示了如何通过 pandas 和 Jupyter 访问它。它还逐步介绍了如何获取身份验证令牌。 https://medium.com/azure-data-lake/using-jupyter-notebooks-and-pandas-with-azure-data-lake-store-48737fbad305