Skip to content

Commit 2ddcfd4

Browse files
committed
Add CloudObjectStorage R class for COS support
1 parent 677b840 commit 2ddcfd4

File tree

1 file changed

+78
-23
lines changed

1 file changed

+78
-23
lines changed

r/sparkr/R/osconfig.R

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ swifturl = function(name, container_name, object_name){
99

1010

1111
#' sparkcontext is a SparkContext object.
12-
#'
12+
#'
1313
#' name is a string that identifies this configuration. You can
1414
#' use any string you like. This allows you to create
1515
#' multiple configurations to different Object Storage accounts.
1616
#' auth_url, username and password are string credentials for your
1717
#' Softlayer Object Store
18-
#' @export softlayer
18+
#' @export softlayer
1919
#' @exportClass softlayer
2020

2121
softlayer <- setRefClass("softlayer",
2222
fields=list(name="character", container_name="character", object_name="character",
23-
sparkcontext='jobj', auth_url="character",
23+
sparkcontext='jobj', auth_url="character",
2424
tenant = "character", username="character", password="character"),
25-
methods=list(initialize =
25+
methods=list(initialize =
2626
function( sparkcontext, name, auth_url, tenant, username, password,public=FALSE,
27-
swift2d_driver='com.ibm.stocator.fs.ObjectStoreFileSystem'){
27+
swift2d_driver='com.ibm.stocator.fs.ObjectStoreFileSystem'){
2828

29-
.self$name = name
29+
.self$name = name
3030
prefix = paste("fs.swift2d.service" , name, sep =".")
3131
hConf = SparkR:::callJMethod(sparkcontext, "hadoopConfiguration")
3232
SparkR:::callJMethod(hConf, "set", "fs.swift2d.impl", swift2d_driver)
@@ -41,21 +41,21 @@ softlayer <- setRefClass("softlayer",
4141
SparkR:::callJMethod(hConf, "set", paste(prefix, "use.get.auth", sep='.'), "true")
4242
invisible(SparkR:::callJMethod(hConf, "setBoolean", paste(prefix, "location-aware", sep='.'), FALSE))
4343
SparkR:::callJMethod(hConf, "set", paste(prefix, "password", sep='.'), password)
44-
45-
44+
45+
4646
},
47-
47+
4848
url = function(container_name, object_name){
4949
return(swifturl(name, container_name, object_name))}
5050
)
5151
)
5252

5353

54-
54+
5555
#' sparkcontext: a SparkContext object.
5656
#'
5757
#' credentials: a dictionary with the following required keys:
58-
#'
58+
#'
5959
#' auth_url
6060
#' project_id (or projectId)
6161
#' user_id (or userId)
@@ -73,28 +73,28 @@ softlayer <- setRefClass("softlayer",
7373
#' instances, the values for these credentials can be obtained
7474
#' by clicking on the 'insert to code' link just below a data
7575
#' source.
76-
#' @export bluemix
76+
#' @export bluemix
7777
#' @exportClass bluemix
7878

79-
79+
8080
bluemix <- setRefClass("bluemix",
81-
fields=list(name="character", credentials = "list",
81+
fields=list(name="character", credentials = "list",
8282
sparkcontext='jobj', public = "character"),
83-
methods=list(initialize =
83+
methods=list(initialize =
8484
function(..., sparkcontext, name=NULL, credentials,
85-
public=FALSE,swift2d_driver='com.ibm.stocator.fs.ObjectStoreFileSystem'){
85+
public=FALSE,swift2d_driver='com.ibm.stocator.fs.ObjectStoreFileSystem'){
8686

8787
callSuper(...,credentials=credentials)
88-
88+
8989
if ( is.null(name)) name <<- credentials["name"][[1]]
90-
90+
9191
user_id = try( credentials['user_id'][[1]])
9292
if(class(user_id)=="try-error") user_id = credentials['userId'][[1]]
93-
93+
9494
tenant = try( credentials['project_id'][[1]])
9595
if(class(tenant)=="try-error") tenant = credentials['projectId'][[1]]
96-
97-
.self$name = name
96+
97+
.self$name = name
9898
prefix = paste("fs.swift2d.service" , name, sep =".")
9999
hConf = SparkR:::callJMethod(sparkcontext, "hadoopConfiguration")
100100
SparkR:::callJMethod(hConf, "set", "fs.swift2d.impl", swift2d_driver)
@@ -108,8 +108,63 @@ bluemix <- setRefClass("bluemix",
108108
invisible(SparkR:::callJMethod(hConf, "setBoolean", paste(prefix, "public", sep='.'), public))
109109
#invisible(SparkR:::callJMethod(hConf, "setInt", paste(prefix, "http.port", sep='.'), 8080))
110110
},
111-
111+
112112
url = function( container_name, object_name){
113113
return(swifturl(name, container_name, object_name))}
114114
)
115-
)
115+
)
116+
117+
#' CloudObjectStorage is a class that is designed for IBM cloud object storage (COS)
118+
#' It sets up the hadoop config for COS and provide the final file url.
119+
#'
120+
#' sparkContext: a SparkContext object.
121+
#''
122+
#' credentials: a dictionary with the following required keys:
123+
#' endpoint
124+
#' accessKey
125+
#' secretKey
126+
#'
127+
#' configurationName: string identifies the configurations that has been
128+
#' set.
129+
#' When using this from a IBM Spark service instance that
130+
#' is configured to connect to particular Bluemix object store
131+
#' instances, the values for these credentials can be obtained
132+
#' by clicking on the 'insert to code' link just below a data
133+
#' source.
134+
#' @export bluemix
135+
#' @exportClass bluemix
136+
CloudObjectStorage <- setRefClass("CloudObjectStorage",
137+
fields=list(configName="character"),
138+
methods=list(
139+
initialize = function(..., sparkContext, credentials, configurationName){
140+
141+
142+
if (is.null(credentials["endpoint"][[1]])) {
143+
stop("Attribute endpoint in credentials is missing!")
144+
}
145+
146+
if (is.null(credentials["accessKey"][[1]])) {
147+
stop("Attribute accessKey in credentials is missing!")
148+
}
149+
150+
if (is.null(credentials["secretKey"][[1]])) {
151+
stop("Attribute secretKey in credentials is missing!")
152+
}
153+
154+
.self$configName = configurationName
155+
prefix = "fs.s3d.service"
156+
hConf = SparkR:::callJMethod(sparkContext, "hadoopConfiguration")
157+
SparkR:::callJMethod(hConf, "set", paste(prefix, "endpoint", sep='.'), credentials['endpoint'][[1]])
158+
SparkR:::callJMethod(hConf, "set", paste(prefix, "access.key", sep='.'), credentials['accessKey'][[1]])
159+
SparkR:::callJMethod(hConf, "set", paste(prefix, "secret.key", sep='.'), credentials['secretKey'][[1]])
160+
},
161+
162+
getConfigName = function() {
163+
return (.self$configName)
164+
},
165+
166+
url = function(bucketName, objectName){
167+
return(paste("s3d://", bucketName, ".service/", objectName, sep = ""))
168+
}
169+
)
170+
)

0 commit comments

Comments
 (0)