当前位置: 首页 > 工具软件 > AutoRest > 使用案例 >

Oracle REST 数据服务 (ORDS):AutoREST

邹晟睿
2023-12-01

文概述了 Oracle REST 数据服务 (ORDS) 的 AutoREST 功能,该功能允许您将表和视图自动公开为 RESTful Web 服务。

创建测试数据库用户

我们需要一个新的数据库用户来进行测试。

CONN / AS SYSDBA
ALTER SESSION SET CONTAINER=pdb1;

DROP USER testuser2 CASCADE;
CREATE USER testuser2 IDENTIFIED BY testuser2
  DEFAULT TABLESPACE users QUOTA UNLIMITED ON users;
  
GRANT CREATE SESSION, CREATE TABLE TO testuser2;

创建并填充 EMP 表的副本。

CONN testuser2/testuser2@pdb1

CREATE TABLE EMP (
  EMPNO NUMBER(4,0), 
  ENAME VARCHAR2(10 BYTE), 
  JOB VARCHAR2(9 BYTE), 
  MGR NUMBER(4,0), 
  HIREDATE DATE, 
  SAL NUMBER(7,2), 
  COMM NUMBER(7,2), 
  DEPTNO NUMBER(2,0), 
  CONSTRAINT PK_EMP PRIMARY KEY (EMPNO)
  );
  
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7369,'SMITH','CLERK',7902,to_date('17-DEC-80','DD-MON-RR'),800,null,20);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7499,'ALLEN','SALESMAN',7698,to_date('20-FEB-81','DD-MON-RR'),1600,300,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7521,'WARD','SALESMAN',7698,to_date('22-FEB-81','DD-MON-RR'),1250,500,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7566,'JONES','MANAGER',7839,to_date('02-APR-81','DD-MON-RR'),2975,null,20);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7654,'MARTIN','SALESMAN',7698,to_date('28-SEP-81','DD-MON-RR'),1250,1400,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7698,'BLAKE','MANAGER',7839,to_date('01-MAY-81','DD-MON-RR'),2850,null,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7782,'CLARK','MANAGER',7839,to_date('09-JUN-81','DD-MON-RR'),2450,null,10);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7788,'SCOTT','ANALYST',7566,to_date('19-APR-87','DD-MON-RR'),3000,null,20);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7839,'KING','PRESIDENT',null,to_date('17-NOV-81','DD-MON-RR'),5000,null,10);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7844,'TURNER','SALESMAN',7698,to_date('08-SEP-81','DD-MON-RR'),1500,0,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7876,'ADAMS','CLERK',7788,to_date('23-MAY-87','DD-MON-RR'),1100,null,20);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7900,'JAMES','CLERK',7698,to_date('03-DEC-81','DD-MON-RR'),950,null,30);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7902,'FORD','ANALYST',7566,to_date('03-DEC-81','DD-MON-RR'),3000,null,20);
insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7934,'MILLER','CLERK',7782,to_date('23-JAN-82','DD-MON-RR'),1300,null,10);
commit;

启用 ORDS 和 AutoREST

为测试架构启用 REST Web 服务。我们为架构使用任何唯一且合法的 URL 映射模式,因此我们不会公开架构名称。在这种情况下,我们使用“hr”作为模式别名。

BEGIN
  ORDS.enable_schema(
    p_enabled             => TRUE,
    p_schema              => 'TESTUSER2',
    p_url_mapping_type    => 'BASE_PATH',
    p_url_mapping_pattern => 'hr',
    p_auto_rest_auth      => FALSE
  );
    
  COMMIT;
END;
/

现在可以使用以下基本 URL 引用模式中的 Web 服务。

http://localhost:8080/ords/hr/

最后一步是为 EMP 表启用 AutoREST。

BEGIN
  ORDS.enable_object (
    p_enabled      => TRUE, -- Default  { TRUE | FALSE }
    p_schema       => 'TESTUSER2',
    p_object       => 'EMP',
    p_object_type  => 'TABLE', -- Default  { TABLE | VIEW }
    p_object_alias => 'employees'
  );
    
  COMMIT;
END;
/

请注意,该对象称为 EMP,但我们希望 Web 服务将其称为“员工”,因此是对象别名。要禁用 AutoREST,请使用P_ENABLED设置为 FALSE的参数重复调用。

我们现在准备开始。

GET Web Services (READ)

默认情况下,浏览器使用 GET 方法进行 HTTP 调用,因此可以从浏览器 URL 栏中调用以下 URL。

以下 URL 分别返回 JSON 文档,其中包含有关测试模式中对象和指定对象结构的元数据。

Available Objects : http://localhost:8080/ords/hr/metadata-catalog/
Object Description: http://localhost:8080/ords/hr/metadata-catalog/employees/

有多种方法可以从启用 AutoREST 的表或视图中查询数据。以下 URL 返回 EMP 表中的所有数据。请记住,对象别名设置为“employees”。

http://localhost:8080/ords/hr/employees/

使用主键值返回来自单个行的数据。逗号分隔的列表用于连接键。

http://localhost:8080/ords/hr/employees/7521

可以使用 offset 和 limit 参数对数据进行分页。以下 URL 从 EMP 表中返回一个包含 5 行数据的页面,从第 6 行开始。

http://localhost:8080/ords/hr/employees/?offset=5&limit=5

有多种运算符可用于过滤从对象返回的数据(此处)。下面显示了一些示例。

# job = 'MANAGER'
http://localhost:8080/ords/hr/employees/?q={"job":"MANAGER"}

# salary >= 3000
http://localhost:8080/ords/hr/employees/?q={"sal":{"$gte":3000}}

# job = 'MANAGER' AND salary >= 2000
http://localhost:8080/ords/hr/employees/?q={"job":"MANAGER","sal":{"$gte":2000}}

# Top paid manager.
http://localhost/ords/hr/employees/?q={"job":"MANAGER","$orderby":{"sal":"desc"}}&offset=0&limit=1

POST Web 服务(插入)

使用 POST 方法创建新记录。执行此操作所需的 URL、方法、标头和有效负载如下所示。

URL        : http://localhost:8080/ords/hr/employees/
Method     : POST
Header     : Content-Type: application/json
Raw Payload: { "empno": 9999, "ename": "HALL", "job": "ANALYST", "mgr": 7782, "hiredate": "01-JAN-2016", "sal": 1000, "comm": null, "deptno": 10 }

如果有效负载放在名为“/tmp/insert-payload.json”的文件中,则以下“curl”命令将向 EMP 表中插入一行。

$ curl -i -X POST --data-binary @/tmp/insert-payload.json -H "Content-Type: application/json" http://localhost:8080/ords/hr/employees/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Location: http://localhost:8080/ords/hr/employees/9999
ETag: "LrUFzrlvUWKDl8yIQWriVCbyGqK7Phzh5S/H/out3bWUcMbQTGmtqFD2TvNGioU/zaYFwgiqE79yj9ygs5U2UQ=="
Location: http://localhost:8080/ords/hr/employees/9999
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 25 Jun 2016 17:04:49 GMT

{"empno":9999,"ename":"HALL","job":"ANALYST","mgr":7782,"hiredate":"2016-01-01T00:00:00Z","sal":1000,"comm":null,"deptno":10,"links":[{"rel":"self","href":"http://localhost:8080/ords/hr/employees/9999"},{"rel":"edit","href":"http://localhost:8080/ords/hr/employees/9999"},{"rel":"describedby","href":"http://localhost:8080/ords/hr/metadata-catalog/employees/item"},{"rel":"collection","href":"http://localhost:8080/ords/hr/employees/"}]}
$

除了 Web 服务输出之外,我们还可以通过查询表看到该行已创建。

SELECT * FROM emp WHERE empno = 9999;

     EMPNO ENAME      JOB	       MGR HIREDATE	    SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      9999 HALL       ANALYST	      7782 01-JAN-16	   1000 		   10

SQL>

PUT Web 服务(更新)

使用 PUT 方法更新或插入记录(如果它们丢失)。执行此操作所需的 URL、方法、标头和有效负载如下所示。

URL        : http://localhost:8080/ords/hr/employees/9999
Method     : PUT
Header     : Content-Type: application/json
Raw Payload: { "ename": "WOOD", "deptno": 20 }

请注意,要更新的行由 URL 确定,类似于使用主键的 GET 调用。除 PK 列外,任何未在有效负载中指定的列都设置为空。

如果有效负载放在名为“/tmp/update-payload.json”的文件中,则以下“curl”命令将更新 EMP 表中的一行。

$ curl -i -X PUT --data-binary @/tmp/update-payload.json -H "Content-Type: application/json" http://localhost:8080/ords/hr/employees/9999
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Location: http://localhost:8080/ords/hr/employees/9999
ETag: "+/HytdM4atnPSuuDDaUrG5ZQNF9DtlWhM3NAalo3vqQ7a0ICHNrscmma+1ktAQVOnD66H+Pz88FahM96Ch9dDw=="
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 25 Jun 2016 17:10:55 GMT

{"empno":9999,"ename":"WOOD","job":null,"mgr":null,"hiredate":null,"sal":null,"comm":null,"deptno":20,"links":[{"rel":"self","href":"http://localhost:8080/ords/hr/employees/9999"},{"rel":"edit","href":"http://localhost:8080/ords/hr/employees/9999"},{"rel":"describedby","href":"http://localhost:8080/ords/hr/metadata-catalog/employees/item"},{"rel":"collection","href":"http://localhost:8080/ords/hr/employees/"}]}
$

除了 Web 服务输出之外,我们还可以通过查询表看到该行已被更新。

SELECT * FROM emp WHERE empno = 9999;

     EMPNO ENAME      JOB	       MGR HIREDATE	    SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      9999 WOOD

DELETE Web Services (DELETE)

使用 DELETE 方法删除记录。下面显示了执行此操作所需的 URL 和方法。显示了两种变体。第一种类似于 PUT 方法,将主键值添加到 URL 中。第二个使用查询字符串来定位要删除的行。如果您指定其他标头或有效负载信息,则 Web 服务调用可能会失败。

URL        : http://localhost:8080/ords/hr/employees/9999
Method     : DELETE

URL        : http://localhost:8080/ords/hr/employees/?q={"empno":9999}
Method     : DELETE

下面的“curl”命令将从 EMP 表中删除一行。该 URL 是上述 URL 的编码版本。

$ curl -i -X DELETE  http://localhost:8080/ords/hr/employees/?q=%7B%22empno%22%3A9999%7D
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 25 Jun 2016 17:15:26 GMT

{"rowsDeleted":1}
$

除了 Web 服务输出,我们可以通过查询表看到该行已被删除。

SELECT * FROM emp WHERE empno = 9999;


批量加载

除了基本的 DML 和查询之外,还可以使用 AutoREST 上传批量数据。执行此操作所需的 URL、方法、标头和有效负载如下所示。请注意,有效负载是 CSV 数据。

URL        : http://localhost:8080/ords/hr/employees/batchload?dateFormat="DD-MON-YYYY"
Method     : POST
Header     : Content-Type text/csv
Raw Payload:
empno,ename,job,mgr,hiredate,sal,comm,deptno
9990,JONES,CLERK,,24-JUN-2016,1000,,20
9991,SMITH,CLERK,,24-JUN-2016,1000,,20
9992,DAVIS,CLERK,,24-JUN-2016,1000,,20
9993,BROWN,CLERK,,24-JUN-2016,1000,,20
9994,CLARK,CLERK,,24-JUN-2016,1000,,20

如果有效负载放置在名为“/tmp/data.csv”的文件中,则以下“curl”命令将批量加载到 EMP 表中。

$ curl -i -X POST --data-binary @/tmp/data.csv -H "Content-Type: text/csv" http://localhost:8080/ords/hr/employees/batchload?dateFormat="DD-MON-YYYY"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Sat, 25 Jun 2016 17:21:27 GMT

#INFO Number of rows processed: 5
#INFO Number of rows in error: 0
0 - SUCCESS: Load processed without errors
$

除了 Web 服务输出之外,我们还可以通过查询表看到行已加载。

SELECT * FROM emp WHERE empno > 9000;

     EMPNO ENAME      JOB	       MGR HIREDATE	    SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      9990 JONES      CLERK		   24-JUN-16	   1000 		   20
      9991 SMITH      CLERK		   24-JUN-16	   1000 		   20
      9992 DAVIS      CLERK		   24-JUN-16	   1000 		   20
      9993 BROWN      CLERK		   24-JUN-16	   1000 		   20
      9994 CLARK      CLERK		   24-JUN-16	   1000 		   20

SQL>

显示启用的对象

USER_ORDS_ENABLED_OBJECTS视图显示启用的对象。

SET LINESIZE 200
COLUMN parsing_schema FORMAT A20
COLUMN parsing_object FORMAT A20
COLUMN object_alias FORMAT A20
COLUMN type FORMAT A20
COLUMN status FORMAT A10

SELECT parsing_schema,
       parsing_object,
       object_alias,
       type,
       status
FROM   user_ords_enabled_objects
ORDER BY 1, 2;
 类似资料: