Avro

Building the JDBC URL

After installing the license, open the connection management page by running java -jar kingswaysoft.jdbc.jar. Enter the required details to generate the JDBC connection URL. Click Test Connection to test the generated URL, or Copy to Clipboard to copy the connection string for use in your application.

Note: If the license is not installed, you can still use the connection manager to generate a JDBC URL; however, the 'Test Connection' feature will be disabled.

General Page

The General page allows you to specify connection properties for the Avro service.

connectionmanage

General Settings
Data Source Mode

The Data Source Mode specifies the method used to locate Avro files.

Available options are:
  • Folder (Dataset)
  • File
In Folder (Dataset) mode, it is expected that the specified root directory will have subdirectories that contain the Avro files. In File mode, it is expected that the specified root directory will contain the Avro files.
Source Path

The Source Path is the directory containing .avro or .avsc files. Each file will be treated as a unique table.

Using the JDBC Driver

This section provides examples that use JDBC classes such as Connection, Statement, and ResultSet to manage interactions with Avro data. It covers regular statements and prepared statements for complex or frequently executed queries.

When writing to a table that is based off an Avro schema file (.avsc) an Avro data file (.avro) will be generated if the query execution was successful. Subsequent read or writes to the table will use this Avro data file and the Avro schema file is no longer necessary.

The schema in both the Avro data file and the Avro schema file must be identical, otherwise you will receive an error. This is due to the Avro data file containing both the data and the schema.

Executing Statements

After you have connected from your code, you can execute SQL statements using the Statement class. For connection details, see Connecting with DriverManager or Connecting with DataSource. For parameterized statements, see Executing Prepared Statements.

SELECT

Use the Statement class's generic execute method or the executeQuery method to execute SQL statements that return data. To retrieve the results of a query, call the getResultSet method of the Statement.

String sql = "SELECT * FROM select_test WHERE id < 4";
try {
    ResultSet resultSet = statement.executeQuery(sql);
    LOGGER.info(resultSet.toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}

INSERT

Use either the generic execute method or the executeUpdate method of the Statement class to execute an INSERT operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the inserted data's ID, exceptions raised during execution, and details of the affected data.

String sql = "INSERT INTO write_test (id, val) VALUES (1000, 'original')";

try {
    statement.executeUpdate(sql);
    LOGGER.info(statement.getResultSet().toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{val=original, id=1000},false

UPDATE

Use either the generic execute method or the executeUpdate method of the Statement class to execute an UPDATE operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the updated data's ID, exceptions raised during execution, and details of the affected data.

String sql = "UPDATE write_test SET val = 'modified' WHERE id = 1000";
try {
    statement.executeUpdate(sql);
    LOGGER.info(statement.getResultSet().toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{val=modified},false

DELETE

Use either the generic execute method or the executeUpdate method of the Statement class to execute a DELETE operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the deleted data's ID, exceptions raised during execution, and details of the affected data.

String sql = "DELETE FROM write_test WHERE id = 1000";
try {
    statement.executeUpdate(sql);
    LOGGER.info(statement.getResultSet().toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{id=1000},false

Executing Prepared Statements

Using a PreparedStatement can improve performance when you need to execute a SQL statement multiple times with different parameters. Unlike a Statement object, a PreparedStatement object is provided with a SQL statement when it is created, which can then be executed with different values each time. This special type of statement is derived from the more general Statement class.

Below are the steps outlining how to execute a prepared statement:

Creating and Executing a Prepared Statement
  1. Create a PreparedStatement
  2. Set Parameters
    • Declare parameters by calling the corresponding setter method of the PreparedStatement.
    • NOTE: The parameter indices start at 1.
  3. Execute the PreparedStatement
    • Use the generic execute or executeUpdate method of the PreparedStatement.
  4. Retrieve Results
    • Call the getResultSet method of the PreparedStatement to obtain the query results, which are returned as a ResultSet.
  5. Iterate Over the Result Set
    • Use the next method of the ResultSet to iterate through the results. To obtain column information, use the ResultSetMetaData class. Instantiate a ResultSetMetaData object by calling the getMetaData method of the ResultSet.

SELECT

Use the PreparedStatement class's generic execute method or the executeQuery method to execute SQL statements that return data.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the retrieved data.

String sql = "SELECT * FROM select_test WHERE id < ?";
try {
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setInt(1, 4);

    boolean ret = ps.execute(sql);
    if (ret) {
        ResultSet rs = ps.getResultSet();
        LOGGER.info(rs.toString());
    }
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}

INSERT

Use either the generic execute method or the executeUpdate method of the PreparedStatement class to execute an INSERT operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the ID of inserted data, exceptions raised during execution, and the data affected by the insertion.

String sql = "INSERT INTO write_test (id, val) VALUES (?, ?)";
try {
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setInt(1, 1000);
    ps.setString(2, "original");
    ps.executeUpdate();
    LOGGER.info(ps.getResultSet().toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{val=original, id=1000},false

UPDATE

Use either the generic execute method or the executeUpdate method of the PreparedStatement class to execute an UPDATE operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the ID of updated data, exceptions raised during execution, and the data affected by the update.

String sql = "UPDATE write_test SET val = ? WHERE id = ?";
try {
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setString(1, "modified");
    ps.setInt(2, 1000);
    ps.executeUpdate();

    LOGGER.info(ps.getResultSet().toString());
} catch (SQLException e) {
    LOGGER.severe(e.toString());
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{val=modified},false

DELETE

Use either the generic execute method or the executeUpdate method of the PreparedStatement class to execute a DELETE operation.

The results of SQL queries are saved in a ResultSet. You can retrieve the ResultSet after execution to view the deleted data's ID, exceptions raised during execution, and details of the affected data.

String deleteSql = "DELETE FROM write_test WHERE id = ?";
try {
    PreparedStatement ps = connection.prepareStatement(deleteSql);
    ps.setInt(1, 1000);
    ps.executeUpdate();
    LOGGER.info(ps.getResultSet().toString());
} catch (SQLException e) {
    e.printStackTrace();
}
id,errorcode,errormessage,processdata,haserrors
null,null,null,{id=1000},false

Using Query Level Options for UPDATE and DELETE

When an UPDATE or DELETE statement matches more than one row in Avro File mode, you can use USING OPTIONS to control how the driver handles the multiple matches.

Note: This option is supported only in Avro File mode. It is not supported in Folder (Dataset) mode.

Supported multiple match actions are:

Option Behavior
All Matches Updates or deletes all matched rows. This is the default behavior when no option is specified.
First Match Updates or deletes only the first matched row.
Last Match Updates or deletes only the last matched row.
Raise Error Fails the statement when more than one row is matched.
Ignore Skips the operation when more than one row is matched. If exactly one row is matched, that row is processed.

You can specify the option as a named value:

String sql = "UPDATE write_test SET val = 'modified' WHERE groupId = 'A' "
           + "USING OPTIONS (MultipleMatchAction = 'First Match')";
statement.executeUpdate(sql);

Or as a single option value:

String sql = "DELETE FROM write_test WHERE groupId = 'A' USING OPTIONS ('Raise Error')";
statement.executeUpdate(sql);

Metadata Discovery

This section provides examples on how to retrieve table and column metadata using the getTables, getColumns, and getPrimaryKeys methods from the DatabaseMetaData interface. These are essential for discovering database structures.

Tables

The getTables method from the DatabaseMetaData interface can be used to retrieve a list of tables.

This method only retrieves tables that are not write-only.

To get a list of tables that includes write-only tables, query the table [system.tables](/products/jdbc-driver-pack/help-manual/advancedfeatures#systemtables).

try {
    Connection connection = buildRestConnectionFromDriverManager();
    ResultSet rs = connection.getMetaData().getTables(null, null, null, null);
    LOGGER.info("\r\n" + rs.toString());
} catch (SQLException e) {
    LOGGER.severe(e.getMessage());
}
TABLE_CAT,TABLE_SCHEM,TABLE_NAME,TABLE_TYPE,REMARKS
null,null,select_test,Table,null
null,null,write_test,Table,null

The getTables method returns the following metadata columns:

Column Name Data Type Description
TABLE_CAT String The catalog that contains the table.
TABLE_SCHEM String The schema of the table.
TABLE_NAME String The name of the table.
TABLE_TYPE String The type of the table (e.g., TABLE or VIEW).
REMARKS String An optional description of the table.

Columns

Use the getColumns method of the DatabaseMetaData interface to retrieve detailed information about database columns. To narrow the results to a specific table, specify the table name using the parameter table_name.

This method returns columns only for tables that are not write-only.

To get columns for write-only tables, query the table [system.columns](/products/jdbc-driver-pack/help-manual/advancedfeatures#systemcolumns).

try {
    Connection connection = buildRestConnectionFromDriverManager();
    ResultSet rs = connection.getMetaData().getColumns(null, null, "select_test", null);
    LOGGER.info(rs.toString());
} catch (SQLException e) {
    e.printStackTrace();
}
TABLE_CAT,TABLE_SCHEM,TABLE_NAME,COLUMN_NAME,DATA_TYPE,TYPE_NAME,COLUMN_SIZE,BUFFER_LENGTH,DECIMAL_DIGITS,NUM_PREC_RADIX,NULLABLE,REMARKS,COLUMN_DEF,SQL_DATA_TYPE,SQL_DATETIME_SUB,CHAR_OCTET_LENGTH,ORDINAL_POSITION,IS_NULLABLE,IS_AUTOINCREMENT,IS_GENERATEDCOLUMN,DTS_TYPE
null,null,select_test,id,4,INTEGER,0,null,0,0,null,null,null,4,null,null,null,null,null,null,DT_I4
null,null,select_test,name,12,VARCHAR,0,null,0,0,null,null,null,12,null,null,null,null,null,null,DT_WSTR
null,null,select_test,age,4,INTEGER,0,null,0,0,null,null,null,4,null,null,null,null,null,null,DT_I4
null,null,select_test,salary,6,FLOAT,0,null,0,0,null,null,null,6,null,null,null,null,null,null,DT_R8
null,null,select_test,active,16,BOOLEAN,0,null,0,0,null,null,null,16,null,null,null,null,null,null,DT_BOOL

The getColumns method returns the following columns:

Column Name Data Type Description
TABLE_CAT String The database name.
TABLE_SCHEM String The table schema.
TABLE_NAME String The table name.
COLUMN_NAME String The column name.
DATA_TYPE Integer The data type represented by a constant value from java.sql.Types.
TYPE_NAME String The data type name used by the driver.
COLUMN_SIZE Integer The length in characters of the column or the numeric precision.
BUFFER_LENGTH Integer The buffer length.
DECIMAL_DIGITS Integer The column scale or number of digits to the right of the decimal point.
NUM_PREC_RADIX Integer The radix, or base.
NULLABLE Integer Whether the column can contain null as defined by the following JDBC DatabaseMetaData constants: columnNoNulls (0) or columnNullable (1).
REMARKS String The comment or note associated with the object.
COLUMN_DEF String The default value for the column.
SQL_DATA_TYPE Integer Reserved by the specification.
SQL_DATETIME_SUB Integer Reserved by the specification.
CHAR_OCTET_LENGTH Integer The maximum length of binary and character-based columns.
ORDINAL_POSITION Integer The position of the column in the table, starting at 1.
IS_NULLABLE String Whether a null value is allowed: YES or NO.
IS_AUTOINCREMENT String Whether the column value is assigned by Avro in fixed increments.
IS_GENERATEDCOLUMN String Whether the column is generated: YES or NO.
DTS_TYPE String Object DTS attribute type.

Primary Keys

This is not supported in Avro as the Avro core specification does not support specifying fields as primary keys.

Connection Settings

Connection Setting Type Default Value Description
DataSourceMode String "Folder" The method used to detect Avro files and to populate entities within the driver.
ContinueOnErrors Boolean false Determines if the program continues executing SQL statements after encountering an error.
LogFileSize String "10485760" A string specifying the maximum size in bytes for a log file.
LogLevel String "INFO" The logging level for the JDBC driver.
LogPath String "./jdbcLogs" The directory where log files are stored.
OemKey String "" The OEM license key.
ResultPath String "" The path where the execution result files are saved.
SourcePath String "" The system path scanned for .avro or .avsc files. Each file will be treated as a unique table.
SaveResult Boolean false The SaveResult parameter indicates whether to save the execution results to a file.
ServiceName String "Avro" The ServiceName refers to the name of the service API selected by the user.