Note Greenplum Database R Client is deprecated and will be removed in a future Greenplum Database release.

This chapter contains the following information:

About Greenplum R

The Greenplum R Client (GreenplumR) is an interactive in-database data analytics tool for Greenplum Database. The client provides an R interface to tables and views, and requires no SQL knowledge to operate on these database objects.

You can use GreenplumR with the Greenplum PL/R procedural language to run an R function on data stored in Greenplum Database. GreenplumR parses the R function and creates a user-defined function (UDF) for execution in Greenplum. Greenplum runs the UDF in parallel on the segment hosts.

You can similarly use GreenplumR with Greenplum PL/Container 3 (Beta), to run an R function against Greenplum data in a high-performance R sandbox runtime environment.

No analytic data is loaded into R when you use GreenplumR, a key requirement when dealing with large data sets. Only the R function and minimal data is transferred between R and Greenplum.

Supported Platforms

GreenplumR supports the following component versions:

GreenplumR VersionR VersionGreenplum VersionPL/R VersionPL/Container Version
1,1,0, 1.0.03.6+6.1+3.0.2+3.0.0 (Beta)

Prerequisites

You can use GreenplumR with Greenplum Database and the PL/R and PL/Container procedural languages. Before you install and run GreenplumR on a client system:

  • Ensure that your Greenplum Database installation is running version 6.1 or newer.

  • Ensure that your client development system has connectivity to the Greenplum Database master host.

  • Ensure that R version 3.6.0 or newer is installed on your client system, and that you set the $R_HOME environment variable appropriately.

  • Determine the procedural language(s) you plan to use with GreenplumR, and ensure that the language(s) is installed and configured in your Greenplum Database cluster. Refer to PL/R Language and PL/Container Language (3.0 Beta) for language installation and configuration instructions.

  • Verify that you have registered the procedural language(s) in each database in which you plan to use GreenplumR to read data from or write data to Greenplum. For example, the following command lists the extensions and languages registered in the database named testdb:

    1. $ psql -h gpmaster -d testdb -c '\dx'
    2. List of installed extensions
    3. Name | Version | Schema | Description
    4. -------------+----------+------------+----------------------------------------------------------------
    5. plcontainer | 1.0.0 | public | GPDB execution sandboxing for Python and R
    6. plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
    7. plr | 8.3.0.16 | public | load R interpreter and run R script from within a database

    Check for the plr and/or plcontainer extension Name.

Installing the Greenplum R Client

GreenplumR is an R package. You obtain the package from VMware Tanzu Network (or the GreenplumR github repository) and install the package within the R console.

  1. Download the package from the Greenplum Procedural Languages filegroup on VMware Tanzu Network. The naming format of the downloaded file is greenplumR‑<version>‑gp6.tar.gz.

    Note the file system location of the downloaded file.

  2. Follow the instructions in Verifying the Greenplum Database Software Download to verify the integrity of the Greenplum Procedural Languages GreenplumR software.

  3. Install the dependent R packages: ini, shiny, and RPostgreSQL. For example, enter the R console and run the following, or equivalent, command:

    1. user@clientsys$ R
    2. > install.packages(c("ini", "shiny", "RPostgreSQL"))

    You may be prompted to select a CRAN download mirror. And, depending on your client system configuration, you may be prompted to use or create a personal library.

    After downloading, R builds and installs these and dependent packages.

  4. Install the GreenplumR R package:

    1. > install.packages("/path/to/greenplumR‑<version>‑gp6.tar.gz", repos = NULL, type = "source")
  5. Install the Rdpack documentation package:

    1. > install.packages("Rdpack")

Example Data Sets

GreenplumR includes the abalone and null.data sample datasets. Refer to the reference pages for more information:

  1. > help( abalone )
  2. > help( null.data )

Using the Greenplum R Client

You use GreenplumR to perform in-database analytics. Typical operations that you may perform include:

  • Loading the GreenplumR package.
  • Connecting to and disconnecting from Greenplum Database.
  • Examining database objects.
  • Analyzing and manipulating data.
  • Running R functions in Greenplum Database.

Loading GreenplumR

Use the R library() function to load GreenplumR:

  1. user@clientsys$ R
  2. > library("GreenplumR")

Connecting to Greenplum Database

The db.connect() and db.connect.dsn() GreenplumR functions establish a connection to Greenplum Database. The db.disconnect() function closes a database connection.

The GreenplumR connect and disconnect function signatures follow:

  1. db.connect( host = "localhost", user = Sys.getenv("USER"),
  2. dbname = user, password = "", port = 5432, conn.pkg = "RPostgreSQL",
  3. default.schemas = NULL, verbose = TRUE, quick = FALSE )
  4. db.connect.dsn( dsn.key, db.ini = "~/db.ini", default.schemas = NULL,
  5. verbose = TRUE, quick = FALSE )
  6. db.disconnect( conn.id = 1, verbose = TRUE, force = FALSE )

When you connect to Greenplum Database, you provide the master host, port, database name, user name, password, and other information via function arguments or a data source name (DSN) file. If you do not specify an argument or value, GreenplumR uses the default.

The db.connect[.dsn]() functions return an integer connection identifier. You specify this identifier when you operate on tables or views in the database. You also specify this identifier when you close the connection.

The db.disconnect() function returns a logical that identifies whether or not the connection was successfully disconnected.

To list and display information about active Greenplum connections, use the db.list() function.

Example:

  1. ## connect to Greenplum database named testdb on host gpmaster
  2. > cid_to_testdb <- db.connect( host = "gpmaster", port=5432, dbname = "testdb" )
  3. Loading required package: DBI
  4. Created a connection to database with ID 1
  5. [1] 1
  6. > db.list()
  7. Database Connection Info
  8. ## -------------------------------
  9. [Connection ID 1]
  10. Host : gpmaster
  11. User : gpadmin
  12. Database : testdb
  13. DBMS : Greenplum 6
  14. > db.disconnect( cid_to_testdb )
  15. Connection 1 is disconnected!
  16. [1] TRUE

Examining Database Obects

The db.objects() function lists the tables and views in the database identified by a specific connection identifier. The function signature is:

  1. db.objects( search = NULL, conn.id = 1 )

If you choose, you can specify a filter string to narrow the returned results. For example, to list the tables and views in the public schema in the database identified by the default connection identifier, invoke the function as follows:

  1. > db.objects( search = "public." )

Analyzing and Manipulating Data

The fundamental data structure of R is the data.frame. A data frame is a collection of variables represented as a list of vectors. GreenplumR operates on db.data.frame objects, and exposes functions to convert to and manipulate objects of this type:

  • as.db.data.frame() - writes data in a file or a data.frame into a Greenplum table. You can also use the function to write the results of a query into a table, or to create a local db.data.frame.
  • db.data.frame() - creates a temporary R object that references a view or table in a Greenplum database. No data is loaded into R when you use this function.

Example:

  1. ## create a db.data.frame from the abalone example data set;
  2. ## abalone is a data.frame
  3. > abdf1 <- as.db.data.frame(abalone, conn.id = cid_to_testdb, verbose = FALSE)
  4. ## sort on the id column and preview the first 5 rows
  5. > lk( sort( abdf1, INDICES=abdf1$id ), 5 )
  6. id sex length diameter height whole shucked viscera shell rings
  7. 1 1 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.150 15
  8. 2 2 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.070 7
  9. 3 3 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.210 9
  10. 4 4 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.155 10
  11. 5 5 I 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.055 7
  12. ## write the data frame to a Greenplum table named abalone_from_r;
  13. ## use most of the function defaults
  14. > as.db.data.frame( abdf1, table.name = "public.abalone_from_r" )
  15. The data contained in table "pg_temp_93"."gp_temp_5bdf4ec7_42f9_9f9799_a0d76231be8f" which is wrapped by abdf1 is c
  16. opied into "public"."abalone_from_r" in database testdb on gpmaster !
  17. Table : "public"."abalone_from_r"
  18. Database : testdb
  19. Host : gpmaster
  20. Connection : 1
  21. ## list database objects, should display the newly created table
  22. > db.objects( search = "public.")
  23. [1] "public.abalone_from_r"

Running R Functions in Greenplum Database

GreenplumR supports two functions that allow you to run an R function, in-database, on every row of a Greenplum Database table: db.gpapply() and db.gptapply(). You can use the Greenplum PL/R or PL/Container procedural language as the vehicle in which to run the function.

The function signatures follow:

  1. db.gpapply( X, MARGIN = NULL, FUN = NULL, output.name = NULL, output.signature = NULL,
  2. clear.existing = FALSE, case.sensitive = FALSE, output.distributeOn = NULL,
  3. debugger.mode = FALSE, runtime.id = "plc_r_shared", language = "plcontainer",
  4. input.signature = NULL, ... )
  5. db.gptapply( X, INDEX, FUN = NULL, output.name = NULL, output.signature = NULL,
  6. clear.existing = FALSE, case.sensitive = FALSE,
  7. output.distributeOn = NULL, debugger.mode = FALSE,
  8. runtime.id = "plc_r_shared", language = "plcontainer",
  9. input.signature = NULL, ... )

Use the second variant of the function when the table data is indexed.

By default, db.gp[t]apply() passes a single data frame input argument to the R function FUN. If you define FUN to take a list of arguments, you must specify the function argument name to Greenplum table column name mapping in input.signature. You must specify this mapping in table column order.

Example 1:

Create a Greenplum table named table1 in the database named testdb. This table has a single integer-type field. Populate the table with some data:

  1. user@clientsys$ psql -h gpmaster -d testdb
  2. testdb=# CREATE TABLE table1( id int );
  3. testdb=# INSERT INTO table1 SELECT generate_series(1,13);
  4. testdb=# \q

Create an R function that increments an integer. Run the function on table1 in Greenplum using the PL/R procedural language. Then write the new values to a table named table1_r_inc:

  1. user@clientsys$ R
  2. > ## create a reference to table1
  3. > t1 <- db.data.frame("public.table1")
  4. > ## create an R function that increment an integer by 1
  5. > fn.function_plus_one <- function(num)
  6. {
  7. return (num[[1]] + 1)
  8. }
  9. > ## create the function output signature
  10. > .sig <- list( "num" = "int" )
  11. > ## run the function in Greenplum and print
  12. > x <- db.gpapply( t1, output.name = NULL, FUN = fn.function_plus_one,
  13. output.signature = .sig, clear.existing = TRUE, case.sensitive = TRUE, language = "plr" )
  14. > print(x)
  15. num
  16. 1 2
  17. 2 6
  18. 3 12
  19. 4 13
  20. 5 3
  21. 6 4
  22. 7 5
  23. 8 7
  24. 9 8
  25. 10 9
  26. 11 10
  27. 12 11
  28. 13 14
  29. > ## run the function in Greenplum and write to the output table
  30. > db.gpapply(t1, output.name = "public.table1_r_inc", FUN = fn.function_plus_one,
  31. output.signature = .sig, clear.existing = TRUE, case.sensitive = TRUE,
  32. language = "plr" )
  33. ## list database objects, should display the newly created table
  34. > db.objects( search = "public.")
  35. [1] "public.abalone_from_r" "public.table1_r_inc"

Example 2:

Create a Greenplum table named table2 in the database named testdb. This table has two integer-type fields. Populate the table with some data:

  1. user@clientsys$ psql -h gpmaster -d testdb
  2. testdb=# CREATE TABLE table2( c1 int, c2 int );
  3. testdb=# INSERT INTO table2 VALUES (1, 2);
  4. testdb=#\q

Create an R function that takes two integer arguments, manipulates the arguments, and returns both. Run the function on the data in table2 in Greenplum using the PL/R procedural language, writing the new values to a table named table2_r_upd:

  1. user@clientsys$ R
  2. > ## create a reference to table2
  3. > t2 <- db.data.frame("public.table2")
  4. > ## create the R function
  5. > fn.func_with_two_args <- function(a, b)
  6. {
  7. a <- a * 20
  8. b <- a + 66
  9. c <- list(a, b)
  10. return (as.data.frame(c))
  11. }
  12. > ## create the function input signature, mapping function argument name to
  13. > ## table column name
  14. > input.sig <- list('a' = 'c1', 'b' = 'c2')
  15. > ## create the function output signature
  16. > return.sig <- list('a' = 'int', 'b' = 'int')
  17. > ## run the function in Greenplum and write to the output table
  18. > db.gpapply(t2, output.name = "public.table2_r_upd", FUN = fn.func_with_two_args,
  19. output.signature = return.sig, clear.existing = TRUE, case.sensitive = TRUE,
  20. language = "plr", input.signature = input.sig )

View the contents of the Greenplum table named table2_r_upd:

  1. user@clientsys$ psql -h gpmaster -d testdb
  2. testdb=# SELECT * FROM table2_r_upd;
  3. a | b
  4. ----+----
  5. 20 | 86
  6. (1 row)
  7. testdb=# \q

Limitations

GreenplumR has the following limitations:

  • The db.gpapply() and db.gptapply() functions currently operate only on conn.id = 1.
  • The GreenplumR function reference pages are not yet published. You can find html versions of the reference pages in your R library R/<platform>-library/3.6/GreenplumR/html directory. Open the 00Index.html file in the browser of your choice and select the function of interest.
  • The GreenplumR() graphical user interface is currently unsupported.

Function Summary

GreenplumR provides several functions. To obtain reference information for GreenplumR functions while running the R console, invoke the help() function. For example, to display the reference information for the GreenplumR db.data.frame() function:

  1. > help( db.data.frame )

GreenplumR functions include:

Table 2. GreenplumR Functions
CategoryNameDescription
Aggregate Functionsmean(), sum(), count(), max(), min(), sd(), var(), colMeans(), colSums(), colAgg(), db.array()Functions that perform a calculation on multiple values and return a single value.
Connectivity Functionsconnection infoExtract connection information.
db.connect()Create a database connection.
db.connect.dsn()Create a database connection using a DSN.
db.disconnect()Disconnect a database connection.
db.list()List database connections.
Database Object Functionsas.db.data.frame()Create a db.data.frame from a file or data.frame, optionally writing the data to a Greenplum table.
db.data.frame()Create a data frame that references a view or table in the database.
db.objects()List the table and view objects in the database.
db.existsObject()Identifies whether a table or view exists in the database.
Mathematical Functionsexp(), abs(), log(), log10(), sign(), sqrt(), factorial(), sin(), cos(), tan(), asin(), acos(), atan(), atan2()Mathematical functions that take db.obj as an argument.
Greenplum Functionsdb.gpapply()Wrap an R function with a UDF and run it on every row of data in a Greenplum table.
db.gptapply()Wrap an R function with a UDF and run it on every row of data grouped by an index in a Greenplum table.