Krishna
Krishna

Reputation: 179

Batch insert in OrientDB

I have around 10,000 records(approx) that I want to do batch insert in Orient DB. My problem is some of the record already exists in DB(That I decide on basis of some unique fields). So my batch operation should update the existing record and create the new for the non existing one. It may be possible that for a given batch file same record can occur twice with different value(apart from unique field), Such record should also get updated.

Example:

description firstName   lastName    title           organization    currentStage    inCurrentStageAsOf  serviceLevelMonitored
description1    John    Doe         CEO             organization1   Existing        1012001             Availability
description2    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description3    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description2    Jane    Joe         CIO             organization2   End-of-Life     2022002             Performance
description4    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description5    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description6    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description7    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description8    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description9    Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description10   Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description11   Jane    Doe         CIO             organization2   End-of-Life     2022002             Performance
description2    Jane    Doe                         organization2   End-of-Life     2022002             Performance

For the above operation if my unique fields are description and lastName. Then I have duplicate records in file at Rec2 and Rec13, However Rec4 is not duplicate. Also it is possible that some of the Record already exist in DB.

How am I suppose to apply batch if my file is huge? What I can think now is fire asyn thread to DB to find if Rec exist, then update the Id for the existing Record and separate the whole bunch into two(Record to be updated and to be created) and the apply batch for create and update separately. Just looking for advice if there is some thing which does it at one shot. Thank you.

Upvotes: 0

Views: 517

Answers (1)

Ivan Mainetti
Ivan Mainetti

Reputation: 1982

you can do it using UPDATE UPSERT.

As explained in the manual you need a UNIQUE index on the interested properties, so in your case

CREATE INDEX yourClass.desc_lastName ON yourClass (description,lastName) UNIQUE_HASH_INDEX

then you can update using (e.g. for your first record):

UPDATE yourClass set description="description1",firstName="John",lastName="Doe",title="",organization="organization1",currentStage="Existing" UPSERT WHERE description="description1" AND lastName="Doe"

Upvotes: 3

Related Questions