[Yandex Cloud documentation](../../index.md) > [Yandex Query](../index.md) > Data sources and sinks > Working with Yandex Object Storage > Reading data via connections

# Reading data from Object Storage via Query connections

Using Yandex Object Storage connections is convenient for prototyping and initial data access configuration.

Query example for reading data:

```sql
SELECT
    *
FROM
    object_storage.`*.tsv`
WITH
(
    format=tsv_with_names,
    SCHEMA
    (
        `timestamp` Uint32,
        action String
    )
);
```

## Setting up a connection {#create_connection}

To create a connection to Object Storage:

1. In the [management console](https://console.yandex.cloud), select the folder where you want to create a connection.
1. Navigate to **Yandex Query**.
1. In the left-hand panel, switch to the **Connections** tab.
1. Click ![info](../../_assets/console-icons/plus.svg) **Create new**.
1. Specify the connection settings:

   1. Under **General parameters**:

      * **Name**: Object Storage connection name.
      * **Type**: `Object Storage`.
  
   1. Under **Connection type parameters**:

      * **Bucket auth**: Select `Public` or `Private` depending on the type of read access to objects in the bucket.

        For a public bucket, specify a name in the **Bucket** field.
        For a private bucket:
        * Select **Cloud and Folder** where the data source is located.
        * Select an existing bucket or create a new one.
        * Select an existing [service account](../../iam/concepts/users/service-accounts.md) or create a new one. Assign it the [`storage.viewer`](../../storage/security/index.md#storage-viewer) role required to access the data.

          To use the service account on your behalf, you need the `iam.serviceAccounts.user` [role](../../iam/security/index.md#iam-serviceAccounts-user).

1. Click **Create**.

## Data model {#data_model}

Object Storage stores data as binary files. To read data, use the following SQL statement:

```sql
SELECT
    <expression>
FROM
    <connection>.<path>
WITH(
    FORMAT = "<data_format>",
    COMPRESSION = "<compression_format>",
    SCHEMA = (<schema_description>))
WHERE
    <filter>;
```

Where:

* `<connection>`: Storage [connection](#create_connection) name.
* `<path>`: Path to file(s) within the bucket. Supports the `*` wildcard.
* `<data_format>`: File [data format](formats.md#formats).
* `<compression_format>`: File [compression format](formats.md#compression_formats).
* `<data_schema>`: [Description of data](#schema) stored in files.

### Data schema {#schema}

The data schema includes the following fields:

- Field name
- Field type
- Required flag

For example, the schema below describes a required field `Year` of type `Int32`:

```text
Year Int32 NOT NULL
```

If a `NOT NULL` field is missing from the file being processed, the operation will terminate with an error. If a nullable field is missing from the file being processed, no error will occur, and the field will be set to `NULL`. The `NULL` keyword may be omitted in nullable fields.

### Automatic schema inference {#inferring}

Automatic schema inference is supported for all [data formats](formats.md#formats) except `raw` and `json_as_string`. It is convenient when a schema has many fields. Instead of entering them manually, you can use the `WITH_INFER` hint:

```sql
SELECT
    <expression>
FROM
    <connection>.<path>
WITH(
    FORMAT = "<data_format>",
    COMPRESSION = "<compression_format>",
    WITH_INFER="true")
WHERE
    <filter>;
```

Where:

* `<connection>`: Storage [connection](#create_connection) name.
* `<path>`: Path to file(s) within the bucket. Supports the `*` wildcard.
* `<data_format>`: File [data format](formats.md#formats).
* `<compression_format>`: File [compression format](formats.md#compression_formats).


This query will automatically infer field names and types.

### Data path formats {#path_format}

Yandex Query supports the following data path formats:

| Path format | Description | Example |
|----|----|---|
| Path ends with `/` | Points to a folder | Path `/a` addresses everything inside a folder:<br>`/a/b/c/d/1.txt`<br>`/a/b/2.csv` |
| Path contains a `*` wildcard | Points to any file in any subfolder matching the pattern | Path `/a/*.csv` addresses all files in the following subfolders:<br>`/a/b/c/1.csv`<br>`/a/2.csv`<br>`/a/b/c/d/e/f/g/2.csv` |
| Path does not end with `/` and contains no wildcards | Points to an individual file | Path `/a/b.csv` refers to the specific file `/a/b.csv` |

## Example of reading data via connections {#read_example}

Query example for reading data from Object Storage:

```sql
SELECT
    *
FROM
    connection.`folder/filename.csv`
WITH(
    format='csv_with_names',
    SCHEMA
    (
        Year int,
        Manufacturer String,
        Model String,
        Price Double
    )
);
```

Where:

* `connection`: Object Storage connection name.
* `folder/filename.csv`: File path within the Object Storage bucket.
* `SCHEMA`: Data schema described in the file.