可写外部表
本节我要演示下如何从数据库中卸载数据到平面文件,数据库表为EMP,导出目的文件为emp.txt。
gtlions=# select * from emp; empno | ename | job | mgr | hiredate | sal | comm | deptno
-------+--------+-----------+------+------------+----------+----------+--------
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
8200 | 孙坚 | 销售 | 8180 | 1934-11-30 | 1000.00 | | 70
8220 | 孙策 | 财务 | 8180 | 1937-12-03 | 4100.00 | 345.00 | 50
8240 | 诸葛谨 | 研发 | 8180 | 1940-01-07 | 1000.00 | | 80
7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
8000 | 叶子 | 老大 | | 1984-01-18 | 99999.00 | 99999.00 | 90
8010 | 刘备 | 经理 | 8000 | 1904-01-01 | 21000.00 | 200.00 | 60
8020 | 张飞 | 销售 | 8010 | 1907-02-04 | 41000.00 | 300.00 | 70
8040 | 赵云 | 财务 | 8010 | 1910-03-07 | 61000.00 | | 50
8060 | 关羽 | 研发 | 8010 | 1913-04-10 | 81000.00 | | 80
8080 | 诸葛亮 | 闲人 | 8010 | 1916-05-13 | 10100.00 | | 60
8100 | 曹操 | 经理 | 8000 | 1919-06-16 | 12100.00 | 389.00 | 60
8120 | 曹丕 | 销售 | 8100 | 1922-07-19 | 14100.00 | | 70
8140 | 曹植 | 财务 | 8100 | 1925-08-22 | 16100.00 | 700.00 | 50
8160 | 曹昂 | 研发 | 8100 | 1928-09-25 | 18100.00 | | 80
8180 | 孙权 | 经理 | 8000 | 1931-10-28 | 20100.00 | 409.00 | 60
(28 rows)
gtlions=# create writable external table emp_exp(like emp) location ('gpfdist://o564gtser1:8080/emp_exp.txt') format 'text';
NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table
CREATE EXTERNAL TABLE
gtlions=# insert into emp_exp select * from emp;
INSERT 0 28
gtlions=# select * from emp_exp;
ERROR: it is not possible to read from a WRITABLE external table.
HINT: Create the table as READABLE instead
[gpadmin@o564gtser1 gpfdist]$ cat emp_exp.txt
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-02-22 1250.00 500.00 30
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 \N 30
7782 CLARK MANAGER 7839 1981-06-09 2450.00 \N 10
7788 SCOTT ANALYST 7566 1982-12-09 3000.00 \N 20
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30
7876 ADAMS CLERK 7788 1983-01-12 1100.00 \N 20
7900 JAMES CLERK 7698 1981-12-03 950.00 \N 30
7902 FORD ANALYST 7566 1981-12-03 3000.00 \N 20
7934 MILLER CLERK 7782 1982-01-23 1300.00 \N 10
8200 孙坚 销售 8180 1934-11-30 1000.00 \N 70
8220 孙策 财务 8180 1937-12-03 4100.00 345.00 50
8240 诸葛谨 研发 8180 1940-01-07 1000.00 \N 80
7369 SMITH CLERK 7902 1980-12-17 800.00 \N 20
7566 JONES MANAGER 7839 1981-04-02 2975.00 \N 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 1400.00 30
7839 KING PRESIDENT \N 1981-11-17 5000.00 \N 10
8000 叶子 老大 \N 1984-01-18 99999.00 99999.00 90
8010 刘备 经理 8000 1904-01-01 21000.00 200.00 60
8020 张飞 销售 8010 1907-02-04 41000.00 300.00 70
8040 赵云 财务 8010 1910-03-07 61000.00 \N 50
8060 关羽 研发 8010 1913-04-10 81000.00 \N 80
8080 诸葛亮 闲人 8010 1916-05-13 10100.00 \N 60
8100 曹操 经理 8000 1919-06-16 12100.00 389.00 60
8120 曹丕 销售 8100 1922-07-19 14100.00 \N 70
8140 曹植 财务 8100 1925-08-22 16100.00 700.00 50
8160 曹昂 研发 8100 1928-09-25 18100.00 \N 80
8180 孙权 经理 8000 1931-10-28 20100.00 409.00 60
本节我要演示下如何从数据库中卸载数据到平面文件,数据库表为EMP,导出目的文件为emp.txt。
gtlions=# select * from emp; empno | ename | job | mgr | hiredate | sal | comm | deptno
-------+--------+-----------+------+------------+----------+----------+--------
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30
7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 30
7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 10
7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | | 20
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30
7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | | 20
7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 30
7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 20
7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
8200 | 孙坚 | 销售 | 8180 | 1934-11-30 | 1000.00 | | 70
8220 | 孙策 | 财务 | 8180 | 1937-12-03 | 4100.00 | 345.00 | 50
8240 | 诸葛谨 | 研发 | 8180 | 1940-01-07 | 1000.00 | | 80
7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 20
7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 20
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30
7839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 10
8000 | 叶子 | 老大 | | 1984-01-18 | 99999.00 | 99999.00 | 90
8010 | 刘备 | 经理 | 8000 | 1904-01-01 | 21000.00 | 200.00 | 60
8020 | 张飞 | 销售 | 8010 | 1907-02-04 | 41000.00 | 300.00 | 70
8040 | 赵云 | 财务 | 8010 | 1910-03-07 | 61000.00 | | 50
8060 | 关羽 | 研发 | 8010 | 1913-04-10 | 81000.00 | | 80
8080 | 诸葛亮 | 闲人 | 8010 | 1916-05-13 | 10100.00 | | 60
8100 | 曹操 | 经理 | 8000 | 1919-06-16 | 12100.00 | 389.00 | 60
8120 | 曹丕 | 销售 | 8100 | 1922-07-19 | 14100.00 | | 70
8140 | 曹植 | 财务 | 8100 | 1925-08-22 | 16100.00 | 700.00 | 50
8160 | 曹昂 | 研发 | 8100 | 1928-09-25 | 18100.00 | | 80
8180 | 孙权 | 经理 | 8000 | 1931-10-28 | 20100.00 | 409.00 | 60
(28 rows)
gtlions=# create writable external table emp_exp(like emp) location ('gpfdist://o564gtser1:8080/emp_exp.txt') format 'text';
NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table
CREATE EXTERNAL TABLE
gtlions=# insert into emp_exp select * from emp;
INSERT 0 28
gtlions=# select * from emp_exp;
ERROR: it is not possible to read from a WRITABLE external table.
HINT: Create the table as READABLE instead
[gpadmin@o564gtser1 gpfdist]$ cat emp_exp.txt
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-02-22 1250.00 500.00 30
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 \N 30
7782 CLARK MANAGER 7839 1981-06-09 2450.00 \N 10
7788 SCOTT ANALYST 7566 1982-12-09 3000.00 \N 20
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30
7876 ADAMS CLERK 7788 1983-01-12 1100.00 \N 20
7900 JAMES CLERK 7698 1981-12-03 950.00 \N 30
7902 FORD ANALYST 7566 1981-12-03 3000.00 \N 20
7934 MILLER CLERK 7782 1982-01-23 1300.00 \N 10
8200 孙坚 销售 8180 1934-11-30 1000.00 \N 70
8220 孙策 财务 8180 1937-12-03 4100.00 345.00 50
8240 诸葛谨 研发 8180 1940-01-07 1000.00 \N 80
7369 SMITH CLERK 7902 1980-12-17 800.00 \N 20
7566 JONES MANAGER 7839 1981-04-02 2975.00 \N 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 1400.00 30
7839 KING PRESIDENT \N 1981-11-17 5000.00 \N 10
8000 叶子 老大 \N 1984-01-18 99999.00 99999.00 90
8010 刘备 经理 8000 1904-01-01 21000.00 200.00 60
8020 张飞 销售 8010 1907-02-04 41000.00 300.00 70
8040 赵云 财务 8010 1910-03-07 61000.00 \N 50
8060 关羽 研发 8010 1913-04-10 81000.00 \N 80
8080 诸葛亮 闲人 8010 1916-05-13 10100.00 \N 60
8100 曹操 经理 8000 1919-06-16 12100.00 389.00 60
8120 曹丕 销售 8100 1922-07-19 14100.00 \N 70
8140 曹植 财务 8100 1925-08-22 16100.00 700.00 50
8160 曹昂 研发 8100 1928-09-25 18100.00 \N 80
8180 孙权 经理 8000 1931-10-28 20100.00 409.00 60
这样就把数据卸载到平面文件了,注意writable是不允许除了insert之外的操作的。
-EOF-