Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZEPPELIN-3644] SPARQL-Interpreter support via Apache Jena ARQ #3480

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -51,7 +51,7 @@ services:
env:
global:
# Interpreters does not required by zeppelin-server integration tests
- INTERPRETERS='!beam,!hbase,!pig,!jdbc,!file,!flink,!ignite,!kylin,!lens,!cassandra,!elasticsearch,!bigquery,!alluxio,!scio,!livy,!groovy,!sap,!java,!geode,!neo4j,!hazelcastjet,!submarine'
- INTERPRETERS='!beam,!hbase,!pig,!jdbc,!file,!flink,!ignite,!kylin,!lens,!cassandra,!elasticsearch,!bigquery,!alluxio,!scio,!livy,!groovy,!sap,!java,!geode,!neo4j,!hazelcastjet,!submarine,!sparql'

matrix:
include:
Expand Down
1 change: 1 addition & 0 deletions conf/interpreter-list
Expand Up @@ -43,4 +43,5 @@ sap org.apache.zeppelin:zeppelin-sap:0.9.0 SAP Supp
scalding org.apache.zeppelin:zeppelin-scalding_2.0.10:0.9.0 Scalding interpreter
scio org.apache.zeppelin:zeppelin-scio:0.9.0 Scio interpreter
shell org.apache.zeppelin:zeppelin-shell:0.9.0 Shell command
sparql org.apache.zeppelin:zeppelin-sparql:0.9.0 Sparql interpreter
submarine org.apache.zeppelin:zeppelin-submarine:0.9.0 Submarine interpreter
1 change: 1 addition & 0 deletions docs/_includes/themes/zeppelin/_navigation.html
Expand Up @@ -153,6 +153,7 @@
<li><a href="{{BASE_PATH}}/interpreter/scalding.html">Scalding</a></li>
<li><a href="{{BASE_PATH}}/interpreter/scio.html">Scio</a></li>
<li><a href="{{BASE_PATH}}/interpreter/shell.html">Shell</a></li>
<li><a href="{{BASE_PATH}}/interpreter/sparql.html">Sparql</a></li>
<li><a href="{{BASE_PATH}}/interpreter/submarine.html">Submarine</a></li>
</ul>
</li>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -158,6 +158,7 @@ limitations under the License.
* [Scio](./interpreter/scio.html)
* [Shell](./interpreter/shell.html)
* [Spark](./interpreter/spark.html)
* [Sparql](./interpreter/sparql.html)
* [Submarine](./interpreter/submarine.html)

#### External Resources
Expand Down
55 changes: 55 additions & 0 deletions docs/interpreter/sparql.md
@@ -0,0 +1,55 @@
---
layout: page
title: "SPARQL Interpreter for Apache Zeppelin"
description: "SPARQL is an RDF query language able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. Apache Zeppelin uses Apache Jena"
group: interpreter
---
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
{% include JB/setup %}

# SPARQL Interpreter for Apache Zeppelin

<div id="toc"></div>

## Overview
[SPARQL](https://www.w3.org/TR/sparql11-query/) is an RDF query language able to retrieve and manipulate data stored in Resource Description Framework (RDF) format.
Apache Zeppelin uses [Apache Jena](https://jena.apache.org/) to query SPARQL-Endpoints.

To query your endpoint configure it in the Interpreter-Settings and use the **%sparql** interpreter.
Then write your query in the paragraph.
If you want the prefixes to replace the URI's, set the replaceURIs setting.

## Configuration
<table class="table-configuration">
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
<tr>
<td>sparql.endpoint</td>
<td>http://dbpedia.org/sparql</td>
<td>Complete URL of the endpoint</td>
</tr>
<tr>
<td>sparql.replaceURIs</td>
<td>true</td>
<td>Replace the URIs in the result with the prefixes</td>
</tr>
</table>

## Example

<img src="{{BASE_PATH}}/assets/themes/zeppelin/img/docs-img/sparql-example.png" width="100%"/>
5 changes: 5 additions & 0 deletions docs/usage/interpreter/installation.md
Expand Up @@ -243,6 +243,11 @@ You can also find the below community managed interpreter list in `conf/interpre
<td>org.apache.zeppelin:zeppelin-shell:0.9.0</td>
<td>Shell command</td>
</tr>
<tr>
<td>sparql</td>
<td>org.apache.zeppelin:zeppelin-sparql:0.9.0</td>
<td>Sparql interpreter</td>
</tr>
<tr>
<td>submarine</td>
<td>org.apache.zeppelin:zeppelin-submarine:0.9.0</td>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -86,6 +86,7 @@
<module>hazelcastjet</module>
<module>geode</module>
<module>ksql</module>
<module>sparql</module>
<module>zeppelin-web</module>
<module>zeppelin-server</module>
<module>zeppelin-jupyter</module>
Expand Down
80 changes: 80 additions & 0 deletions sparql/pom.xml
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>zeppelin-interpreter-parent</artifactId>
<groupId>org.apache.zeppelin</groupId>
<version>0.9.0-SNAPSHOT</version>
<relativePath>../zeppelin-interpreter-parent/pom.xml</relativePath>
</parent>

<groupId>org.apache.zeppelin</groupId>
<artifactId>zeppelin-sparql</artifactId>
<packaging>jar</packaging>
<version>0.9.0-SNAPSHOT</version>
<name>Zeppelin: Sparql interpreter</name>
<description>Zeppelin sparql support</description>

<properties>
<interpreter.name>sparql</interpreter.name>
<jena.version>3.12.0</jena.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>${jena.version}</version>
</dependency>

<!-- test libraries -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-fuseki-main</artifactId>
<version>${jena.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
143 changes: 143 additions & 0 deletions sparql/src/main/java/org/apache/zeppelin/sparql/JenaInterpreter.java
@@ -0,0 +1,143 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.sparql;

import org.apache.http.HttpStatus;

import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryParseException;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.sparql.ARQException;
import org.apache.jena.sparql.engine.http.QueryExceptionHTTP;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.util.Map;

import org.apache.zeppelin.interpreter.InterpreterResult;


/**
* Interpreter for SPARQL-Query via Apache Jena ARQ.
*/
public class JenaInterpreter implements SparqlEngine {
private static final Logger LOGGER = LoggerFactory.getLogger(JenaInterpreter.class);

private String serviceEndpoint;
private boolean replaceURIs;
private boolean removeDatatypes;

private QueryExecution queryExecution;

public JenaInterpreter(String serviceEndpoint, boolean replaceURIs, boolean removeDatatypes) {
this.serviceEndpoint = serviceEndpoint;
this.replaceURIs = replaceURIs;
this.removeDatatypes = removeDatatypes;
}

@Override
public InterpreterResult query(String query) {
LOGGER.info("SPARQL: Run Query '" + query + "' against " + serviceEndpoint);

try {
queryExecution = QueryExecutionFactory.sparqlService(serviceEndpoint, query);
PrefixMapping prefixMapping = queryExecution.getQuery().getPrefixMapping();

// execute query and get Results
ResultSet results = queryExecution.execSelect();

// transform ResultSet to TSV-String
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ResultSetFormatter.outputAsTSV(outputStream, results);
String tsv = new String(outputStream.toByteArray());

if (replaceURIs) {
LOGGER.info("SPARQL: Replacing URIs");
tsv = replaceURIs(tsv, prefixMapping);
}

if (removeDatatypes) {
LOGGER.info("SPARQL: Removing datatypes");
tsv = removeDatatypes(tsv);
}

return new InterpreterResult(
InterpreterResult.Code.SUCCESS,
InterpreterResult.Type.TABLE,
tsv);
} catch (QueryParseException e) {
LOGGER.error(e.toString());
return new InterpreterResult(
InterpreterResult.Code.ERROR,
"Error: " + e.getMessage());
} catch (QueryExceptionHTTP e) {
LOGGER.error(e.toString());
int responseCode = e.getResponseCode();

if (responseCode == HttpStatus.SC_UNAUTHORIZED) {
return new InterpreterResult(
InterpreterResult.Code.ERROR,
"Unauthorized.");
} else if (responseCode == HttpStatus.SC_NOT_FOUND) {
return new InterpreterResult(
InterpreterResult.Code.ERROR,
"Endpoint not found, please check endpoint in the configuration.");
} else {
return new InterpreterResult(
InterpreterResult.Code.ERROR,
"Error: " + e.getMessage());
}
} catch (ARQException e) {
return new InterpreterResult(
InterpreterResult.Code.INCOMPLETE,
"Query cancelled.");
}
}

@Override
public void cancel() {
if (queryExecution != null) {
queryExecution.abort();
}
}

@Override
public void close() {
if (queryExecution != null) {
queryExecution.close();
}
}

private String replaceURIs(String tsv, PrefixMapping prefixMapping) {
Map<String, String> pmap = prefixMapping.getNsPrefixMap();
for (Map.Entry<String, String> entry : pmap.entrySet()) {
tsv = tsv.replaceAll(entry.getValue(), entry.getKey() + ":");
}
return tsv;
}

private String removeDatatypes(String tsv) {
// capture group: "($1)"^^<.+?>
return tsv.replaceAll("\"(.+?)\"\\^\\^\\<.+?\\>", "$1");
}
}
32 changes: 32 additions & 0 deletions sparql/src/main/java/org/apache/zeppelin/sparql/SparqlEngine.java
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.sparql;

import org.apache.zeppelin.interpreter.InterpreterResult;


/**
* Interpreter for SPARQL-Query via Apache Jena ARQ.
*/
public interface SparqlEngine {
InterpreterResult query(String query);

void cancel();

void close();
}