使用C#代码从Data Lake Store读取文件的内容 [英] Read content of file from Data Lake Store using C# code

查看:86
本文介绍了使用C#代码从Data Lake Store读取文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Azure Data Lake Store文件中读取内容.但是,在连接/打开连接时,连接本身将失败&抛出异常

I am trying to read the content from the Azure Data Lake Store file. But while connecting / opening the connection itself fails & exception is thrown

 var stream = _adlsFileSystemClient.FileSystem.Open(_adlsAccountName, "/folder1/"+file.PathSuffix);

通过以下方式获取异常

Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown.

无法识别该问题. 如何解决此问题?

Unable to recognize the issue. How to resolve this issue?

谢谢

推荐答案

引发了类型为"Microsoft.Rest.Azure.CloudException"的异常.

Exception of type 'Microsoft.Rest.Azure.CloudException' was thrown.

这是基本的异常信息.因此,请尝试使用提琴手工具捕获详细的错误信息.

It is basic exception info. So please have a try to use fiddler tool to catch the detail error info.

根据您的代码,似乎未找到文件或授权异常.

According to your code, it seems that file is not found or authorization exception.

1.如果是404错误:请尝试使用以下格式并从azure门户中检查文件.

1.If it is 404 error: Please a try to use the following format and check the file from the azure portal.

 var srcPath = "/mytempdir/tomtest.txt";
 var stream = adlsFileSystemClient.FileSystem.Open(adlsAccountName, srcPath);

2.如果这是授权异常,并且您正在使用带有客户机密的服务到服务身份验证.请分配许可给azure AD应用程序来操作该文件.有关更多详细信息,请参阅

2.If it is the authorization exception and if you are using service-to-service authentication with client secret. Please assign permission to the azure AD Application to operate the file. More details please refer to the document.

以下是我的测试代码和屏幕截图:

The following is my test code and screenshot:

 var applicationId = "your application Id";
 var secretKey = "secret Key";
 var tenantId = "Your tenantId";
 var adlsAccountName = "adls account name";
 var creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, applicationId, secretKey).Result;
 var adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);
 var srcPath = "/mytempdir/tomtest.txt";
 var destPath = @"F:\tom\testfile.txt";
 using (var stream = adlsFileSystemClient.FileSystem.Open(adlsAccountName, srcPath))
 using (var fileStream = new FileStream(destPath, FileMode.Create))
 {
    stream.CopyTo(fileStream);
 }

Packages.config

Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Hyak.Common" version="1.0.2" targetFramework="net452" />
  <package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net452" />
  <package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.DataLake.Store" version="1.0.4" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.DataLake.StoreFileSystem" version="0.9.6-preview" targetFramework="net452" />
  <package id="Microsoft.Azure.Management.DataLake.StoreUploader" version="1.0.1-preview" targetFramework="net452" />
  <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net452" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net452" />
  <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
  <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.12" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
</packages>

这篇关于使用C#代码从Data Lake Store读取文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆