Objectivity/DB Spark Adapter : Spark Adapter Tutorial : Updating Data
3 
Updating Data
In this topic you will modify the values of attributes on specific customers in the federated database. The Objectivity/DB Spark Adapter makes use of the customer OIDs to ensure that the correct instances are updated in the federated database. Recall that an OID is the unique identifier of an object stored in the federated database.
Each customer that was previously added to the federated database has a long integer attribute called points with a randomly generated value. In this section you will update some of these values.
1. Open src\main\scala\com\thingspan\spark\demo\UpdateCustomerPoints.scala in your preferred editor.
2. Look at the code that reads the data frame.
val customerDF = sqlContext.read.format("com.objy.spark.sql").
  option("objy.bootFilePath", bootFile).
  option("objy.dataClassName", "com.thingspan.spark.demo.Customer").
  option("objy.addOidColumn", "customerOid").
  load()
...
customerDF.registerTempTable("customers")
Note the use of the objy.addOidColumn option, which adds a new temporary column with the OID for each customer. The column name for the new data is customerOid. When you read data from the federated database into a data frame, you need to use addOidColumn if your intention is to update specific objects by OID when you write data back to the federated database.
3. Look at the code that updates the points.
var customers = sqlContext.sql(queryString.toString)
...
customers = customers.withColumn("points", customers("points") + 3)
The queryString will be passed in from the updateCustomerPoints Gradle task (see the build.gradle file). The query identifies the customers of interest—namely, those named Daz Quinn. Each such customer is awarded three additional points.
4. Look at the code that writes the updated data back to the federated database.
customers.write.
  mode(SaveMode.Append).
  format("com.objy.spark.sql").
  option("objy.bootFilePath",  bootFile).
  option("objy.dataClassName", "com.thingspan.spark.demo.Customer").
  option("objy.updateByOid", "customerOid").
  save()
The objy.updateByOid option uses the previously added customerOid column to write the updates back to the correct instances.
5. From the ObjySparkTutorial directory, run the task as follows:
gradlew updateCustomerPoints
Verifying Results
Objectivity/DB provides an administrative tool called the DO runner that lets you perform queries, updates, and data ingest for a federated database. Let’s use the DO runner to verify that the customer points have been updated.
1. Send a DO statement that queries for customers named Daz Quinn and shows their point counts:
objy DO -boot data\customers.boot -outputFormat table -statement "FROM com.thingspan.spark.demo.Customers WHERE firstName == 'Daz' AND lastName == 'Quinn'  RETURN firstName, lastName, points;"
2. (Optional) Run the updateCustomerPoints Gradle task again, followed by the DO query.