DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. rdfQuery-Process RDF Data in your browser

rdfQuery-Process RDF Data in your browser

Shiv Kumar Ganesh user avatar by
Shiv Kumar Ganesh
·
Nov. 13, 12 · Interview
Like (0)
Save
Tweet
Share
3.31K Views

Join the DZone community and get the full member experience.

Join For Free

rdfQuery is a JavaScript library which helps us to process RDF data in the browser itself. It helps in parsing RDFa data embedded in the webpage and is even used to reason the RDF content on the webpage and discover interesting relations. It can be used to create interfaces for editing the semantic web. rdfQuery is available in three flavor :-

  1. Core rdfQuery – Helps one to create a triplestores and query it.
  2. rdfQuery with RDFa- Helps parsing RDFa and add it to your page.
  3. rdfQuery with rules- Helps to reason over the triplestores using rules.

To get started we will cover each of the above. I have divided the entire tutorial into three parts so that one could go across the different features of rdfQuery separately and would be able to create something useful out of it.

Core rdfQuery(Manual Creation of DataBank)

Core rdfQuery provides with methods to create databanks using triples. These triples can be just strings in turtle format. It also allows to add namespace for vocabulary such as foaf  etc. I have also included some custom namespace for my data. In this tutorial we would just build a small application which would make a databank manually using rdfQuery and then query the databank using simple SPARQL and print the data onto a table. So without more delay lets start with it.

Download a copy of rdfQuery Core library along with the latest copy of JQuery. After downloading these library include these into the html page.(You could also use JSFiddle to do this where you can include the rdfQuery library in the resource area.)

The syntax to create a DataBank using rdfQuery is

$.rdf.databank.(array [,options]);

The array above which you pass to the databank function can be a string of RDF data or can be an array of $.rdf.triple objects. I planned to make an RDF of myself along with my friends. So the code goes somewhat like this 

var rdfDataBank= $.rdf.databank([
'fbook:shivkumarganesh a foaf:Person',
'fbook:shivkumarganesh foaf:name "Shiv Kumar Ganesh" .',
'fbook:shivkumarganesh foaf:firstName "Shiv" .',
'fbook:shivkumarganesh foaf:lastName "Ganesh" .',
'fbook:shivkumarganesh foaf:family_name "Ganesh" .',
'fbook:shivkumarganesh foaf:nick "Shivy" .',
'fbook:shivkumarganesh foaf:knows fbook:Priyank',
'fbook:shivkumarganesh foaf:knows fbook:Karuna',
'fbook:Priyank a foaf:Person .',
'fbook:Priyank foaf:name "Priyank Chaube" .',
'fbook:Karuna a foaf:Person .',
'fbook:Karuna foaf:name "Karunakaran K" .'

], {
base: 'http://shivkumarganesh.in/',
namespaces: {
foaf: 'http://xmlns.com/foaf/0.1/',
fbook: 'http://facebook.com/',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
}
});

Lets review some major part of the code :-

  • In line 1 var rdfDataBank is an databank object which stores the rdf data and this is the object upon which we would be calling the SPARQL queries.
  • Line 16 refers to the point where the options begin in the $.rdf.databank(array [,options]) .
  • Options refer to the base URI along with and array of namespace that are used in the above RDF description eg: rdf, fbook, foaf.

I have also described about few of my friends in these rdf triples. They just have their name described. This entire thing as mentioned above can be done using $.rdf.triple object too.We can code it as follows :-

$.rdf.databank([
$.rdf.triple('subject predicate object .' , {
namespaces:{//The valid namespace used for any of the above subject,predicate,object}
})
]);

Now since we know how to make a data bank manually. Lets go ahead and Query the data bank object which in this case is rdfDataBank.

var dataBankQuery = $.rdf({
databank: rdfDataBank
}).where('?s ?p ?o');
var dataBankBindings = dataBankQuery.select();
for(var i=0;i{
$('#dataQueried').append('
'+''+dataBankBindings[i].s.value+''+''+dataBankBindings[i].p.value+''+''+dataBankBindings[i].o.value+''+'
');
}

In the above code, in line no. 1 we declare an object(dataBankQuery) which contains the query results.After this in Line 4 we call select() method on the dataBankQuery this select method helps to return an array that contains objects representing the query bindings as JavaScript objects.So that after this the dataBankBindings variable can be queried for individual data.

We should also note that $.rdf( argument ) also takes in databank as an argument. It does that to refer the databank where the data is populated as in to differentiate the databank in use. I have also tried to traverse the entire object containing the binding to show the rdf triples in a tabular format.

If we want to export the triples we can do that by using a function called dump() called on the databank.

var dataBankJSON=rdfDataBank.dump();

This is how you would make a data bank manually using rdfQuery and query it using SPARQL. In the next tutorial we would cover another method of using rdfQuery where we use to generate data banks from the information on the page itself i.e the RDFa information.

The demo can be found on fiddle 

Image: Courtesy rdfQuery (Google Codes)

Resource Description Framework Data (computing)

Published at DZone with permission of Shiv Kumar Ganesh. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • Using JSON Web Encryption (JWE)
  • Why Every Fintech Company Needs DevOps
  • Choosing the Best Cloud Provider for Hosting DevOps Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: