This topic describes how to use SQLTask and Tunnel to export a large amount of data.

Background information

  • SQLTask

    SQLTask is an SQL class of MaxCompute SDK for Java. SQLTask allows you to execute SQL statements and obtain the returned results. For more information about SQLTask, see SDK for Java.

    The SQLTask.getResult(i) method returns a list. You can repeatedly use this method in an SQL statement to obtain the complete SQL computation result. However, if you use this method, the SELECT statement can return a maximum of 10,000 data entries to the MaxCompute client upon each query. For more information, see the valid values of READ_TABLE_MAX_ROW in Project operations. If you execute the SELECT statement on the MaxCompute client or by using SQLTask, you limit the number of data entries to return in each query result to 10,000. If you use the CREATE TABLE XX AS SELECT, INSERT INTO TABLE, or INSERT OVERWRITE TABLE statement to insert the query result into a table, the number of data entries to return is not limited.

  • Tunnel

    If the data that you want to export is all data in a table or a specific partition, you can use the Tunnel command-line tool to export all data at a time. If the number of data entries exceeds 10,000, we recommend that you use Tunnel commands to export the data entries. MaxCompute provides the Tunnel command-line tool and Tunnel SDK. For more information, see Tunnel commands and Tunnel SDK.


SQLTask cannot process more than 10,000 data entries in each query. You can use SQLTask together with Tunnel to export more than 10,000 data entries. The following sample code is for your reference:
private static final String accessId = "userAccessId";
private static final String accessKey = "userAccessKey";
private static final String endPoint = "";
private static final String project = "userProject";
private static final String sql = "userSQL";
private static final String table = "Tmp_" + UUID.randomUUID().toString().replace("-", "_");// Use a random string as the name of the temporary table for storing the exported data. 
private static final Odps odps = getOdps();
public static void main(String[] args) {
     * Download the query result that is returned when you use SQLTask. 
     * */
private static void tunnel() {
    TableTunnel tunnel = new TableTunnel(odps);
    try {
        DownloadSession downloadSession = tunnel.createDownloadSession(project, table);
        System.out.println("Session Status is : "+ downloadSession.getStatus().toString());
        long count = downloadSession.getRecordCount();
        System.out.println("RecordCount is: " + count);
        RecordReader recordReader = downloadSession.openRecordReader(0, count);
        Record record;
        while ((record = != null) {
            consumeRecord(record, downloadSession.getSchema());
    } catch (TunnelException e) {
    } catch (IOException e1) {
     * Save the data. 
     * If only a small amount of data is returned, you can display the query result and copy the data. You can also use the package to write the query result to a local file or a file on a remote server. 
     * */
private static void consumeRecord(Record record, TableSchema schema) {
     * Execute the SQL statement and save the query result as a temporary table so that you can use Tunnel to download the result. 
     * Set the lifecycle of the saved data to one day. This prevents the data that fails to be deleted from occupying too much storage space. 
     * */
private static void runSql() {
    Instance i;
    StringBuilder sb = new StringBuilder("Create Table ").append(table)
        .append(" lifecycle 1 as ").append(sql);
    try {
        i =, sb.toString());
    } catch (OdpsException e) {
     * Initialize the connection of the MaxCompute project. 
     * */
private static Odps getOdps() {
    Account account = new AliyunAccount(accessId, accessKey);
    Odps odps = new Odps(account);
    return odps;