2017-06-22 53 views
0

我想我datalake store中創建新的文件夾中創建文件夾,裏面是在代碼中沒有錯誤但沒有被反映在datalake storeAzure的數據湖店通過C#腳本

代碼:

using System; 
using System.Collections.Generic; 

using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Threading; 

using Microsoft.Azure.Management.DataLake.Store; 
using Microsoft.Azure.Management.DataLake.Store.Models; 
using Microsoft.Rest.Azure.Authentication; 
namespace test_dlstore 
{ 
    class Program 
    { 
     private static DataLakeStoreAccountManagementClient _adlsClient; 
     private static DataLakeStoreFileSystemManagementClient _adlsFileSystemClient; 

     private static string _adlsAccountName; 
     private static string _subId; 
     private static void Main(string[] args) 
     { 
      _adlsAccountName = "[email protected]"; 

      _subId = "2342342-97ce-a54b2-ba6e-234234234234234"; 

      string localFolderPath = @"C:\myfolder\"; // TODO: Make sure this exists and can be overwritten. 
      string localFilePath = Path.Combine(localFolderPath, "try.txt"); 
      string remoteFolderPath = "adl://mystore.azuredatalakestore.net/myfolder"; 
      string remoteFilePath = Path.Combine(remoteFolderPath, "try.txt"); 

      SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); 
      var tenant_id = "my-tenant-id"; 
      var nativeClientApp_clientId = "1950a258-227b-4e31-a9cf-717495945fc2"; 


      var activeDirectoryClientSettings = ActiveDirectoryClientSettings.UsePromptOnly(nativeClientApp_clientId, new Uri("urn:ietf:wg:oauth:2.0:oob")); 
      var creds = UserTokenProvider.LoginWithPromptAsync(tenant_id, activeDirectoryClientSettings).Result; 

      _adlsClient = new DataLakeStoreAccountManagementClient(creds) { SubscriptionId = _subId }; 
      _adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds); 
      Console.WriteLine(ListAdlStoreAccounts()); 
      Console.WriteLine(AppendToFile("adl://mystore.azuredatalakestore.net/myfolder/stage/testfile.txt", "abcdefghijklmnopqrstuvwxyz")); 
      CreateDirectory("adl://mystore.azuredatalakestore.net/myfolder/newdir"); 

      Console.ReadLine(); 

     } 
     // Append to file 
     public static string AppendToFile(string path, string content) 
     { 
      using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) 
      { 
       Console.WriteLine(_adlsAccountName, path, content); 
       Console.WriteLine(path); 
       Console.WriteLine(content); 
       _adlsFileSystemClient.FileSystem.AppendAsync(_adlsAccountName, path, stream); 
       return "Tried"; 
      } 
     } 
     public static string CreateDirectory(string path) 
     { 
      _adlsFileSystemClient.FileSystem.MkdirsAsync(_adlsAccountName, path); 
      return "Tried Creating directory."; 
     } 
} 
} 

的上面的代碼執行後,程序並沒有錯誤退出。連接正在進行。

而且它顯示datalake多家店,但他們卻無法在數據存儲湖做任何事情。

我是新來的天青,請幫助我。

+0

如果你只是嘗試創建文件夾,請試試使用'_adlsFileSystemClient.FileSystem.Mkdirs(_adlsAccountName, 「/ FOLDERNAME」)'; –

+0

@ TomSun-MSFT嘗試過。但**在我的datalake商店沒有更改**。 – Shubham

+0

請嘗試使用我提到的代碼而不用其他代碼。請使用 相對路徑不全路徑。我在我身邊測試了它的正常工作。 –

回答

0

我做了演示測試在我的身邊,它的工作原理上正確的一面。以下是我的詳細步驟:

準備:

註冊的AD應用和分配角色的applcation,更多詳情請 參考Azure official tutorials。之後,我們可以得到tenantIdAPPID祕密密鑰從Azure的門戶網站。

步驟:

1,創建一個C#控制檯項目

2.Referece的Microsoft.Azure.Management.DataLake.Store SDK,詳細信息請參考packages.config文件部分。

3.添加後續的代碼

var applicationId = "appid"; 
var secretKey = "secretkey"; 
var tenantId = "tenantid"; 
var adlsAccountName = "adlsAccount Name"; 
var creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, applicationId, secretKey).Result; 
var adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds,clientTimeoutInMinutes:60); 
adlsFileSystemClient.FileSystem.Mkdirs(adlsAccountName, "/tomtest/newfolder"); 

4.Run測試代碼

從天青門戶

enter image description here

5.檢查。

enter image description here

Packages.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="Microsoft.Azure.Management.DataLake.Store" version="2.2.0" targetFramework="net452" /> 
    <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net452" /> 
    <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net452" /> 
    <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.7" 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

謝謝。這工作。自昨天以來,我一直被困在這個問題上。 – Shubham

相關問題