In order to access files that are in Cloud Object Storage, you need to provide the path of the file in the form of a CloudObjectStorage
instance. Add following lines, after the file credentials, to your code:
var soc = new CloudObjectStorage(sc, credentials, null, "bluemix_cos")
var myFile = sc.textFile(soc.url("your_bucket","data.txt"))
In the second code line above, "your_bucket" is the value of "BUCKET" in the file credentials.
There is one last thing we need to do before we are good to go. The CloudObjectStorage
constructor accepts credentials in a way that is for the CSV files. Luckily the main difference between the two formats is the HashMap
key names; hence, we can easily manage this problem by changing the above credentials manually, as mentioned below:
"IBM_API_KEY_ID" to "apiKey"
"IAM_SERVICE_ID" to "serviceId"
"ENDPOINT" to "endPoint"
"IBM_AUTH_ENDPOINT" to "iamServiceEndpoint"
For further details, you can visit the link below:
http://rehmans.org/2018/01/31/how-to-access-a-text-file-from-ibm-dsx-scala-spark-notebook/