次の各項で説明するとおり、Oracle R Enterpriseを使用して、Rセッションにデータベース常駐データからRプロキシ・オブジェクトを作成できます。
そのスキーマ内のデータベース表のデータへのアクセスを取得するには、ore.sync
関数を使用します。
Rセッションでore.connect
を呼び出すと、Oracle R EnterpriseはOracle Databaseインスタンス内のスキーマへの接続を作成します。ore.sync
関数は、スキーマ内の表のプロキシであるore.frame
オブジェクトを作成します。ore.attach
関数を使用して、R検索パスのスキーマを表すR環境を追加できます。
ore.sync
関数を使用して、データベース表のプロキシとしてore.frame
オブジェクトを作成する場合、ore.frame
プロキシ・オブジェクトの名前はデータベース・オブジェクトの名前と同じです。各ore.frame
プロキシ・オブジェクトには、対応するデータベース・オブジェクトに関するメタデータが含まれます。
このプロキシore.frame
オブジェクトを使用して、表からデータを選択できます。表からデータを選択するR操作を実行すると、操作によってデータベース・オブジェクトから現在のデータが返されます。ただし、一部のアプリケーションが表に列を追加したか、データベース・オブジェクトのメタデータを変更した場合、そのデータベース・オブジェクトに対して再びore.sync
が呼び出されるまでore.frame
プロキシ・オブジェクトは変更を反映しません。
表を指定せずにore.sync
関数を呼び出した場合、およびOracle Databaseインスタンスへの接続を確立したore.connect
関数呼出しでall
引数がFALSE
だった場合は、ore.sync
関数がore.connect
に指定されたスキーマ内の各表にプロキシ・オブジェクトを作成します。table
引数を使用してore.frame
プロキシ・オブジェクトを作成する表を指定できます。
ヒント:
メモリー・リソースおよび時間を節約するには、Rセッションで使用する表にプロキシのみを追加してください。
schema
引数を使用して、R環境およびプロキシ・オブジェクトを作成するスキーマを指定できます。指定したデータベース・スキーマに一度に存在できるのは、1つの環境のみです。use.keys
引数を使用すると、ore.frame
オブジェクトを順序付けするために表で主キーを使用するかどうかを指定できます。
ヒント:
順序付けはデータベースに負荷がかかります。Rでのほとんどの操作には順序付けは不要なため、データのサンプリングやその他の目的で必要な場合を除いて、通常はuse.keys
をFALSE
に設定してください。
query
引数を使用すると、SQL SELECT
文を指定できます。これによって、データベース内にビューを作成せずに問合せ用のore.frame
を作成できます。これは、現行スキーマに対してCREATE VIEW
システム権限を持っていない場合に有用です。schema
引数とquery
引数を同じore.sync
呼出しで使用することはできません。
ore.ls
関数を使用して、スキーマの環境内のデータベース表に対応するore.frame
プロキシ・オブジェクトをリストできます。ore.exists
関数を使用して、データベース表のore.frame
プロキシ・オブジェクトがR環境に存在するかどうかを確認できます。プロキシ・オブジェクトが存在する場合はTRUE
が、存在しない場合はFALSE
が関数によって返されます。ore.rm
関数を使用してR環境からore.frame
プロキシ・オブジェクトを削除できます。
次の例では、ore.sync
関数の使用方法を示します。
この例ではまず、ore.exec
関数を呼び出して、rquserのデータベース・スキーマに存在する表を表すいくつかの表を作成します。次に、ore.sync
を呼び出し、スキーマの3つの表を指定します。ore.sync
の呼出しによりrquserにR環境を作成し、そのスキーマ内の指定された表に対するプロキシore.frame
オブジェクトを作成します。この例では、現在の環境にあるore.frame
プロキシ・オブジェクトをリストします。TABLE3表はスキーマ内に存在しますが、ore.frame
プロキシ・オブジェクトは含まれません(このプロキシ・オブジェクトはore.sync
呼出しに含められていないため)。
次に、query
引数を使用してore.sync
を呼び出して、指定されたSQL問合せに対するore.frame
オブジェクトを作成します。再びore.frame
オブジェクトをリストします。
次にore.sync
を再び呼び出し、SHスキーマにR環境を作成し、そのスキーマ内の指定された表に対するプロキシ・オブジェクトをその環境内に作成します。この例では、ore.exists
関数を呼び出して、指定された表が現在の環境に存在するかどうかを確認した後に、SH環境に存在するかどうかを確認します。SH環境内のRオブジェクトをリストします。
次に、ore.frame
オブジェクトのQUERY1
、QUERY2
およびTABLE4
をrquser環境から削除します。最後に、環境内のプロキシ・オブジェクトを再びリストします。
注意:
ore.rm
関数の呼出しにより、TABLE4表のプロキシであるore.frame
が環境から削除されます。スキーマから表は削除されません。
例2-10 ore.frameプロキシ・オブジェクトをR環境に追加するためのore.syncの使用方法
# After connecting to a database as rquser, create some tables. ore.exec("CREATE TABLE TABLE1 AS SELECT * FROM dual") ore.exec("CREATE TABLE TABLE2 AS SELECT * FROM dual") ore.exec("CREATE TABLE TABLE3 AS SELECT * FROM dual") ore.exec("CREATE TABLE TABLE4 AS SELECT * FROM dual") # Create ore.frame objects for the specified tables. ore.sync(table = c("TABLE1", "TABLE3", "TABLE4")) # List the ore.frame proxy objects in the current environment. ore.ls() # Create ore.frame objects for the specified queries. ore.sync(query = c("QUERY1" = "SELECT 0 X, 1 Y FROM dual", "QUERY2" = "SELECT 1 X, 0 Y FROM dual")) ore.ls() # The rquser user has been granted SELECT permission on the tables in the # SH schema. ore.sync("SH", table = c("CUSTOMERS", "SALES")) # Find out if the CUSTOMERS ore.frame exists in the rquser environment. ore.exists("CUSTOMERS") # Find out if it exists in the SH environment. ore.exists("CUSTOMERS", schema = "SH") # List the ore.frame proxy objects in the SH environment. ore.ls("SH") # Remove the ore.frame objects for the specified objects. ore.rm(c("QUERY1", "QUERY2", "TABLE4")) # List the ore.frame proxy objects in the current environment again. ore.ls()この例のリスト
R> # After connecting to a database as rquser, create some tables. R> ore.exec("CREATE TABLE TABLE1 AS SELECT * FROM dual") R> ore.exec("CREATE TABLE TABLE2 AS SELECT * FROM dual") R> ore.exec("CREATE TABLE TABLE3 AS SELECT * FROM dual") R> ore.exec("CREATE TABLE TABLE4 AS SELECT * FROM dual") R> # Create ore.frame objects for the specified tables. R> ore.sync(table = c("TABLE1", "TABLE3", "TABLE4")) R> # List the ore.frame proxy objects in the current environment. R> ore.ls() [1] "TABLE1" "TABLE3" "TABLE4" R> # Create ore.frame objects for the specified queries. R> ore.sync(query = c("QUERY1" = "SELECT 0 X, 1 Y FROM dual", + "QUERY2" = "SELECT 1 X, 0 Y FROM dual")) R> ore.ls() [1] "QUERY1" "QUERY2" "TABLE1" "TABLE3" "TABLE4" R> # The rquser user has been granted SELECT permission on the tables in the R> # SH schema. R> ore.sync("SH", table = c("CUSTOMERS", "SALES")) R> # Find out if the CUSTOMERS ore.frame exists in the rquser environment. R> ore.exists("CUSTOMERS") [1] FALSE R> # Find out if it exists in the SH environment. R> ore.exists("CUSTOMERS", schema = "SH") [1] TRUE R> # List the ore.frame proxy objects in the SH environment. R> ore.ls("SH") [1] "CUSTOMERS" "SALES" R> # Remove the ore.frame objects for the specified objects. R> ore.rm(c("QUERY1", "QUERY2", "TABLE4")) R> # List the ore.frame proxy objects in the current environment again. R> ore.ls() [1] "TABLE1" TABLE3"
ore.sync
を使用してR環境およびore.frame
プロキシ・オブジェクトを作成した後、ore.get
関数を使用し、名前を指定してプロキシ・オブジェクトを取得できます。SH_CUST <- ore.get(name = "CUSTOMERS", schema = "SH")
のようにore.get
を使用して、表のプロキシore.frame
を取得し、それをRで変数に割り当てることができます。ore.frame
はRグローバル環境に存在し、この環境は.GlobalEnv
を使用して参照できるため、ls
関数によって返されるリストに表示されます。また、このオブジェクトはRグローバル環境に存在するため、データベース・スキーマを表すR環境とは対照的に、ore.ls
関数によってリストされません。
例2-11 データベース表を取得するためのore.getの使用方法
この例では、ore.sync
関数を呼び出して、SHスキーマのCUSTOMERS表のプロキシであるore.frame
オブジェクトを作成します。次に、そのプロキシ・オブジェクトのディメンションを取得します。
ore.sync(schema = "SH", table = "CUSTOMERS", use.keys = FALSE) dim(ore.get(name = "CUSTOMERS", schema = "SH"))例2-11のリスト
R> ore.sync(schema = "SH", table = "CUSTOMERS", use.keys = FALSE) R> dim(ore.get(name = "CUSTOMERS", schema = "SH")) [1] 630 15
ore.attach
を使用して、データベース・スキーマのR環境をR検索パスに追加します。R環境を追加すると、スキーマ環境を指定することなくore.sync
関数で作成したプロキシ・オブジェクトを介してデータベース表に名前を指定してアクセスできます。
デフォルト・スキーマは接続作成時に指定したもので、検索パスでのデフォルト位置は2です。スキーマおよび位置はore.attach関数呼出しで指定できます。また、環境の追加時に名前の競合が発生するかどうかをore.attach
関数で示すかどうかを指定できます。ore.detach
関数を使用して、R検索パスからスキーマに対する環境をデタッチできます。
例2-12 データベース・スキーマの環境を追加するためのore.attachの使用方法
この例では、ore.attach
関数の使用方法を示します。例にあるコメントは、関数の呼出しを説明しています。
# Connected as rquser. # Add the environment for the rquser schema to the R search path. ore.attach() # Create an unordered ore.frame proxy object in the SH environment for the # specifed table. ore.sync(schema = "SH", table = "CUSTOMERS", use.keys = FALSE) # Add the environment for the SH schema to the search path and warn if naming # conflicts exist. ore.attach("SH", 3, warn.conflicts = TRUE) # Display the number of rows and columns in the proxy object for the table. dim(CUSTOMERS) # Remove the environment for the SH schema from the search path. ore.detach("SH") # Invoke the dim function again. dim(CUSTOMERS)例2-12のリスト
R> # Connected as rquser. R> # Add the environment for the rquser schema to the R search path. R> ore.attach() R> # Create an unordered ore.frame proxy object in the SH environment for the R> # specifed table. R> ore.sync(schema = "SH", table = "CUSTOMERS", use.keys = FALSE) R> # Add the environment for the SH schema to the search path and warn if naming R> # conflicts exist. R> ore.attach("SH", 3, warn.conflicts = TRUE) R> # Display the number of rows and columns in the proxy object for the table. R> dim(CUSTOMERS) [1] 630 15 R> # Remove the environment for the SH schema from the search path. R> ore.detach("SH") R> # Invoke the dim function again. R> dim(CUSTOMERS) Error: object 'CUSTOMERS' not found