2014-02-05 1 views
0

이 요구 사항은 배열/쿼리 응답을 얻은 후 CSV로 직접 보고서 데이터를 내보내고 scv를 빌드 한 후 아마존에 최종 CSV를 다시 업로드해야하는 경우입니다. 시간이 걸리다. redshift postgresql을 사용하여 직접 CSV를 만들 수있는 방법이 있습니까? pgSQL의 - 당신은 S3에 결과를 저장하는 UNLOAD 문을 사용할 수 있습니다PgSQL - 선택된 쿼리 데이터를 헤더가있는 amazon s3으로 직접 보냅니다.

답변

2

아마존의 적색 편이에 버전 pgSQL의 8.0.2

감사합니다 - 선택 쿼리 데이터가 여기에 헤더 와 아마존 S3 서버에 직접 수출 pgSQL의 내 버전입니다 버킷. 이 경우 여러 파일 (하나의 컴퓨팅 노드 당 하나 이상)이 생성됩니다.

모든 파일을 다운로드하고, 로컬로 결합하고, 필요한 경우 정렬 한 다음 열 머리글을 추가하고 결과를 S3에 다시 업로드해야합니다.

EC2 인스턴스를 사용하면 시간이 오래 걸릴 수 없습니다. EC2와 S3를 연결하는 것이 좋습니다. 내 경험에

는 가장 빠른 방법은 쉘 '명령을 사용하는 것입니다 : 잘 내 첫 번째 방법이었다

# run query on the redshift 
export PGPASSWORD='__your__redshift__pass__' 
psql \ 
    -h __your__redshift__host__ \ 
    -p __your__redshift__port__ \ 
    -U __your__redshift__user__ \ 
    __your__redshift__database__name__ \ 
    -c "UNLOAD __rest__of__query__" 

# download all the results 
s3cmd get s3://path_to_files_on_s3/bucket/files_prefix* 

# merge all the files into one 
cat files_prefix* > files_prefix_merged 

# sort merged file by a given column (if needed) 
sort -n -k2 files_prefix_merged > files_prefix_sorted 

# add column names to destination file 
echo -e "column 1 name\tcolumn 2 name\tcolumn 3 name" > files_prefix_finished 

# add merged and sorted file into destination file 
cat files_prefix_sorted >> files_prefix_finished 

# upload destination file to s3 
s3cmd put files_prefix_finished s3://path_to_files_on_s3/bucket/... 

# cleanup 
s3cmd del s3://path_to_files_on_s3/bucket/files_prefix* 
rm files_prefix* files_prefix_merged files_prefix_sorted files_prefix_finished 
+0

을, 그러나 나는 시도 cron 작업을 사용하여 PHP는 16메가바이트 파트 파일을 구문 분석보다 8 분했다 받아 들여지지 않는 22MB의 최종 CSV 파일로 변환 할 수 있습니다. 나는이 접근법으로 실현할 수없는 몇 분 안에 CSV를보고 할 수있는 일을 필요로한다. – rohitmb

+0

은 postgresql이나 amazon 기술로부터 CSV로 직접 내보낼 수있는 어떤 것이있다. – rohitmb