【问题标题】:How to Append a File using Web HDFS REST API in C#?如何在 C# 中使用 Web HDFS REST API 附加文件?
【发布时间】:2017-12-28 13:22:52
【问题描述】:

我之前使用 Azure Data Lake 上传文件,但仍想追加现有 Data Lake 文本文件的文本文件内容。是否有任何选项可用于在 C# 中使用 Web HDFS REST API 附加文本文件数据?

我参考这个链接enter link description here

代码: 我可以参考上面的链接获取附加的 URL。但是我如何使用这个 URL 并使用 c# 附加一个文件?

private const string AppendUrl = "https://{0}.azuredatalakestore.net/webhdfs/v1/{1}?&op=APPEND&noredirect=true";

【问题讨论】:

    标签: c# rest api azure hadoop


    【解决方案1】:

    如果您想使用 Rest Api 来执行此操作,我们可以使用以下代码。我用 Postman 测试它。

    private const string AppendUrl = "https://{datalakeName}.azuredatalakestore.net/webhdfs/v1/{filepath}?append=true&op=APPEND&api-version=2016-11-01"
    var token = "eyJ0eX.....";
    using (var client = new HttpClient())
    {
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
         var result = client.GetAsync(url).Result;
         var data = result.Content.ReadAsStringAsync().Result;
    }
    

    我们也可以使用 Azure Microsoft.Azure.Management.DataLake.Store 来做到这一点。如何获取应用ID和秘钥可以参考official document。获取数据湖访问权限的更多详细步骤可以参考另一个SO thread

    var applicationId = "application Id";
    var secretKey = "secretKey";
    var tenantId = "tenant id";
    var adlsAccountName = "datalake account name";
    var creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, applicationId, secretKey).Result;
    var adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds,clientTimeoutInMinutes:60);
    var stream = File.OpenRead(@"C:\tom\testtext.txt");
    var test = adlsFileSystemClient.FileSystem.AppendWithHttpMessagesAsync(adlsAccountName, "test/abc.txt", stream).Result;
    

    包:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.Azure.Management.DataLake.Store" version="2.3.0-preview" targetFramework="net452" />
      <package id="Microsoft.Azure.Management.DataLake.StoreUploader" version="1.0.0-preview" targetFramework="net452" />
      <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net452" />
      <package id="Microsoft.Rest.ClientRuntime" version="2.3.9" targetFramework="net452" />
      <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.9" targetFramework="net452" />
      <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.0-preview" targetFramework="net452" />
      <package id="Newtonsoft.Json" version="9.0.2-beta1" targetFramework="net452" />
    </packages>
    

    【讨论】:

    • 其余的 Api 代码对我有用。非常感谢@Tom Sun
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2017-04-27
    • 2022-01-02
    • 2020-02-24
    • 2020-06-02
    相关资源
    最近更新 更多