Multiple SPARQL "INSERT WHERE" queries in a single request
Asked Answered
W

2

8

My question is similar to what was asked on this thread Is it possible to combine those 2 SPARQL INSERT into one?

I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.

An example of it would be,

PREFIX Sensor: <http://example.com/Equipment.owl#> 
{
    INSERT { 
        ?subject1 Sensor:test2 'newValue1' . 
           }
    WHERE {
        ?subject1 Sensor:test1  'testValue1' . 
          }
};
{
    INSERT { 
        ?subject2 Sensor:test2 'newValue2' . 
           }
    WHERE {
        ?subject2 Sensor:test1  'testValue2' . 
          }
};

I know the above query is wrong. I would like to know if something similar is possible in SPARQL.

Warhead answered 9/4, 2012 at 19:55 Comment(0)
S
9

Yes, this is possible. In fact, your example is almost completely fine, just lose the brackets around each insert:

PREFIX Sensor: <http://example.com/Equipment.owl#> 
INSERT { 
    ?subject1 Sensor:test2 'newValue1' . 
}
WHERE {
    ?subject1 Sensor:test1  'testValue1' . 
};
INSERT { 
   ?subject2 Sensor:test2 'newValue2' . 
}
WHERE {
   ?subject2 Sensor:test1  'testValue2' . 
}

is a valid SPARQL update sequence.

Shopper answered 10/4, 2012 at 7:29 Comment(4)
It worked ! Thanks Jeen I ran a bulk update of 100, then 1000 triples using the above query. The updates get really slow as I keep increasing the number of triples. For 100 INSERT WHERE updates it takes 4.2 sec and 1000 INSERT WHERE updates it takes 40.7 sec Is this the correct and most efficient way to do bulk INSERT WHERE updates ?Warhead
I don't think so, I'm guessing it's far more efficient to do that in a single update, rather than a sequence with an update for each individual value. But that's a separate question and requires a bit more detail about what your data looks like and what, exactly, you are trying to achieve :)Shopper
I am trying to bulk insert Sensor readings in a RDF store which is SPARQL 1.1 compliant. I get a stream of readings and the corresponding Sensor ID. I need to lookup the sensor ID get the URI for that sensor and if present insert the sensor measurements. So I need to do a lookup first and then a Insert. Pre-fetching Sensor URIs might not be good because the updates will be from different machines and the number of sensor is supposed to scale to a million and more. please let me know if I am on the correct track.Warhead
@JeenBroekstra In this case, I think that the multiple queries can be combined into one just by using a values block. I've added an answer showing that approach.Rahman
R
3

I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.

You can do this using values to specify the oldValue/newValue pairs that you want, and it only requires a single insert. It also scales more nicely to new pairs, since you only have to add one line to the query.

PREFIX Sensor: <http://example.com/Equipment.owl#> 
INSERT { 
    ?subject Sensor:test2 ?newValue 
}
WHERE {
    values (?oldValue ?newValue) { 
        ('testValue1' 'newValue1')
        ('testValue2' 'newValue2')
    }
    ?subject Sensor:test1 ?oldValue
}
Rahman answered 24/5, 2016 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.