[英]How to create resources in azure data lake gen1 with java sdk?
I am new to azure data lake gen1.我是 azure 数据湖 gen1 的新手。 I am trying to create files and directories with the java sdk. SO far, I have written this piece of code to perform the creation operation.我正在尝试使用 java sdk 创建文件和目录。到目前为止,我已经编写了这段代码来执行创建操作。
final static String clientId = "b";
final static String tenantId = "";
final static String clientSecret = "";
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
String authority = "https://login.microsoftonline.com/b94/oauth2/v2.0/token";
AuthenticationContext context = new AuthenticationContext(authority, true, Executors.newFixedThreadPool(1));
Future<AuthenticationResult> result = context.acquireToken("https://management.core.windows.net/", new ClientCredential(clientId, clientSecret), null);
ADLStoreClient client = ADLStoreClient.createClient("kaushal.azuredatalakestore.net", result.get().getAccessToken());
client.createDirectory("/test");
}
I get following error on execution of above code: ---执行上述代码时出现以下错误:---
Exception in thread "main"线程“main”中的异常
com.microsoft.azure.datalake.store.ADLException: Operation MKDIRS failed with HTTP403: AccessControlException Last encountered exception thrown after 1 tries [HTTP403(AccessControlException)] com.microsoft.azure.datalake.store.ADLException:操作 MKDIRS 因 HTTP403 失败:AccessControlException 最后遇到的异常在 1 次尝试后抛出 [HTTP403(AccessControlException)]
What surprises me is that, I am successfully able to create resources via console but not with the code, I am not trying to find the actual root cause of this error.令我惊讶的是,我能够通过控制台成功创建资源,但不能使用代码,我并没有试图找到此错误的实际根本原因。
Your code seems fine, although check out a working example as below.您的代码看起来不错,但请查看下面的工作示例。
From the portal, you need to add the execute permission to the root node, if not done already.从门户中,您需要将执行权限添加到根节点(如果尚未完成)。 And don't forget to grant access ADL permissions to your client.并且不要忘记向您的客户授予访问 ADL 权限。
Navigate to your ADL account and then select Access Control (IAM):导航到您的 ADL 帐户,然后导航到 select 访问控制 (IAM):
Register your application (hope thats done already)注册您的应用程序(希望已经完成)
Navigate to your ADL account and then select Access Control (IAM):导航到您的 ADL 帐户,然后导航到 select 访问控制 (IAM):
Click on +Add to add to add role-based permissions.点击+Add添加添加基于角色的权限。
Under Role select the "Owner".在角色 select“所有者”下。 Under Select, select your application.下Select、select您申请。 This will grant the "Owner" role for this ADL account to your application.这会将此 ADL 帐户的“所有者”角色授予您的应用程序。
Note: If you are not able to assign the "Owner" role, you can set fine-grained RWX ACL permissions for your application, allowing it access to the files and folders of your ADLS account.注意:如果您无法分配“所有者”角色,您可以为您的应用程序设置细粒度的 RWX ACL 权限,允许它访问您的 ADLS 帐户的文件和文件夹。
Refer: Access control in Azure Data Lake Storage Gen1参考: Azure Data Lake Storage Gen1中的访问控制
Example: to create a directory named " kar " in ktestadls.azuredatalakestore.net
示例:在ktestadls.azuredatalakestore.net
中创建名为“ kar ”的目录
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.azure.datalake.store.ADLStoreClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CreateDirectory {
static ADLStoreClient client;
public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
setup();
}
public static void setup() throws IOException, ExecutionException, InterruptedException {
String APP_ID = "<your app id>";
String APP_SECRET = "<your app secret>";
String dirName = "/kar";
String StoreAcct = "ktestadls";
String authority = "https://login.microsoftonline.com/<your tenant id>";
String resourcUrl = "https://management.core.windows.net/";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
// Acquire Token
Future<AuthenticationResult> result = context.acquireToken(
resourcUrl,
new ClientCredential(APP_ID, APP_SECRET),
null
);
String token = result.get().getAccessToken();
System.out.println(token);
String account = StoreAcct + ".azuredatalakestore.net";
client = ADLStoreClient.createClient(account, token);
client.createDirectory(dirName);
System.out.println("finish.....");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.