Configure conditioned replication between databases in a single instance of Cloud Spanner

77 Views Asked by At

im working on a school project about replication in databases. I created a cloud spanner instance with 5 databases. The "main" database is named electores_nac and i want to replicate only certain data to another DB named electores_prov_buenos_aires. The objective is replicate all the subjects that live in a state, It doesn't matter what city you are from as long as you are from the state used to filter, in this case "Buenos Aires". That it's because in another DB, named electores_buenos_aires_laplata i want to replicate from electores_prov_buenos_aires only the subjects that live in a specific city from that state. My electores_nac DB definition:

CREATE TABLE ciudades (
  idciudad bigint NOT NULL,
  nombre character varying(100),
  idprovincia bigint,
  PRIMARY KEY(idciudad)
);

CREATE TABLE electores (
  idtipo bigint NOT NULL,
  nro_documento character varying(20) NOT NULL,
  apellidos character varying(100),
  nombres character varying(100),
  fecha_nacimiento date,
  idprofesion bigint,
  idciudad bigint,
  calle character varying(150),
  altura bigint,
  PRIMARY KEY(idtipo, nro_documento),
  CONSTRAINT fk_electores_ciudades FOREIGN KEY (idciudad) REFERENCES ciudades(idciudad)
);

CREATE TABLE profesiones (
  idprofesion bigint NOT NULL,
  descripcion character varying(100),
  PRIMARY KEY(idprofesion)
);

ALTER TABLE electores ADD CONSTRAINT fk_electores_profesiones FOREIGN KEY (idprofesion) REFERENCES profesiones(idprofesion);

CREATE TABLE provincias (
  idprovincia bigint NOT NULL,
  nombre character varying(100),
  PRIMARY KEY(idprovincia)
);

ALTER TABLE ciudades ADD CONSTRAINT fk_ciudades_provincias FOREIGN KEY (idprovincia) REFERENCES provincias(idprovincia);

CREATE TABLE tipos_documentos (
  idtipo bigint NOT NULL,
  descripcion character varying(150),
  sigla character varying(20),
  PRIMARY KEY(idtipo)
);

ALTER TABLE electores ADD CONSTRAINT fk_electores_tipos_documentos FOREIGN KEY (idtipo) REFERENCES tipos_documentos(idtipo);

An image from the layout of the DB: Layout of the BD

I tried to use the following cloud shell command to configure the replication but with no success:

gcloud spanner databases update electores_prov_buenos_aires --replication-config=us-east5 --replica-type=READ_WRITE --replica-database=electores_nac --data-splitting-basis=provincias#1
2

There are 2 best solutions below

0
SQLmojoe On

Replication in Cloud Spanner refers to its automated data replication mechanism to provide distribution (for locality, scale) and replicas (for HA/DR). It is not a user feature to make copies of a table or databases. See https://cloud.google.com/spanner/docs/replication for more details.

To make copies of all or part of the data in one database to another, you can use Spanner's import/export feature. Full copies can made using backup/restore.

0
Hailong Wen On

As SQLmojoe mentioned above, this is not the use case for "replication".

Not sure what you are trying to achieve here. If you would like to do a one-time copy of the data, you can write a program to query based on the filter and then insert those rows to another DB. If you would like to monitor any future changes, you can set up change stream.