

# Loading data into member clusters

When you load data into a database cluster group for Aurora, you load it into its member clusters. Where a table sits whole on one member cluster, you load it as you would load any Aurora DB cluster. Where a table's rows spread across member clusters, as in a scaleout table, you divide the input by the routing function first. The routing layer can't divide a bulk load for you.

The table decides which loading path you use. A table mapped whole to one member cluster takes a single bulk load. A scaleout table needs one load per member cluster, because each row must reach the member cluster that its routing function selects. Loading changes data on the member clusters, not data mappings or routing functions.

## Before you begin
<a name="databaseclustergroups-loading-data-before"></a>

Before you load, do the following:
+ Create the tables on the member clusters that hold them, and declare their placement with data mappings and, for a scaleout table, the scaleout table definition. For more information, see [Changing data mappings](databaseclustergroups-changing-mappings.md) and [Creating and loading a scaleout table](databaseclustergroups-create-scaleout-table.md).
+ At the routing endpoint, read the member cluster aliases from `dbclustergroup_sys.member_cluster_mappings`, so that you can pin a session to a named member cluster.
+ Create the users that the load runs as: a router user, and a member cluster user with the same name on each member cluster that you load.
+ Decide for each table whether it sits whole on one member cluster or spreads across member clusters as a scaleout table.
+ Give every large table a primary key before you load it.

## Loading a table that sits on one member cluster
<a name="databaseclustergroups-loading-data-whole"></a>

A table that a data mapping places whole on one member cluster takes a single bulk load.

**To load a table that sits on one member cluster**

1. At the routing endpoint, read the table's row in `dbclustergroup_sys.data_mappings`, and confirm that it names one member cluster alias.

1. Connect in a session pinned to that member cluster, or over a direct connection to it.

1. Run the bulk load.

1. At the routing endpoint, run a routed read of the table, and compare its row count with your input.

Load with `LOAD DATA LOCAL INFILE` in a session pinned to the member cluster, or over a direct connection to it. Name the table by its cluster name. `LOAD DATA LOCAL INFILE` needs `local_infile` turned on in the member cluster and in the client.

```sql
SET SESSION router_cluster_pin = '{{MEMBER_CLUSTER_ALIAS}}';
LOAD DATA LOCAL INFILE '{{FILE}}' INTO TABLE {{CLUSTER_DB}}.{{CLUSTER_TABLE}}
  FIELDS TERMINATED BY ',';
SET SESSION router_cluster_pin = DEFAULT;
```

To load from Amazon S3 over a direct connection to the member cluster, see [Loading data into an Amazon Aurora MySQL DB cluster from text files in an Amazon S3 bucket](AuroraMySQL.Integrating.LoadFromS3.md).

In query routing mode, a `LOAD DATA` at the routing endpoint also loads such a table. The routing layer routes the statement by its `INTO TABLE` target to the member cluster that the data mapping names, as it routes an `INSERT` into that table.

## Loading a scaleout table
<a name="databaseclustergroups-loading-data-scaleout"></a>

A bulk load into a scaleout table through the routing layer isn't supported. In an `INSERT`, the routing layer reads the routing key value from the statement and routes the row. A bulk load sends its rows as a stream that the routing layer doesn't parse, so it can't tell which member cluster each row belongs to. Divide the input by the routing function, and load each member cluster's part in a session pinned to that member cluster. For how the routing function maps keys to collocations, see [Choosing a routing key and routing function](databaseclustergroups-routing-key.md).

Under a `MOD` routing function, compute each row's remainder as `{{KEY}} % {{N}}`, which keeps the sign of the key (for example, -7 % 4 is -3). A negative key gives a negative remainder, so list the negative remainders in the routing function's `VALUES IN` lists too. In the routing function, find the collocation whose `VALUES IN` list holds the remainder. Then read that collocation's `member_cluster_alias` from `dbclustergroup_sys.data_mappings`. Under a `PREFIX` routing function, keep each key up to the end of its {{depth}}-th segment, and find the collocation that lists that prefix. Under a `COLUMN` routing function, find the collocation whose `VALUES IN` list holds the key, or whose `VALUES IN RANGE` bounds include it. Compare character keys under the collation of the routing key, as the routing layer does. Under a `HASH` routing function, the routing layer computes the hash itself. No statement returns the hash or the collocation of a key value, so you can't divide the input in your client. Load a `HASH` scaleout table at the routing endpoint with `INSERT` statements, as described after the following procedure.

**A pinned load bypasses the routing function**  
A pinned session sends rows to the member cluster that you name, not to the member cluster that the routing function selects. A routed read computes the key's member cluster and looks only there, so it never finds a row loaded onto the wrong member cluster. Divide the input strictly by the routing function.

**To load a scaleout table**

1. In your client, divide the input file by the routing function, one file per member cluster.

1. At the routing endpoint, outside a transaction, pin a session to the first member cluster.

1. In the pinned session, load that member cluster's file into its member table, by the member table's cluster name.

1. In the same session, clear the pin.

1. Repeat steps 2 through 4 for each remaining member cluster.

1. At the routing endpoint, run a routed read for one key value in each collocation.

In query routing mode, the routing layer rejects `LOAD DATA` into a scaleout table, because the statement carries no routing key value. In a pinned session, `LOAD DATA LOCAL INFILE` and `INSERT` both load a member table.

```sql
SET SESSION router_cluster_pin = '{{MEMBER_CLUSTER_ALIAS_1}}';
LOAD DATA LOCAL INFILE '{{FILE_FOR_CLUSTER_1}}' INTO TABLE {{CLUSTER_DB}}.{{CLUSTER_TABLE}}
  FIELDS TERMINATED BY ',';
SET SESSION router_cluster_pin = DEFAULT;
```

If you can't compute the member cluster of each row yourself, load through the routing endpoint with `INSERT` statements instead, each with its routing key value. Send one row per statement, or several rows only when they all map to the same collocation, because the routing layer rejects a multi-row `INSERT` whose rows span collocations.

No statement returns the hash or the collocation of a routing key value. `EXPLAIN` at the routing endpoint, on a statement that carries a key value, names the member cluster of that key under `Target Member Clusters`, one key in each statement. For more information, see [EXPLAIN](databaseclustergroups-sql-explain.md).

## Loading from an existing database
<a name="databaseclustergroups-loading-data-existing"></a>

Where the data comes from decides how you bring it in:
+ **An Aurora DB cluster.** Restore a snapshot of the cluster onto Aurora MySQL version 8.4.900, and add the restored cluster to the group rather than copying its rows. Writes made after the snapshot don't reach the restored member cluster, so stop writes or copy them before you switch. A cluster that joins keeps its data, and data mappings make that data reachable through the routing endpoint. For more information, see [Add member clusters](databaseclustergroups-gs-add-members.md).
+ **A member cluster of the same group.** Run a data movement job. The source keeps serving traffic during the copy, and queries on the moving objects are blocked only during a short cutover. For more information, see [Moving data between member clusters](databaseclustergroups-data-movement.md).
+ **A source outside Aurora.** Load into a member cluster endpoint with your engine's import tools, or with AWS Database Migration Service (AWS DMS). For a table mapped whole, load the member cluster that holds it. For a scaleout table, load each member cluster with the rows that its routing function selects. Then add any data mappings that don't exist. For more information about AWS DMS, see the [AWS Database Migration Service User Guide](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html).

If the binary log isn't on for the source member cluster, a data movement job turns it on without a restart. The job keeps it on until the job ends, and then returns the setting to its configured value, also without a restart. You don't need to turn it on yourself. The preview of a move shows a warning when the job will turn the binary log on, so you can measure the effect on write throughput before you start.

## Monitoring a load
<a name="databaseclustergroups-loading-data-monitoring"></a>

How you watch a load depends on the path that you used:
+ **A bulk load or an import tool.** Read the client's output and the target member cluster's metrics. Each member cluster keeps publishing its own metrics and logs, so a load is visible there as it is on any Aurora DB cluster. To compare member clusters side by side, see [Monitoring database cluster groups for Aurora](databaseclustergroups-monitoring.md).
+ **A data movement job.** At the routing endpoint, read the job's row in `dbclustergroup_data_movement.move_info`, filtered on its job ID. For more information, see [Reading move status](databaseclustergroups-running-a-move.md#databaseclustergroups-move-monitoring).

To separate load traffic from routed application traffic on one member cluster, read each session's `_rds_connection_source` connection attribute, as described in [Identifying hot member clusters](databaseclustergroups-ts-uneven-load.md#databaseclustergroups-identify-heat).

## Limitations
<a name="databaseclustergroups-loading-data-limits"></a>

The following table lists the limits on loading and what to do instead.


| Limit | What to do instead | 
| --- | --- | 
| A bulk load into a scaleout table through the routing layer isn't supported. | Divide the input and load it in a pinned session per member cluster, or insert one row per statement at the routing endpoint. | 
| A statement loads rows for one member cluster only. | Split the load by member cluster. | 
| The routing layer doesn't correct the placement of a pinned load. | Place each row on the member cluster that the routing function selects. | 
| A data movement job moves a whole collocation, not part of a table's rows. | Load the rows yourself, as described on this page. | 
| A job moves at most 10,000 tables and 1 TiB of table data, indexes included. | Divide the collocation so that each move is within these limits, and run one move for each part. | 

Each move runs as a single task, so the rate of one move doesn't rise with the size of the member clusters.

## Related pages
<a name="databaseclustergroups-loading-data-related"></a>

For related tasks and concepts, see the following pages:
+ [Creating and loading a scaleout table](databaseclustergroups-create-scaleout-table.md)
+ [Moving data between member clusters](databaseclustergroups-data-movement.md)
+ [Copying data and running backfills](databaseclustergroups-backfill.md)