Redshift Schema: 다른 기타 관계형 데이터베이스와 동일한 구조
아래와 같이 스키마를 생성
스키마 및 사용자 관리 쿼리
-- 관리자 역활이 있다면 아래와 같이 생성 가능
CREATE SCHEMA raw_data;
CREATE SCHEMA analytics;
CREATE SCHEMA adhoc;
CREATE SCHEMA pii;
-- 모든 스키마를 리스트하기:
select * from pg_namespace;
-- 유저 생성
-- 이름 : 6글자 이상
-- 비밀번호 : 대소문자, 숫자 포함
CREATE USER keeyong PASSWORD '...';
-- 모든 사용자를 리스트하기
select * from pg_user;
그룹 생성/설정
한 사용자는 다수의 그룹에 속할 수 있음
그룹의 문제는 계승이 안된다는 점
너무 많은 그룹을 만들게 되고 관리가 힘들어짐
예를 들어 다음과 같은 그룹이 존재
그룹 생성 - CREATE GROUP
그룹에 사용자 추가 - ALTER GROUP 그룹이름 ADD USER 사용자이름
그룹에 스키마/테이블 접근 권한 설정 (나중에 설명)
모든 그룹을 리스트하기: select * from pg_group;
CREATE GROUP analytics_users;
CREATE GROUP analytics_authors;
CREATE GROUP pii_users;
ALTER GROUP analytics_authors ADD USER keeyong;
ALTER GROUP analytics_users ADD USER keeyong;
ALTER GROUP pii_users ADD USER keeyong;
역할(Role) 생성/설정
역할은 그룹과 달리 계승 구조를 만들 수 있음
역할은 사용자에게 부여될 수도 있고 다른 역할에 부여될 수도 있음
한 사용자는 다수의 역할에 소속가능함
모든 역할을 리스트하기: select * from SVV_ROLES;
Group과 비교해서 좋은 점
CREATE ROLE staff;
CREATE ROLE manager;
CREATE ROLE external;
-- 하준이라는 사용자에게 스태프 역활을 부여
GRANT ROLE staff TO hajun;
-- 매니저는 스태프의 역활을 계승
GRANT ROLE staff TO ROLE manager;
select * from SVV_ROLES;
COPY 명령을 사용해 raw_data 스키마 밑 3개의 테이블에 레코드를 적재해볼 예정
각 테이블을 CREATE TABLE 명령으로 raw_data 스키마 밑에 생성
이때 각 테이블의 입력이 되는 CSV 파일을 먼저 S3로 복사해야함
S3에서 해당 테이블로 복사를 하려면 Redshift가 S3 접근권한을 가져야함
raw_data 테스트 테이블 만들기 - 테이블 생성
CREATE TABLE raw_data.user_session_channel (
userid integer ,
sessionid varchar(32) primary key,
channel varchar(32)
);
CREATE TABLE raw_data.session_timestamp (
sessionid varchar(32) primary key,
ts timestamp
);
CREATE TABLE raw_data.session_transaction (
sessionid varchar(32) primary key,
refunded boolean,
amount int
);
raw_data 테스트 테이블 만들기 - S3 버킷 생성과 파일 업로드
Redshift의 COPY SQL을 사용해서 앞서 3개의 테이블 내용을 적재해보자
먼저 입력이 되는 CSV 파일들을 적당한 위치에 다운로드 받기
user_session_channel.csv
session_timestamp.csv
session_transaction.csv
AWS 콘솔에서 S3 bucket 하나 만들고 거기로 업로드하기
Redshift에 S3 접근 권한 설정
Redshift가 앞서 만든 S3 버킷을 접근할 수 있어야함
AWS IAM(Identity and Access Management)을 이용해 이에 해당하는 역할(Role)을 만들고 이를 Redshift에 부여해야함
IAM에서 역활을 생성
redshift.read.s3
로 한다.그 후 redshift 클러스터(namespace - 보안 및 암호화)에 IAM을 부여한다
S3 버켓 만들기
redshift와 같은 리전으로 선택
버켓에서 폴더를 하나 만든다 (test_data)
이후 해당 폴더에 앞서 언급한 3개의 csv파일을 업로드한다.