简体   繁体   English

用于访问 Azure Data Lake Store 的 Python 代码

[英]Python code to access Azure Data Lake Store

I 'm looking at Microsoft Documentation here and here , I have created Web App in Azure Active Directory to access the Data Lake Store我正在此处此处查看 Microsoft 文档,我在 Azure Active Directory 中创建了 Web 应用程序以访问数据湖存储

From the Web App I have Object ID , Application ID and Key从 Web 应用程序我有对象 ID应用程序ID 和密钥

looking at the documentations I see this:查看文档我看到了这个:

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

how to use it to authenticate my code and run operation on Data Lake Store?如何使用它来验证我的代码并在 Data Lake Store 上运行操作?

here is my full test code:这是我的完整测试代码:

## 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')

when I try to ru the code I get error:当我尝试运行代码时出现错误:

[Running] python "c:....\\dls.py" Traceback (most recent call last): File "c:....\\dls.py", line 38, in adlCreds = lib.auth(tenant_id, client_secret, client_id, resource = ' https://datalake.azure.net/ ') File "C:\\Python36\\lib\\site-packages\\azure\\datalake\\store\\lib.py", line 130, in auth password, client_id) File "C:\\Python36\\lib\\site-packages\\adal\\authentication_context.py", line 145, in acquire_token_with_username_password return self._acquire_token(token_func) File "C:\\Python36\\lib\\site-packages\\adal\\authentication_context.py", line 109, in _acquire_token return token_func(self) File "C:\\Python36\\lib\\site-packages\\adal\\authentication_context.py", line 143, in token_func return token_request.get_token_with_username_password(username, password) File "C:\\Python36\\lib\\site-packages\\adal\\token_request.py", line 280, in get_token_with_username_password self._user_realm.discover() File "C:\\Python36\\lib\\site-packages\\adal\\user_realm.py", line 152, in discover raise AdalError(return_error_stri [运行] 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", line 130, in auth password, client_id) 文件“C:\\Python36\\lib\\site-packages\\adal\\authentication_context.py”,第 145 行,acquire_token_with_username_password 返回 self._acquire_token(token_func) 文件“C:\\Python36\\lib\\site-packages\\adal\\authentication_context .py”,第 109 行,在 _acquire_token 中返回 token_func(self) 文件“C:\\Python36\\lib\\site-packages\\adal\\authentication_context.py”,第 143 行,在 token_func 中返回 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 行,在发现 raise AdalError(return_error_stri ng, error_response) adal.adal_error.AdalError: User Realm Discovery request returned http error: 404 and server response: ng, error_response) adal.adal_error.AdalError: User Realm Discovery 请求返回 http 错误:404 和服务器响应:

404 - File or directory not found. 404 - 未找到文件或目录。

Server Error服务器错误

404 - File or directory not found. 404 - 未找到文件或目录。

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.您要查找的资源可能已被删除、更名或暂时不可用。

[Done] exited with code=1 in 1.216 seconds [完成] 在 1.216 秒内以代码 = 1 退出

There are two different ways of authenticating.有两种不同的认证方式。 The first one is interactive which is suitable for end users.第一个是交互式的,适合最终用户。 It even works with multi factor authentication.它甚至适用于多因素身份验证。 Here is how you do it.这是你如何做到的。 You need to be interactive in order to log on.您需要互动才能登录。

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

The second method is to use service principal identities in Azure Active directory.第二种方法是使用 Azure Active Directory 中的服务主体标识。 A step by step tutorial for setting up an Azure AD application, retrieving the client id and secret and configuring access using the SPI is available here: 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有关设置 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>')

Here is blog post that shows how to access it through pandas and Jupyter.这是博客文章,展示了如何通过 Pandas 和 Jupyter 访问它。 It also has a step by step on how to get the authentication token.它还逐步介绍了如何获取身份验证令牌。 https://medium.com/azure-data-lake/using-jupyter-notebooks-and-pandas-with-azure-data-lake-store-48737fbad305 https://medium.com/azure-data-lake/using-jupyter-notebooks-and-pandas-with-azure-data-lake-store-48737fbad305

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM