JDBC development

更新时间:
复制 MD 格式

You can use Java Database Connectivity (JDBC) to access the Lindorm Distributed Processing System (LDPS) service and use Spark SQL for data queries, analysis, and generation.

Prerequisites

  • You have created a Lindorm instance and activated LindormTable. For more information, see Create an instance.

  • You have activated the LDPS service. For more information, see Activate LDPS.

  • You have installed a Java development environment. JDK 1.8 or later is required.

JDBC endpoint

To view the JDBC endpoint and JAR address for LDPS, click Database Connection on the instance details page, and then click the Compute Engine tab. For more information, see View endpoints. LDPS provides JDBC endpoints for both VPC and public network connections in the format jdbc:hive2://<host>:<port>.

Connect to JDBC with Java

  1. Add the JDBC dependency to your project. The following example uses Maven.

    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>2.3.8</version>
    </dependency>
  2. Use the following Java code to connect to the JDBC service:

    import java.sql.*;
    public class App {
        public static void main(String[] args) throws Exception {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            String endpoint = "jdbc:hive2://123.234.XX.XX:10009/;?token=bisdfjis-f7dc-fdsa-9qwe-dasdfhhv8****";
            String user = "";
            String password = "";
            Connection con = DriverManager.getConnection(endpoint, user, password);
            Statement stmt = con.createStatement();
            String sql = "SELECT * FROM test";
            ResultSet res = stmt.executeQuery(sql);
            while (res.next()) {
                System.out.println(res.getString(1));
            }
        }
    }
  3. Optional: To configure additional job parameters, add them to the JDBC endpoint string, as shown in the following example:

    String endpoint = "jdbc:hive2://123.234.XX.XX:10009/;?token=bisdfjis-f7dc-fdsa-9qwe-dasdfhhv8****;spark.dynamicAllocation.minExecutors=3;spark.sql.adaptive.enabled=false";

Connect to JDBC with Python

  1. Download the Spark release package .

  2. Decompress the Spark release package.

  3. Configure path variables.

    1. Set the SPARK_HOME environment variable.

      export SPARK_HOME=/path/to/dir/;
    2. Set the CLASSPATH environment variable.

      export CLASSPATH=$CLASSPATH:$SPARK_HOME/jars/*;
    3. Install JayDeBeApi.

      pip install JayDeBeApi
  4. Use the following Python code to connect to the JDBC service:

    import jaydebeapi
    driver = 'org.apache.hive.jdbc.HiveDriver'
    endpoint = 'jdbc:hive2://123.234.XX.XX:10009/;?token=bisdfjis-f7dc-fdsa-9qwe-dasdfhhv8****'
    jarPath = '/path/to/sparkhome/jars/hive-jdbc-****.jar'
    user = '****'
    password = '****'
    conn=jaydebeapi.connect(driver, endpoint, [user, password], [jarPath])
    cursor = conn.cursor()
    cursor.execute("select 1")
    results = cursor.fetchall()
    cursor.close()
    conn.close()
  5. Optional: To configure additional job parameters, add them to the JDBC endpoint string, as shown in the following example:

    endpoint = "jdbc:hive2://123.234.XX.XX:10009/;?token=bisdfjis-f7dc-fdsa-9qwe-dasdfhhv8****;spark.dynamicAllocation.minExecutors=3;spark.sql.adaptive.enabled=false"