2017-12-27 1057 views
1

我正在使用Azure Data Lake上傳文件,但仍想將文本文件內容添加到現有的Data Lake文本文件中。是否有任何選項可用於在C#?中使用Web HDFS REST API附加文本文件數據。如何在C#中使用Web HDFS REST API追加文件?

我是指這個鏈接enter link description here

代碼:我可以參考上面的鏈接獲得附加網址。但是,我如何使用這個URL並使用c#追加文件?

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

回答

2

如果您想使用Rest Api來做到這一點,我們可以使用下面的代碼。我用郵差測試它。

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; 
} 

enter image description here

我們還可以使用Azure的Microsoft.Azure.Management.DataLake.Store做到這一點。如何獲取應用程序ID和密鑰,您可以參考official document。更詳細的步驟來獲得訪問datalake的權限,你可以參考另一個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; 

enter image description here

包:

<?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> 
+0

的REST API代碼爲我工作。非常感謝@ Tom Sun – Saravanakumar