Oracle R Enterpriseには、Oracle R Enterpriseプロキシ・オブジェクト、および任意のRオブジェクトをOracle Databaseに保存するために使用できるデータストアが用意されています。1人以上のユーザーについてデータストアへの読取りアクセス権を付与したり、取り消すことができます。保存したオブジェクトを別のRセッションにリストアできます。データストアのオブジェクトは、RインタフェースおよびSQLインタフェースの両方を介して埋込みRの実行からもアクセスできます。
この項では、データストアの作成および管理に使用可能なOracle R Enterpriseの関数について説明します。この項の内容は次のとおりです。
Oracle R Enterpriseのデータストアを使用すると、Rオブジェクトをデータベースに保存できます。
Oracle R Enterpriseプロキシ・オブジェクトを含むRオブジェクトは、明示的に保存しないかぎり現行のRセッションの間存在します。Rオブジェクトの保存およびリストア用の標準のR関数(save
およびload
)は、ファイルに格納するためにRメモリー内のオブジェクトをシリアライズし、メモリーにリストアするためにデシリアライズします。ただし、Oracle R Enterpriseプロキシ・オブジェクトに対しては、それらの関数はプロキシ・オブジェクトに関連付けられているデータベース・オブジェクトをOracle Databaseに保存しないため、保存されたプロキシ・オブジェクトは別のRセッションでは正しく動作しません。
Oracle R Enterpriseプロキシ・オブジェクト、および任意のRオブジェクトは、ore.save
関数を使用して保存できます。ore.save
関数はOracle R Enterpriseデータストアを指定します。データストアは、Rセッションの終了時にデータベースに保持されます。データストアは、自身が含まれているオブジェクトの参照整合性を保持します。ore.load
関数を使用して、データストア内のオブジェクトを別のRセッションにリストアできます。
データストアを使用して、次のことを実行できます。
Oracle R Enterpriseおよび作成したその他のRオブジェクトを1つのRセッションに保存し、別のRセッションにリストアします。
埋込みRの実行で使用するためにR関数に引数を渡します。
埋込みRの実行で使用するためにオブジェクトを渡します。たとえば、OREdm
パッケージ内の関数を使用してOracle Data Miningモデルを構築し、それをデータストアに保存することもできます。その後、埋込みRの実行を介してそのモデルを使用してデータベースにデータをスコアリングできます。埋込みRの実行関数でのデータストアの使用例は、例6-10を参照してください。
次の表にデータストアを操作する関数をリストし、その簡単な説明を示します。
表2-1 データストアを操作する関数
関数 | 説明 |
---|---|
|
現行のOracle Databaseスキーマのデータストアに関する情報をリストします。 |
|
現行のOracle Databaseスキーマの指定したデータストアに関する詳細な情報を提供します。 |
|
現行のOracle Databaseスキーマからデータストアを削除します。 |
ore.grant |
データストアへ読取りアクセス権を付与します。 |
|
データストアからオブジェクトをR環境に遅延リストアします。 |
|
データストアからオブジェクトをR環境にリストアします。 |
ore.revoke |
データストアへの読取りアクセス権を取り消します。 |
|
Rオブジェクトを新規または既存のデータストアに保存します。 |
関連項目:
埋込みRの実行に対するRインタフェースおよびSQLインタフェースの使用の詳細は、「Oracle R Enterpriseの埋込みRの実行の使用方法」を参照してください。
各データベース・スキーマには、指定したOracle R Enterpriseデータストアを格納する表があります。データストアにはOracle R Enterpriseオブジェクトおよび標準のRオブジェクトを含めることができます。
ore.save
関数を使用してデータストアを作成できます。データストアの作成時にそのデータストアの名前を指定します。オブジェクトを1つ以上のデータストアに保存できます。
データストアにデータベース・オブジェクトのOracle R Enterpriseプロキシ・オブジェクトが含まれているかぎり、データベース・オブジェクトはRセッション間で保持されます。たとえば、OREdm
パッケージでore.odmNB
関数を使用してOracle Data MiningのNaive Bayesモデルを構築することもできます。構築されたore.odmNB
オブジェクトをデータストアに保存してRセッションを終了した場合、Oracle DatabaseはそのOracle Data Miningモデルを削除しません。ore.odmNB
オブジェクトが含まれいるデータストアがない場合は、Rセッションを終了すると、データベースは自動的にそのモデルを削除します。
ore.save
関数は、1つ以上のRオブジェクトを指定されたデータストアに保存します。
デフォルトでは、Oracle R Enterpriseは、現行のユーザー・スキーマにデータストアを作成します。ore.save
に対して引数を指定する場合、特定のオブジェクトの名前、またはオブジェクトのリストを指定できます。データストアへの読取りアクセス権を他のユーザーに付与できるかどうかを指定できます。特定のR環境を指定して保存するオブジェクトを検索できます。overwrite
およびappend
の引数は相互に排他的です。overwrite
引数をTRUE
に設定した場合、既存のデータストアを同じ名前の別のデータストアで置換できます。append
引数をTRUE
に設定した場合、既存のデータストアにオブジェクトを追加できます。description
引数を使用すると、データストアの情報取得時に表示される説明テキストを指定できます。description
引数は、append
引数とともに使用すると機能しません。
例2-18 オブジェクトの保存およびデータストアの作成
この例では、引数の様々な組合せを使用してデータストアを作成する方法を示します。
# Create some R objects. df1 <- data.frame(x1 = 1:5, y1 = letters[1:5]) df2 <- data.frame(x2 = 5:1, y2 = letters[11:15]) iris_of <- ore.push(iris) # Create a database table and an Oracle R Enterprise proxy object for the table. ore.drop("AIRQUALITY") ore.create(airquality, table = "AIRQUALITY") # List the R objects. ls() # List the Oracle R Enterprise proxy objects. ore.ls() # Save the proxy object and all objects in the current workspace environment # to the datastore named ds1 and supply a description. ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore") # Create some more objects. x <- stats::runif(20) # x is an object of type numeric. y <- list(a = 1, b = TRUE, c = "hoopsa") z <- ore.push(x) # z is an object of type ore.numeric. # Create another datastore. ore.save(x, y, name = "ds2", description = "x and y") # Overwrite the contents of datastore ds2. ore.save(x, name = "ds2", overwrite = TRUE, description = "only x") # Append object z to datastore ds2. ore.save(z, name = "ds2", append = TRUE)
この例のリスト
R> # Create some R objects. R> df1 <- data.frame(x1 = 1:5, y1 = letters[1:5]) R> df2 <- data.frame(x2 = 5:1, y2 = letters[11:15]) R> iris_of <- ore.push(iris) R> R> # Create a database table and an Oracle R Enterprise proxy object for the table. R> ore.drop("AIRQUALITY") R> ore.create(airquality, table = "AIRQUALITY") R> R> # List the R objects. R> ls() [1] "df1" "df2" "iris_of" R> R> # List the Oracle R Enterprise proxy objects. R> ore.ls() [1] "AIRQUALITY" R> R> # Save the proxy object and all objects in the current workspace environment R> # to the datastore named ds1 and supply a description. R> ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My datastore") R> R> # Create some more objects. R> x <- stats::runif(20) # x is an object of type numeric. R> y <- list(a = 1, b = TRUE, c = "hoopsa") R> z <- ore.push(x) # z is an object of type ore.numeric. R> R> # Create another datastore. R> ore.save(x, y, name = "ds2", description = "x and y") R> R> # Overwrite the contents of datastore ds2. R> ore.save(x, name = "ds2", overwrite = TRUE, description = "only x") R> R> # Append object z to datastore ds2. R> ore.save(z, name = "ds2", append = TRUE)
ore.grant
およびore.revoke
関数を使用すると、Oracle R Enterpriseのデータストアへのアクセス権を付与したり、取り消すことができます。
ore.grant
およびore.revoke
関数では、データストアへのアクセスを制御できます。自分が所有するデータストアへの読取りアクセス権を、指定したユーザーに付与したり、アクセス権限を取り消すことができます。関数ore.save
、ore.load
、ore.datastore
およびore.datastoreSummary
には、データストアのアクセス権に関連する引数があります。
注意:
ore.create
を使用して永続データベース表およびそのプロキシore.frame
オブジェクトを作成し、付与可能なデータストアにプロキシore.frame
オブジェクトを保存した後、ore.grant
を使用してデータストアへの読取りアクセス権を付与した場合、アクセス権はore.frame
オブジェクトにのみ適用されます。読取りアクセス権は、永続データベース表には拡張されません。表自体への読取り権限を付与するには、適切なSQLコマンドを実行する必要があります。
例2-19 データストアへのアクセス権の付与および取消し
この例では、ローカルのRセッションからOracleデータベースにairqualityデータセットをプッシュしますが、これは、ore.frame
オブジェクトAIRQUALITY
として、また、同じ名前の一時データベース表として存在します。この例では次に、AIRQUALITY
オブジェクトをデータストアds3
に保存し、そのデータストアへのアクセス権を他のユーザーに付与できるように指定します。type = grantable
を指定して関数ore.datastore
を呼び出し、読取りアクセス権が付与されているすべてのデータストアを表示します。ds3
データストアの読取り権限をSCOTTに付与します。その後、type = grant
を指定してore.datastore
を呼び出し、読取りアクセス権が付与されているデータストアを表示します。SCOTTの読取り権限を取り消し、アクセス権が付与されているデータストアを再度表示します。
AIRQUALITY <- ore.push(airquality) ore.save(AIRQUALITY, name = "ds3", description = "My datastore 3", grantable = TRUE) ore.datastore(type = "grantable") ore.datastore(type = "grant") ore.grant("ds3", type = "datastore", user = "SCOTT") ore.datastore(type = "grant") ore.revoke("ds3", type = "datastore", user = "SCOTT") ore.datastore(type = "grant")
この例のリスト
R> AIRQUALITY <- ore.push(airquality) R> ore.save(AIRQUALITY, name = "ds3", + description = "My datastore 3", grantable = TRUE) R> ore.datastore(type = "grantable") datastore.name object.count size creation.date description 1 ds3 1 1451 2015-11-30 18:48:25 My datastore 3 R> ore.datastore(type = "grant") [1] datastore.name grantee <0 rows> (or 0-length row.names) R> ore.grant("ds3", type = "datastore", user = "SCOTT") R> ore.datastore(type = "grant") datastore.name grantee 1 ds3 SCOTT R> ore.revoke("ds3", type = "datastore", user = "SCOTT") R> ore.datastore(type = "grant") [1] datastore.name grantee <0 rows> (or 0-length row.names)
ore.datastore
関数およびore.datastoreSummary
関数を使用することで、現行のユーザー・スキーマのデータストアに関する情報を取得できます。
ore.datastore
関数を使用して、データストアに関する基本的な情報をリストできます。特定のタイプのデータストアに関する情報を取得するには、オプションの文字列引数type
を使用できます。type
の有効な値は次のとおりです。
現行のセッション・ユーザーが作成したデータストアをリストするuser
。これはデフォルト値です。
現行のセッション・ユーザーが他のユーザーに読取りアクセス権を付与できないデータストアをリストするprivate
。
現行のセッション・ユーザーが読取りアクセス権を持っているすべてのデータストアをリストするall
。
現行のセッション・ユーザーが他のユーザーに読取り権限を付与できるデータストアをリストするgrantable
。
現行のセッション・ユーザーが他のユーザーに読取り権限を付与したデータストアをリストするgrant
。
他のユーザーによって現行のセッション・ユーザーに読取り権限が付与されたデータストアをリストするgranted
。
タイプを指定しなかった場合、関数ore.datastore
は、データストア名、データストア内のオブジェクト数、データストアのサイズ、作成日および説明に対応する列を含むdata.frame
オブジェクトを返します。行は、列datastore.name
によってアルファベット順にソートされます。タイプを指定した場合、この関数は、指定したタイプの列を含むdata.frame
を返します。
名前または正規表現パターンを使用してデータストアを検索できます。
ore.datastoreSummary
関数は、接続されているデータベースのユーザー・スキーマにあるデータストアに保存されたRオブジェクトに関する情報を返します。この関数は、data.frame
を返します。オブジェクト名、オブジェクト・クラス、オブジェクトのサイズおよびオブジェクトの長さ(vector
の場合)または行および列の数(data.frame
オブジェクトの場合)に対応する列を含みます。これは、1つの必須の引数としてデータストアの名前を取りますが、オプションの引数としてデータストアの所有者を取ることもできます。
例2-20 ore.datastore関数の使用方法
この例では、ore.datastore
関数の使用方法を示します。
# Create some R objects.df1 <- data.frame(x1 = 1:5, y1 = letters[1:5]) df2 <- data.frame(x2 = 5:1, y2 = letters[11:15]) iris_of <- ore.push(iris) # Create a database table and an Oracle R Enterprise proxy object for the table. ore.drop("AIRQUALITY") ore.create(airquality, table = "AIRQUALITY") # Save the objects to a datastore named ds1 and supply a description. ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore") # Create some more objects. x <- stats::runif(20) # x is an object of type numeric. y <- list(a = 1, b = TRUE, c = "hoopsa") z <- ore.push(x) # z is an object of type ore.numeric. # Create other datastores. ore.save(x, y, name = "ds2", description = "x and y") ore.save(df1, df2, name = "dfobj", description = "df objects") ore.save(x, y, z, name = "another_ds", description = "For pattern matching") # List all of the datastore objects. ore.datastore() # List the specified datastore. ore.datastore("ds1") # List the datastore objects with names that include "ds". ore.datastore(pattern = "ds")
この例のリスト
R> # Create some R objects. R> df1 <- data.frame(x1 = 1:5, y1 = letters[1:5]) R> df2 <- data.frame(x2 = 5:1, y2 = letters[11:15]) R> iris_of <- ore.push(iris) R> R> # Create a database table and an Oracle R Enterprise proxy object for the table. R> ore.drop("AIRQUALITY") R> ore.create(airquality, table = "AIRQUALITY") R> R> # Save the objects to a datastore named ds1 and supply a description. R> ore.save(AIRQUALITY, list = ls(), name = "ds1", description = "My private datastore") R> R> # Create some more objects. R> x <- stats::runif(20) # x is an object of type numeric. R> y <- list(a = 1, b = TRUE, c = "hoopsa") R> z <- ore.push(x) # z is an object of type ore.numeric. R> R> # Create other datastores. R> ore.save(x, y, name = "ds2", description = "x and y") R> ore.save(df1, df2, name = "dfobj", description = "df objects") R> ore.save(x, y, z, name = "another_ds", description = "For pattern matching") R> R> # List all of the datastore objects. R> ore.datastore() datastore.name object.count size creation.date description 1 another_ds 3 1284 2017-04-21 16:08:57 For pattern matching 2 dfobj 2 656 2017-04-21 16:08:38 df objects 3 ds1 4 3439 2017-04-21 16:03:55 My private datastore 4 ds2 2 314 2017-04-21 16:04:32 x and y R> # List the specified datastore. R> ore.datastore("ds1") datastore.name object.count size creation.date description 1 ds1 4 3439 2017-04-21 16:03:55 My private datastore R> # List the datastore objects with names that include "ds". R> ore.datastore(pattern = "ds") datastore.name object.count size creation.date description 1 another_ds 3 1284 2017-04-21 16:08:57 For pattern matching 2 ds1 4 3439 2017-04-21 16:03:55 My private datastore 3 ds2 2 314 2017-04-21 16:04:32 x and y
例2-21 ore.datastoreSummary関数の使用方法
この例では、ore.datastoreSummary
関数の使用方法を示します。この例では、前の例で作成したデータストアを使用します。
ore.datastoreSummary("ds1") ore.datastoreSummary("ds2")
この例のリスト
R> ore.datastoreSummary("ds1") object.name class size length row.count col.count 1 AIRQUALITY ore.frame 1213 6 NA 6 2 df1 data.frame 328 2 5 2 3 df2 data.frame 328 2 5 2 4 iris_of ore.frame 1570 5 NA 5 R> ore.datastoreSummary("ds2") object.name class size length row.count col.count 1 x numeric 182 20 NA NA 2 y list 132 3 NA NA
ore.load
関数は、データストアに保存したRオブジェクトをRグローバル環境(.GlobalEnv
)にリストアします。この関数は、リストアされるオブジェクトの名前を含む文字列ベクターを返します。
保存されたすべてのオブジェクトをロードするか、list
引数を使用してロードするオブジェクトを指定できます。envir
引数を使用すると、オブジェクトをロードする環境を指定できます。
例2-22 データストアからオブジェクトをリストアするためのore.load関数の使用方法
この例では、ore.load
関数を使用して、例2-20で作成されたデータストアからオブジェクトをリストアする方法を示します。この例は、その例と同じRセッションで実行されます。
# List the R objects. ls() # List the datastores. ore.datastore() # Delete the x and z objects. rm(x, z) ls() # Restore all of the objects in datastore ds2. ore.load("ds2") ls() # After ending the R session and starting another session. ls() # The datastore objects persist between sessions. ore.datastore() # Restore some of the objects from datastore ds1. ore.load("ds1", list = c("df1", "df2", "iris_of")) ls()例2-22のリスト
R> # List the R objects. R> ls() [1] "df1" "df2" "iris_of" "x" "y" "z" R> R> # List the datastores. R> ore.datastore() datastore.name object.count size creation.date description 1 another_ds 3 1243 2014-07-24 13:31:56 For pattern mattching 2 dfobj 2 656 2014-07-24 13:31:46 df objects 3 ds1 4 3162 2014-07-24 13:25:17 My datastore 4 ds2 2 1111 2014-07-24 13:27:26 only x R> R> # Delete the x and z objects. R> rm(x, z) R> ls() [1] "df1" "df2" "iris_of" "y" R> R> # Restore all of the objects in datastore ds2. R> ore.load("ds2") [1] "x" "z" R> R> ls() [1] "df1" "df2" "iris_of" "x" "y" "z" R> R> # After ending the R session and starting another session. R> ls() character(0) R> # The datastore objects persist between sessions. R> ore.datastore() datastore.name object.count size creation.date description 1 another_ds 3 1243 2014-07-24 13:31:56 For pattern mattching 2 dfobj 2 656 2014-07-24 13:31:46 df objects 3 ds1 4 3162 2014-07-24 13:25:17 My datastore 4 ds2 2 1111 2014-07-24 13:27:26 only x R> # Restore some of the objects from datastore ds1. R> ore.load("ds1", list = c("df1", "df2", "iris_of")) [1] "df1" "df2" "iris_of" R> ls() [1] "df1" "df2" "iris_of"
ore.delete
関数を使用すると、Oracle R Enterpriseデータストアのオブジェクトを削除、またはデータストア自身を削除できます。データストアを削除するには、名前を指定します。データストアから1つ以上のオブジェクトを削除するには、list
引数を指定します。ore.delete
関数は、削除したオブジェクトまたはデータストアの名前を返します。
データストアを削除すると、Oracle R Enterpriseは、削除したデータストアでRオブジェクトによって参照されていたすべての一時データベース・オブジェクトを破棄します。Rオブジェクトを1つ以上のデータストアに保存していた場合、Oracle R Enterpriseは、一時データベース・オブジェクトを参照しているオブジェクトがないときにのみ、一時データベース・オブジェクトを破棄します。
例2-23 ore.delete関数の使用方法
この例では、ore.delete
を使用して、データストアからオブジェクトを削除した後、データストア全体を削除する方法を示します。この例では、例2-18で作成したオブジェクトを使用します。
# Delete the df2 object from the ds1 datastore. ore.delete("ds1", list = "df2") # Delete the datastore named ds1. ore.delete("ds1")例2-23のリスト
R> # Delete the df2 object from the ds1 datastore. R> ore.delete("ds1", list = "df2")[1] "df2" R> # Delete the datastore named ds1. R> ore.delete("ds1") [1] "ds1"