7.4 例
この項では、いくつかの例を示します。
表へのクラウド・ストレージ・ファイルのロード
次の例は、クラウド・ストレージ・ファイルをデータベース表にロードする方法を示しています。
ユースケース1: DBMS_CLOUD認証方式を使用している場合、DBMS_CLOUD資格証明が設定され、転送方法がdbms_cloudに設定されます。
--Set the DBMS_CLOUD credential
SQL> cs dbc swiftcred
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.example.com/n/test/b/example
DBMS_CLOUD Credential: SWIFTCRED
OCI Profile: Not Set
Transfer Method: dbms_cloud
URI as specified: https://objectstorage.us-ashburn-1.example.com/n/test/b/example
ユース・ケース2: OCIプロファイル認証方式を使用している場合、OCIプロファイルが設定され、転送方法がociに設定されます。
--Set the OCI profile
SQL> cs oci demo
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.example.com/n/test/b/example
DBMS_CLOUD Credential: Not Set
OCI Profile: Demo
Transfer Method: oci
URI as specified: https://objectstorage.us-ashburn-1.example.com/n/test/b/example
--Create Table "locations"
SQL> create table locations(location_id NUMBER(5),location_name VARCHAR2(40));
Table LOCATIONS created
--Load data from LOCATIONS_DATA_TABLE.csv in cloud storage into "locations" table of the schema for the current user
SQL> load locations cs /o/LOCATIONS_DATA_TABLE.csv
format csv
column_names on
delimiter ,
enclosure_left "
enclosure_right "
encoding UTF8
row_limit off
row_terminator default
skip_rows 0
skip_after_names
--Number of rows processed: 50
--Number of rows in error: 0
--Last row processed in final committed batch: 50
0 - SUCCESS: Load processed without errors
SQL> select count(*) from locations;
COUNT(*)
–--------
50
--Load data from LOCATIONS_DATA_TABLE.csv in your current local folder into "locations" table
of the schema for the current user
SQL> load locations cs LOCATIONS_DATA_TABLE.csv
クラウド・ストレージへの表データのアンロード
次の例は、locations表からクラウド・ストレージにデータをアンロードする方法を示しています。データはクラウド・ストレージの場所にアンロードされ、LOCATIONS_DATA_TABLE.csvという名前が付けられます。
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example
–-Specify delimiter as | and terminator as <eol>
SQL> set loadformat delimiter | term <eol> csv
--Unload locations table into cloud storage
SQL> unload locations cs
DBMS_CLOUD Credential: SWIFTCRED --Using DBMS_CLOUD authentication
OCI Profile: Not Set
Transfer Method: dbms_cloud
URI as specified: https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/
Final URI: https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/
format csv
column_names on
delimiter |
enclosure_left "
enclosure_right "
encoding UTF8
row_terminator <eof>
** UNLOAD Start ** at 2020.09.10-17.08.34
Export Separate Files to https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example
DATA TABLE LOCATIONS
File Name: LOCATIONS_DATA_TABLE.csv
Number of Rows Exported: 23
** UNLOAD End ** at 2020.09.10-17.08.39
-- Show file contents
SQL> cs peek /o/LOCATIONS_DATA_TABLE.csv
"LOCATION_ID"|"LOCATION_NAME"<eof>01|"California"<eof>02|"Alaska<eof>03|"Florida"<eof>04|"Texas"<eof>05|"Virginia"<eof>
06|"Louisiana"<eof>07|"Massachusetts"<eof>08|"Arkansas"<eof>09|"Oregon"<eof>10|"Hawaii"<eof>11|"Maryland"<eof>
12|"Pennsylvania"<eof>13|"Colorado<eof>14|"Michigan"<eof>15|"Minnesota"<eof>16|"Ohio"<eof>17|"Maine"<eof>18|"Arizona"<eof>
19|"Missouri"<eof>20|"Vermont"<eof>21|"Kansas"<eof>22|"Nevada"<eof>23|"Illinois"
コンパートメント、バケットおよびオブジェクトのリスト
ノート:
DBMS_CLOUD資格証明を使用する場合、バケット内のオブジェクトのみをリストできます。DBMS_CLOUDの転送方法は、コンパートメントおよびバケットのリストをサポートしていません。
cs https://objectstorage.us-ashburn-1.oraclecloud.com/n/test
--Lists the compartments in the namespace
SQL> cs listc
COMP_APEX
dbtst
old
ords
pdf-fn-compartment
sandbox
sqldev
--Lists the compartments in the compartment sqldev
SQL> cs listc sqldev
sqldev1
--Lists the buckets in the compartment sqldev
SQL> cs listb sqldev
example
test
--Lists the objects in the bucket transfer
SQL> cs listo /b/example
COUNTRIES_DATA_TABLE.csv
DEPARTMENTS_DATA_TABLE.csv
REGIONS_DATA_TABLE.csv
LOCATIONS_DATA_TABLE.csv
--Displays the first 2000 characters of LOCATIONS_DATA_TABLE file
SQL> cs peek /o/LOCATIONS_DATA_TABLE.csv
PUTコマンドを使用したクラウド・ストレージへのローカル・ファイルのロード
次の例は、CS PUTコマンドおよびCloud Storageコマンドを使用して、ローカル・ファイルをクラウド・ストレージにロードする方法を示しています。
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example
--Put an object from local directory into cloud storage
SQL> cs put C:\\Users\\JDOE\\EMPLOYEES_DATA_TABLE.csv /o/myemployees.csv
Put C:\\Users\\JDOE\\EMPLOYEES_DATA_TABLE.csv copied
to https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/myemployees.csv
--List the objects in the bucket
SQL> cs listo
COUNTRIES_DATA_TABLE.csv
DEPARTMENTS_DATA_TABLE.csv
REGIONS_DATA_TABLE.csv
LOCATIONS_DATA_TABLE.csv
myemployees.csv
GETコマンドを使用したクラウド・ストレージからのファイルの取得
次の例は、CS GETコマンドとCloud Storageコマンドを使用してクラウド・ストレージからファイルを取得する方法を示しています。
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/
--Set the local directory
SQL> cd C:\\Users\JDOE\\TempCS
--Get the myemployees.csv file from cloud storage
SQL> cs get /o/myemployees.csv
Get Object https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/myemployees.csv
copied to C:\Users\JDOE\TempCSemployees.csv
DELETEコマンドを使用したクラウド・ストレージ内のファイルの削除
次の例は、CS DELETEコマンドとCloud Storageコマンドを使用してクラウド・ストレージ内のファイルを削除する方法を示しています。
--Set Cloud Storage URI to the bucket
SQL> cs https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/
--Set the local directory
SQL> cd C:\\Users\JDOE\\TempCS
--Delete the myemployees.csv file in cloud storage
SQL> cs delete /n/test/b/example/o/myemployees.csv
Deleted Object
https://objectstorage.us-ashburn-1.oraclecloud.com/n/test/b/example/o/myemployees.csv
別のユーザーへのDBMS_CLOUD資格証明の付与
次の例は、DBMS_CLOUD資格証明を作成して別のユーザーに付与する方法を示しています。
-- Create a DBMS_CLOUD credential using oci-profile
SQL> dbcc create jdoecred profile freedb
DBMS_CLOUD Credential: JDOECRED
OCI Profile: Not Set
Transfer Method: dbms_cloud
URI as specified: https://objectstorage.us-ashburn-1.example.com/n/test/b/example
-- Grant jdoecred to user jdoe
SQL> grant all on jdoecred to jdoe;
-- Connect to the Cloud instance as jdoe
SQL> connect jdoe/********@adw20200526_high
-- Create a synonym for jdoecred
SQL> create synonym mycred for admin.jdoecred;
-- Set the transfer method using the synonym mycred
SQL> cs dbc mycred
-- User can now list the objects in the Cloud Storage 'test' bucket
because jdoe.mycred exists (as synonym)
SQL> cs listo