Object Table queries and optimization
Query object content for computations
By default, a query on an Object Table processes only the object's metadata and does not access its actual content. However, business computations often require reading the object's content. MaxCompute provides a built-in download function that you can integrate into your computations.
GET_DATA_FROM_OSS function syntax
The GET_DATA_FROM_OSS function reads all or part of an object's content and returns it in binary format.
BINARY GET_DATA_FROM_OSS (
STRING <full_object_table_name>,
STRING <key>
[, BIGINT <offset>]
[, BIGINT <length>]
[, STRING <object_not_found_policy>]
)Parameters
Parameter | Required | Data type | Description | Default value |
full_object_table_name | Yes | STRING | The full path to the OBJECT TABLE in the three-tier model, including the Project and Schema names, such as If you use RoleARN for authentication when you create the table, this parameter helps automatically generate a Security Token Service (STS) token to access OSS. | None |
key | Yes | STRING | The name of the accessed object in the Object Table. For more information, see the description of the `key` parameter in the response described in View OBJECT TABLE properties. | None |
offset | No | BIGINT | The starting position from which to read the object content. The value must be greater than or equal to 0. | 0. This means the read operation starts from the beginning of the object. |
length | No | BIGINT | The number of bytes to read. | -1. This means the length is not limited. |
object_not_found_policy | No | STRING | Specifies how MaxCompute returns the result of the function invocation if an object key exists in the cached data but the object no longer exists in OSS. Valid values:
| The default value is OUTPUT_NULL. |
Example 1
To output the result of the GET_DATA_FROM_OSS function as the STRING type, nest it within the `STRING` function.
SET odps.namespace.schema=true;
SELECT STRING(
GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 0, -1, 'OUTPUT_NULL')
)
FROM ot_demo_day;Example 2
Read all the content of the Object Table and return it as a binary value. The full path of the Object Table is <project_name>.default.ot_demo_day. The following code shows different parameter combinations.
SET odps.namespace.schema=true;
-- Complete syntax.
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 0, -1, 'OUTPUT_NULL') FROM ot_demo_day;
-- The following calls are equivalent because they use default parameter values.
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key) FROM ot_demo_day;
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 0) FROM ot_demo_day;
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 0, -1) FROM ot_demo_day;
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 'OUTPUT_NULL') FROM ot_demo_day;
SELECT GET_DATA_FROM_OSS('<project_name>.default.ot_demo_day', key, 0, 'OUTPUT_NULL') FROM ot_demo_day;Optimize Object Table query performance
By default, when you access a table in a MaxCompute SQL query, MaxCompute creates splits based on the number of rows, byte size of records, and total available computing resources, which are limited by your quota configuration. This approach enables parallel computation for different splits, which helps control the long-tail problem in parallel tasks and improves overall query performance.
However, this splitting method is not ideal for SQL queries that download object content from Object Tables for in-memory computation. Performance is often suboptimal, with I/O operations becoming a bottleneck, which can cause a long-tail problem.
You can use either of the following two methods to control concurrency:
The following parameters take effect only when your SQL query uses one of these functions: GET_DATA_FROM_OSS, GET_BLOB_FROM_OSS, or GET_SIGNED_URL_FROM_OSS.
Concurrency by split size
Key | Size |
a0000.jpg | 10MB |
a0001.jpg | 10MB |
a0002.jpg | 10MB |
… | … |
a1022.jpg | 10MB |
a1023.jpg | 10MB |
b.avi | 10GB |
Assume you have only two worker resources available. If the data is processed like an internal table and split by row count or record bytes, you might end up with two splits: split1 ([a0000.jpg ~ a0511.jpg]) and split2 ([a0512.jpg ~ a1023.jpg, b.avi]). The download size for split1 is 10 MB * 512 = 5 GB, while the download size for split2 would be 5 GB + 10 GB = 15 GB. The computational load for split2 is therefore significantly higher than for split1, leading to a severe long-tail problem.
A better approach is to split the data based on the actual size of the objects when a SQL query needs to download them. This helps minimize the long-tail problem. If the logic for consuming OSS object data is time-consuming, flexible splitting capabilities become even more important. In this case, you could create two splits: split1 [a0000.jpg ~ a1023.jpg] and split2 [b.avi]. The download size for each split is now 10 GB.
MaxCompute Object Tables provide this capability and, by default, split data based on the actual object size. The default value is 1 GB, which you can adjust. The granularity can be set to KB, MB, or GB.
SET odps.namespace.schema=true;
-- The default value is 1 GB. You can adjust it as needed.
SET odps.sql.object.table.split.unit.gb = 1;
SELECT get_data_from_oss('project.default.ot_demo_day', key) FROM ot_demo_day WHERE ...
-- Alternatively, you can control the split size in MB. This parameter has a higher priority than the GB parameter.
SET odps.sql.object.table.split.unit.mb = 1;
SELECT get_data_from_oss('project.default.ot_demo_day', key) FROM ot_demo_day WHERE ...
-- Alternatively, you can control the split size in KB. This parameter has the highest priority.
SET odps.sql.object.table.split.unit.kb = 1;
SELECT get_data_from_oss('project.default.ot_demo_day', key) FROM ot_demo_day WHERE ...If you need to disable this query optimization feature (which launches two jobs, the first of which performs preprocessing and can be viewed in Logview), MaxCompute Object Tables provide a way to do so:
SET odps.namespace.schema=true;
SET odps.sql.object.table.split.by.object.size.enabled = false;
SELECT get_data_from_oss('project.default.ot_demo_day', key) FROM ot_demo_day WHERE ...Concurrency by DOP
You can adjust the concurrency for reading data by setting the odps.sql.split.dop parameter. This parameter has a higher priority than odps.sql.mapper.split.size.
If the DOP value is greater than the number of files in the OSS directory, the actual concurrency may differ significantly from the configured DOP value.
If the DOP value is too small, the DOP setting has no effect. You must also use the
odps.input.file.num.limitparameter to change the maximum number of files that a single instance can process.
Syntax
-- Schema syntax disabled:
SET odps.sql.split.dop={"project.table": ***};
-- Schema syntax enabled:
SET odps.sql.split.dop={"project.schema.table": ***};
-- Example. Replace 123 with a value that matches your workload.
SET odps.sql.split.dop={
"project.schema.table": 123
};
SET odps.sql.common.table.planner.ext.hive.bridge=FALSE;
SELECT * FROM <your_table>;Example
Addressing DOP distortion caused by many small files
MaxCompute limits a single instance to processing a maximum of 240 files. Therefore, if an OSS external table directory contains 3,449 files, the minimum concurrency is approximately 3449 / 240 ≈ 15. If you set the DOP value to less than 15, the DOP setting has no effect.
Set
odps.input.file.num.limitto change the maximum number of files a single instance can process.SET odps.sql.split.dop = {"lakehouse47_3.tpch_1t_parquet_snappy.lineitem": 2}; SET odps.input.file.num.limit = 5000;Now, a single instance can process up to 5,000 OSS files. Since the number of files in the lineitem table is well below this limit, the actual concurrency will match the configured DOP value.
Visualizing unstructured data
OSS stores a large amount of unstructured data. To query and analyze this data visually, you can combine Object Tables with the MaxCompute AI Data Explorer client to perform operations such as visual queries, analysis, data preview, and downloads.
To install and configure the MaxCompute AI Data Explorer client, see Quick start. After you configure the client, you can perform the following operations.
Visual query and analysis
Use the built-in SQL editor in the MaxCompute AI Data Explorer client to perform visual query and analysis. For more operations, see SQL development and data analysis.
Prepare data
Log on to the Object Storage Service (OSS) console and upload the image dataset image_demo.zip. This example uses e-commerce images. If you already have unstructured data in OSS, you can skip this step.
Log on to the latest version of the MaxCompute AI Data Explorer client, create a SQL test file, and create an Object Table.
SET odps.namespace.schema=true; CREATE OBJECT TABLE test_ot_data_image LOCATION 'oss://oss-<region>-internal.aliyuncs.com/<oss_path>/' ; -- Refresh the Object Table metadata. ALTER TABLE test_ot_data_image REFRESH METADATA;View file metadata
SET odps.namespace.schema=true; SELECT * FROM test_ot_data_image limit 5;
Data preview
View the file content of the Object Table.
SET odps.namespace.schema=true; SELECT GET_DATA_FROM_OSS('<project>.<schema>.test_ot_data_image', key, 0, -1, 'OUTPUT_NULL') FROM test_ot_data_image; -- Alternatively, you can use the following syntax. SET odps.namespace.schema=true; SELECT _data FROM test_ot_data_image limit 5;Click the Preview button in the query results to view the image content.
Data export
You can export query results in the following formats:
CSV: A comma-separated text file compatible with tools like Excel.
JSON: A structured JSON format.
Parquet: A columnar storage format that works well for downstream processing in big data scenarios.