When you submit SQL statements through MaxCompute SDK for Java, prepending a SET statement directly to your SQL does not work. Instead, pass SQL flags as a hints map to SQLTask.run().
Background
In the DataWorks console and the MaxCompute client, you set SQL flags by prepending SET statements to your SQL. For example, to enable the MaxCompute V2.0 data type edition at the session level:
SET odps.sql.type.system.odps2=true;
SELECT ...MaxCompute SDK for Java does not support this syntax. Use the hints parameter described below.
Set SQL flags via the hints parameter
Pass a HashMap<String, String> as the fourth argument to SQLTask.run(). Each entry in the map sets one SQL flag for the query session, equivalent to one SET command.
String sql = "SELECT ...";
// Add one entry per flag. You can include as many flags as needed.
HashMap<String, String> hints = new LinkedHashMap<String, String>();
hints.put("odps.sql.type.system.odps2", "true");
Instance i = SQLTask.run(odps, odps.getDefaultProject(), sql, hints, null);
i.waitForSuccess();| Parameter | Description |
|---|---|
hints | A map of SQL flag names to their values. |
odps | The Odps client instance used to connect to MaxCompute. |
odps.getDefaultProject() | The MaxCompute project to run the query against. |
该文章对您有帮助吗?