Table.insertOrReplace()

Inserts a document into a table or, if the ID of the document matches the ID of a document already in the table, replaces the document that is in the table.

Operations Performed

Verifies whether a document with the specified ID already exists.
  • If a document with the ID already exists, replaces the existing document.
  • If a document with the ID does not exist, inserts the document.

Example

Original document with ID "movie000001"
{
	"_id" : "movie0000001",
	"title" : "Placeholder doc"
}
Example code
  public Document buildDocument() {
    return MapRDB.newDocument()
        .setId("movie0000001")
        .set("title", "OJAI -- The Documentary")
        .set("studio", "MapR Technologies, Inc.")
        .set("release_date", Values.parseDate("2015-09-29"))
        .set("trailers.teaser", "https://10.10.21.90/trailers/teaser")
        .set("trailers.theatrical", "https://10.10.21.90/trailers/theatrical")
        .setArray("characters", ImmutableList.of("Heroic Developer", "Evil Release Manager", "Mad Development Manager"))
        .set("box_office_gross", 1000000000L);
  }


public void insertOrReplaceDocument(String tablePath, String _id, Document document) {
    try(Table table = MapRDB.getTable(tablePath)) {
      table.insertOrReplace(_id, document);
    }
  }
Parameter Description
_id The value of the document's _id field.
tablePath The path of the table in the MapR file system. See Table Paths.
New document with ID "movie000001"
{
	"_id" : "movie0000001",
	"title" : "OJAI -- The Documentary",
	"studio" : "MapR Technologies, Inc.",
	"release_date" : "2015-09-29",
	"trailers" : {
		"teaser" : "https://10.10.21.90/trailers/teaser",
		"theatrical" : "https://10.10.21.90/trailers/theatrical"
	},
	"characters" : [
		"Heroic Developer", 
           "Evil Release Manager", 
           "Mad Development Manager"
	],
	"box_office_gross" : 1000000000L
}

Permissions Required

  • The readAce and writeAce permissions on the volumes where the JSON tables that contain the documents are located. For information about how to set permissions on volumes, see Setting/Modifying Whole Volume ACEs.
  • readperm and writeperm on the column families in the row.

Link to Javadoc

Table