Skip to content

Commit 3bb7e26

Browse files
Merge pull request #23 from ibm-watson-data-lab/cos_scheme_support_scala
COS Scheme Support + Multiple Config Support [Scala]
2 parents 7d174ca + a37cd4e commit 3bb7e26

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

scala/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ This library is cross-built on both Scala 2.10 (for Spark 1.6.0) and Scala 2.11
4949

5050
#### IBM Spark Service
5151

52-
The `ibmos2spark` Scala library package is now pre-installed on IBM Apache Spark as a service. This includes
53-
service instances created in Bluemix or in Data Science Experience.
52+
The `ibmos2spark` Scala library package is now pre-installed on IBM Apache Spark as a service. This includes
53+
service instances created in Bluemix or in Data Science Experience.
5454

5555
### Snapshots
5656

@@ -148,7 +148,8 @@ var credentials = scala.collection.mutable.HashMap[String, String](
148148
var bucketName = "myBucket"
149149
var objectname = "mydata.csv"
150150

151-
var cos = new CloudObjectStorage(sc, credentials)
151+
var configurationName = "cos_config_name" // you can choose any string you want
152+
var cos = new CloudObjectStorage(sc, credentials, configurationName)
152153
var spark = SparkSession.
153154
builder().
154155
getOrCreate()

scala/src/main/scala/Osconfig.scala

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package com.ibm.ibmos2spark
33
import scala.collection.mutable.HashMap
44
import org.apache.spark.SparkContext
55

6+
object globalVariables {
7+
val DEFAULT_SERVICE_NAME = "service"
8+
}
9+
610

711
object urlbuilder{
812
def swifturl2d(name: String, container_name: String, object_name: String): String = {
@@ -140,22 +144,12 @@ class bluemix(sc: SparkContext, name: String, creds: HashMap[String, String],
140144
141145
* secretKey
142146
143-
* cosId [optional]: this parameter is the cloud object storage unique id. It is useful
144-
to keep in the class instance for further checks after the initialization. However,
145-
it is not mandatory for the class instance to work. This value can be retrieved by
146-
calling the getCosId function.
147-
148-
bucket_name (projectId in DSX) [optional]: string that identifies the defult
149-
bucket nameyou want to access files from in the COS service instance.
150-
In DSX, bucket_name is the same as projectId. One bucket is
151-
associated with one project.
152-
If this value is not specified, you need to pass it when
153-
you use the url function.
154-
*
155-
Warning: creating a new instance of this class would overwrite the existing
156-
spark hadoop configs if set before if used with the same spark context instance.
147+
* configurationName [optional]: string that identifies this configuration. You can
148+
use any string you like. This allows you to create
149+
multiple configurations to different Object Storage accounts.
150+
if a configuration name is not passed the default one will be used "service".
157151
*/
158-
class CloudObjectStorage(sc: SparkContext, credentials: HashMap[String, String], cosId: String = "") {
152+
class CloudObjectStorage(sc: SparkContext, credentials: HashMap[String, String], configurationName: String = "") {
159153

160154
// check if all credentials are available
161155
val requiredValues = Array("endPoint", "accessKey", "secretKey")
@@ -167,16 +161,22 @@ class CloudObjectStorage(sc: SparkContext, credentials: HashMap[String, String],
167161

168162
// set config
169163
val hadoopConf = sc.hadoopConfiguration
170-
val prefix = "fs.s3d.service"
164+
val prefix = "fs.cos." + getConfigurationName()
165+
171166
hadoopConf.set(prefix + ".endpoint", credentials("endPoint"))
172167
hadoopConf.set(prefix + ".access.key", credentials("accessKey"))
173168
hadoopConf.set(prefix + ".secret.key", credentials("secretKey"))
174169

175-
def getCosId() : String = {
176-
return cosId
170+
private def getConfigurationName() : String = {
171+
if (configurationName != "") {
172+
return configurationName
173+
} else {
174+
return globalVariables.DEFAULT_SERVICE_NAME
175+
}
177176
}
178177

179178
def url(bucketName: String, objectName: String) : String = {
180-
return "s3d://" + bucketName + ".service/" + objectName
179+
var serviceName = getConfigurationName()
180+
return "cos://" + bucketName + "." + serviceName + "/" + objectName
181181
}
182182
}

0 commit comments

Comments
 (0)