简体   繁体   English

如何在笔记本外使用 DataBricks dbutils jar?

[英]How to use DataBricks dbutils jar outside notebook?

DataBricks dbutils library needs to be used in eclipse or any other IDE. DataBricks dbutils 库需要在 eclipse 或任何其他 IDE 中使用。 Methods like dbutils.secrets.get are not available from SecretUtil API outside notebook.像 dbutils.secrets.get 这样的方法在笔记本之外的 SecretUtil API 中不可用。 In this scenario we can use com.databricks jar在这种情况下,我们可以使用com.databricks jar

This is the Maven Repo for DataBricks dbutils library这是 DataBricks dbutils 库的 Maven Repo

<dependency>
<groupId>com.databricks</groupId>
<artifactId>dbutils-api_2.11</artifactId>
<version>0.0.3</version>

Once you add this import this in your code在您的代码中添加此导入后

import com.databricks.dbutils_v1.DBUtilsHolder.dbutils

This is the dbutils what we use in DataBricks notebook.这是我们在 DataBricks notebook 中使用的 dbutils。 You can use it like你可以像这样使用它

dbutils.secrets.get(scope, name)

1.If running your java app through IDE --- 1.如果通过IDE运行你的java应用程序---

a.一种。 download the required jar using databricks-connect.使用 databricks-connect 下载所需的 jar。

b.add the downloaded jars.添加下载的罐子。

c.now code will be --- c.现在代码将是---

com.databricks.service.SecretUtils$ secretClient = com.databricks.service.DBUtils.secrets();
System.out.println(secretClient.get("<secret-scope>", "<secret-key>"));

now when you run this it will in your console it will give you a command that you need to run on any of the cluster in your databricks account to get the token.现在,当您运行它时,它将在您的控制台中为您提供一个命令,您需要在您的 databricks 帐户中的任何集群上运行该命令以获取令牌。 once you get the token you need to write the code as:获得令牌后,您需要将代码编写为:

com.databricks.service.SecretUtils$ secretClient = com.databricks.service.DBUtils.secrets();
secretClient.setToken("<token>");
System.out.println(secretClient.get("<secret-scope>", "<secret-key>"));

2.Now if you are going to deploy the jar on databricks then use the below code: a.add the latest maven dependency for dbutils-api can be found here: 2.现在,如果您要在数据块上部署 jar,请使用以下代码: a. 可以在此处找到 dbutils-api 的最新 maven 依赖项:

https://docs.databricks.com/dev-tools/databricks-utils.html#databricks-utilities-api-library https://docs.databricks.com/dev-tools/databricks-utils.html#databricks-utilities-api-library

then simply run the following code:然后只需运行以下代码:

com.databricks.dbutils_v1.SecretUtils hostedSecretClient = com.databricks.dbutils_v1.DBUtilsHolder.dbutils().secrets();
System.out.println(hostedSecretClient.get("<secret-scope>", "<secret-key>"));

Hope this will work!!!希望这会奏效!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM