IBM Cloud Docs
连接 Presto 服务器

连接 Presto 服务器

PrestoCLI 提供了一个基于终端的交互式 shell 来运行查询。

在IBM® watsonx.data中,您可以根据所使用的平台和实用程序,以多种方式连接到Presto服务器。 详情请参见以下章节:

使用 CLI 创建模式时,必须指定位置。 例如 location = s3a://<storage-name>/

先决条件

获取Presto引擎主机名和端口详细信息

  1. 登录 watsonx.data 网络控制台。

  2. 转至 基础架构管理器,然后单击 列表视图

  3. 引擎 选项卡中,单击需要其主机名和端口详细信息的引擎名称。

  4. 主机 标签下,单击 复制到剪贴板 图标以复制主机详细信息。

  5. 将主机详细信息复制到记事本。

获取 IBM API 密钥或 IBM IAM 令牌

根据您的需求,使用 IBM API 密钥或 IBM IAM 令牌。

建议将 IAM 令牌用于压力工作负载。

获取 IBM API 密钥

  1. 登录 IBM Cloud 控制台

  2. 在导航栏中,单击 管理,然后选择 访问权 (IAM)

  3. 单击左侧导航栏中的 API 密钥

  4. 单击创建 +

  5. 在“创建 IBM Cloud API 密钥”窗口中,输入 API 密钥的名称,并输入密钥的相应描述。 例如, ibmlhtoken testing

  6. 单击创建。 将显示包含消息 API 密钥已成功创建 的窗口。

  7. 单击“**下载 **”将 API 密钥保存到本地计算机。

  8. 打开下载的文件并将 API 密钥复制到记事本文件。

获取 IBM Access Management (IAM) 令牌

  1. 调用 IAM 中的 REST 端点以获取 IAM 令牌。

  2. <your-api-key> 替换为 IBM API 密钥。

    curl -X POST 'https://iam.cloud.ibm.com/identity/token' -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=MY_APIKEY'
    

通过 Presto CLI(远程)连接到 Presto 引擎

  1. 从以下网址下载 Presto 可执行文件 jar https://prestodb.io/getting-started/

  2. 将下载的文件重命名为 presto。 使其可与 chmod +x 一起执行并运行。

  3. 要检查PrestoCLI 是否已安装,请运行 ./presto --version. 显示Prestocli 版本。 例如, Presto CLI 0.281-cfbc6eb

  4. 在安装PrestoCLI 的系统中运行以下命令。 您可以使用以下方法之一验证 Presto:

    • 使用用户名和密码:请运行以下命令:

      • 如果使用的是 API 密钥,请运行以下命令:

        ./presto --server <https://Prestoengine host details> --catalog iceberg_data --schema default --user ibmlhapikey_<your-username> --password
        
      • 如果使用 IBM IAM 令牌,请运行以下命令:

        ./presto --server <https://Prestoengine host details> --catalog iceberg_data --schema default --user ibmlhtoken_<your-username> --password
        

      如果您有多个与不同用户的连接,并且想要区分这些连接,那么 <your-username> 是可选的。

      在提示符处输入 IBM API 密钥或 IBM IAM 令牌。

    • JWT 令牌:从 2.1.2 开始,默认提供 JWT 身份验证方法。 运行以下命令,使用 JWT 令牌连接 presto:

    要在 watsonx.data 以前的版本( 2.1.2 发布之前)使用这种验证方法,请联系 IBM 支持部门并启用该功能。

    ```bash {: codeblock}
    ./presto --server <https://Prestoengine host details> --catalog iceberg_data --schema default --access-token <ACCESS_TOKEN>
    ```
    

    要生成 <ACCESS_TOKEN>,请使用以下方法之一:

    • 获取 IBM 访问管理(IAM)令牌,请参阅 (IAM)令牌

    • 使用以下命令获取 <ACCESS_TOKEN>

      curl --location 'https://us-south.lakehouse.dev.cloud.ibm.com/lakehouse/api/v2/auth/authenticate/' \
      --header 'Content-Type: application/json' \
      --data-raw '{
       "username": "ibmlhtoken_<user-name>",
       "password": "<IAM_TOKEN>",
       "instance_id": "<instance_id>",
       "instance_name": ""
      }'
      

      <IAM_TOKEN> : Specify the token generated from (IAM) token.

      <user-name> : Specify the email id.

      <instance_id> : Specify the instance CRN.

  5. 在Presto提示符下键入 show catalogs;。 此时将显示目录列表。 现在,您已通过 Presto CLI 连接到 watsonx.data 中的 Presto 引擎。

    presto:default> show catalogs;
    Catalog
    --------------
    iceberg_data
    jmx
    system
    tpcds
    tpch
    (5 rows)
    

使用JDBC连接Presto引擎

  1. 在客户机上下载并安装 最新 JDBC 驱动程序

  2. 将下载的 jar 文件添加到 Java 应用程序的类路径。

  3. 获取 API 密钥。

    使用 ibmlhapikey 作为用户名,API 密钥作为密码。 有关详细信息,请参阅 获取 IBM API 密钥

  4. 获取主机名和端口。 更多信息,请参阅 获取Presto引擎主机名和端口详细信息

  5. 使用 JDBC 接口创建 Java 应用程序。 以下是 Java 片段的示例:

    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class PrestoJdbcSample
    {
        public static void main(String[] args) throws Exception
        {
            /*
             * example of fetching the location and credentials needed to connect, from
             * environment variables
             */
            String username = System.getenv("ENG_USERNAME");
            String password = System.getenv("ENG_PASSWORD");
            String hostname = System.getenv("ENG_HOST");
            String portnumber = System.getenv("ENG_PORT");
            String presto_url = "jdbc:presto://" + hostname + ":" + portnumber;
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try
            {
                /* load the Presto JDBC Driver class  */
                String driverClass = "com.facebook.presto.jdbc.PrestoDriver";
                Class.forName(driverClass);
                /* Set the connection properties */
                Properties properties = new Properties();
                properties.setProperty("user", username);
                properties.setProperty("password", password);
                properties.setProperty("SSL", "true");
                /* Connect */
                connection = DriverManager.getConnection(presto_url, properties);
                /* Issue a Query */
                String query = "SELECT * FROM tpch.tiny.customer LIMIT 10";
                statement = connection.createStatement();
                resultSet = statement.executeQuery(query);
                /* iterate through the results */
                while (resultSet.next())
                {
                    String phone = resultSet.getString("phone");
                    String name = resultSet.getString("name");
                    System.out.println("phone = " + phone + ", name = " + name);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                /* clean up at the end always **/
                if (resultSet != null)
                {
                    resultSet.close();
                }
                if (statement != null)
                {
                    statement.close();
                }
                if (connection != null)
                {
                    connection.close();
                }
            }
        }
    }
    

    将命令中的参数替换为以下内容: <PRESTO_URL> 将jdbc 标识为 服务器。URL Presto <EMAIL_ID> 与您的电子邮件标识 <API_KEY> 使用 API 密钥

    如果使用 IBM IAM 令牌,请将 ibmapikey 替换为 ibmlhtoken 并传递令牌。

  6. 编译并运行该命令。

使用Python脚本连接Presto引擎

  1. 在客户机工作站上安装 Python 3.x (建议使用3.10 或更高版本) 和 pip3

  2. 使用 DBAPI 接口查询Presto。 以下是样本 python 脚本。

    import os
    import prestodb
    username=os.environ["ENG_USERNAME"]
    password=os.environ["ENG_PASSWORD"]
    hostname=os.environ["ENG_HOST"]
    portnumber=os.environ["ENG_PORT"]
       with prestodb.dbapi.connect(
       host=hostname,
       port=portnumber,
       user=username,
       catalog='tpch',
       schema='tiny',
       http_scheme='https',
       auth=prestodb.auth.BasicAuthentication(username,password)
       ) as conn:
    cur = conn.cursor()
    cur.execute('select * from tpch.tiny.customer limit 10')
    rows = cur.fetchall()
    print(rows)
    
    

    该命令查询显示Presto集群节点的 system.runtime.nodes 系统表。 prestodb.dbapi 中的 DBAPI 实现提供了用于检索几行的方法。 例如,Cursorfetchone()Cursor.fetchmany()。 缺省情况下,Cursor.fetchmany() 会访存一行。 相应地设置 prestodb.dbapi.Cursor.arraysize

  3. 编译并运行该命令。