CockroachDB EXPORT/IMPORT

 

https://www.cockroachlabs.com/docs/stable/export
https://www.cockroachlabs.com/docs/stable/import
https://www.cockroachlabs.com/docs/stable/cloud-storage-authentication

OS: Rocky Linux 9.2 
CockroachDB: v23.2.0


-- 1. nodelocal


cockroach sql \
--certs-dir=/cockroach/crc/certs \
--host=mmm191

create database test;
use test;


create table tab1(col1 int, col2 varchar(10), col3 timestamp);

insert into tab1 values(1,'A','2024-02-10 11:22:33');
insert into tab1 values(2,'B','2024-02-11 11:22:33');
insert into tab1 values(3,'C','2024-02-12 11:22:33');

select * from tab1;

export into csv 'nodelocal://self/tab1'
with nullas='' from table tab1;

                              filename                             | rows | bytes
-------------------------------------------------------------------+------+--------
  export17b32562b2bd10cd0000000000000001-n941832856274665475.0.csv |    3 |    72


-- CSVが出力されたノードで実行
cockroach sql \
--certs-dir=/cockroach/crc/certs \
--host=mmm191

use test;

import into tab1 CSV data ('nodelocal://self/tab1/export17b32562b2bd10cd0000000000000001-n941832856274665475.0.csv');


select * from tab1;

 

-- 2. userfile

cockroach sql \
--certs-dir=/cockroach/crc/certs \
--host=mmm191

use test;


create table tab2(col1 int, col2 varchar(10), col3 timestamp);

insert into tab2 values(1,'A','2024-02-10 11:22:33');
insert into tab2 values(2,'B','2024-02-11 11:22:33');
insert into tab2 values(3,'C','2024-02-12 11:22:33');

select * from tab2;

export into csv 'userfile://test.public.tab2/'
with nullas='' from table tab2;

                              filename                             | rows | bytes
-------------------------------------------------------------------+------+--------
  export17b23e732568e0db0000000000000001-n941835807143264259.0.csv |    3 |    72
(1 row)

import into tab2 CSV data ('userfile://test.public.tab2/export17b23e732568e0db0000000000000001-n941835807143264259.0.csv');


select * from tab2;


-- 3. S3


cockroach sql \
--certs-dir=/cockroach/crc/certs \
--host=mmm191

use test;


create table tab3(col1 int, col2 varchar(10), col3 timestamp);

insert into tab3 values(1,'A','2024-02-10 11:22:33');
insert into tab3 values(2,'B','2024-02-11 11:22:33');
insert into tab3 values(3,'C','2024-02-12 11:22:33');

select * from tab3;

 

export into csv 's3://backet123/tab3/?AWS_ACCESS_KEY_ID=xxxx&AWS_SECRET_ACCESS_KEY=xxxx'
with nullas='' from table tab3;

                              filename                             | rows | bytes
-------------------------------------------------------------------+------+--------
  export17b24073256ebb870000000000000001-n941842992666083331.0.csv |    3 |    72


import into tab3 CSV data ('s3://backet123/tab3/export17b24073256ebb870000000000000001-n941842992666083331.0.csv/?AWS_ACCESS_KEY_ID=xxxx&AWS_SECRET_ACCESS_KEY=xxxx');


select * from tab3;