The COS Select feature only supports the SELECT command to extract the required data and reduce the amount of data transferred, which helps lower the costs and request delay. The following are the standard clauses supported by SELECT queries:
COS Select currently does not support clause queries or joins.
The SELECT statement can extract the data you want to see from a COS object. You can query the data at different dimensions such as column name, function, and expression, and the query result will be returned as a list. The format of SELECT statement call is as follows:
SELECT *
SELECT projection [ AS column_alias | column_alias ] [, ...]
The first SELECT statement is marked with *
(asterisk) and will return all the columns in the COS object. The second one uses a user-defined output scalar expression, and projection creates a list of outputs with custom names for each column.
The WHERE clause uses the following syntax:
WHERE condition
The WHERE clause filters data by condition. condition is an expression that returns a Boolean result, and only rows with a return value of TRUE will be output in the result.
The LIMIT clause uses the following syntax:
LIMIT number
The LIMIT clause sets a limit on the number of records to be returned per query, which can be specified using the number parameter.
The SELECT and WHERE clauses can select the fields to be queried in any of the following ways, depending on whether the file format is CSV or JSON.
_N
to specify the data in column N for query. For any CSV files, the column number increases from 1. For example, the first column is numbered _1
, and the second column is numbered _2
. In the SELECT and WHERE clauses, it is valid to specify the column to be queried using _N
or alias._N
.alias.column_name
or column_name
in the SELECT and WHERE clauses in an SQL statement.alias.name
. A nested array can be accessed in a way such as alias.name1.name2.name3
.[]
operator. For example, you can access the second element in a JSON list using alias[1]
. If you need to access a nested array, you can also do so in a way such as alias.name1.name2[1].name3
.Samples
The following is the sample data in the samples:
{
"name": "Leon",
"org": "Tencent",
"projects":
[
{"project_name":"project1", "completed":true},
{"project_name":"project2", "completed":false}
]
}
name
in the sample data and the query result:Select s.name from COSObject s
{"name":"Leon"}
project_name
in the sample data and the query result:Select s.projects[0].project_name from COSObject s
{"project_name":"project1"}
You can use double quotation marks to indicate whether headers in a CSV file and attribute names in a JSON file are case-sensitive. If no double quotation marks are added, the headers/attribute names are case-insensitive. If this is not explicitly specified, COS Select may throw an exception.
Sample 1. Query objects with a header/attribute name containing "NAME".
Because the following sample SQL query does not contain double quotation marks, the query is case-insensitive. As this header is present in the table, a value will be successfully returned eventually.
SELECT s.name from COSObject s
Because the following sample SQL query contains double quotation marks, the query is case-sensitive. As the table does not contain this header, the SQLParsingError
400 error will be eventually returned.
SELECT s."name" from COSObject s
Sample 2. Query objects with a header/attribute name containing "NAME" and "name".
Because the following sample SQL query does not contain double quotation marks, the query is case-insensitive. As the table contains two headers "NAME" and "name", the query command is ambiguous, and the AmbiguousFieldName
exception will be thrown.
SELECT s.name from COSObject s
Because the following sample SQL query contains double quotation marks, the query is case-sensitive. As the table contains the header "NAME", the query result will be successfully returned.
SELECT s."NAME" from COSObject s
The SQL expressions of COS Select have certain reserved fields such as function name, data type, and operator. Sometimes you probably use these reserved fields as column headers in a CSV file or attribute names in a JSON, which may cause conflicts with reserved fields. In this case, you can use double quotation marks to indicate that you are using a custom field; otherwise, COS will return 400 parse error
.
For the complete list of reserved fields, see Reserved Words.
SELECT s."CAST" from COSObject s
The following sample SQL query does not use double quotation marks to indicate that CAST is a user-defined field, so COS will treat it as a reserved field and return 400 parse error
.SELECT s.CAST from COSObject s
In the SELECT statement and the WHERE clause, you can use SQL scalar expressions (expressions that return a scalar). Currently, COS Select supports the following forms:
Apakah halaman ini membantu?