|
From: Mishel U. V. <dmu...@eu...> - 2016-10-14 09:24:13
|
Hello, I’m trying to connect to Blazegraph using SESAME API with Eclipse IDE for JAVA Developers (NEON 4.6.1), to do this, I'm following a tutorial that Blazegraph provides: https://wiki.blazegraph.com/wiki/index.php/Sesame_API_remote_mode The problem is that when the program is executed, a class from a library of Blazegraph (ServiceProviderHook) through the ServiceProviderHook. forceLoad() method calls to the RDFParserRegistry's method add(), this is inherited for this class from the superclass ServiceRegistry. In Eclipse's console appears : Exception in thread "main" java.lang.NoSuchMethodError: org.openrdf.rio.RDFParserRegistry.add(Ljava/lang/Object;)Ljava/lang/Object Of course, this method isn't in the class RDFParserRegistry, but, it can use it. At first, I looked for a new version of Blazegraph that don’t use this method, because I had problems before with other methods that use the mentioned tutorial, methods like RDFFormat.values(), or RDFFormat.register(), I fixed that problems using an older version of Sesame, specifically, 2.8.10, because in the newest versions these methods are deprecated. My code is simple, only contains the Blazegraph Tutorial says, with a little modification in the file path, and in the file format Turtle instead of N3. For execute this code, I am using bigdata-client dependency in 2.1.4, and sesame-rio-api in 2.8.10. This is it: package prueba.blazegraph; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.openrdf.model.Statement; import org.openrdf.query.BindingSet; import org.openrdf.query.GraphQueryResult; import org.openrdf.query.TupleQueryResult; import org.openrdf.rio.RDFFormat; import org.openrdf.rio.RDFParserRegistry; import org.apache.log4j.Logger; import com.bigdata.rdf.sail.webapp.SD; import com.bigdata.rdf.sail.webapp.client.ConnectOptions; import com.bigdata.rdf.sail.webapp.client.JettyResponseListener; import com.bigdata.rdf.sail.webapp.client.RemoteRepository; import com.bigdata.rdf.sail.webapp.client.RemoteRepositoryManager; public class Blazegraph { private final Logger LOGGER= Logger.getLogger(Blazegraph.class); private final String PATH = "http://localhost:9999/bigdata"; public void conexion() { final RemoteRepositoryManager REPOSITORIO = new RemoteRepositoryManager(PATH,true); try { JettyResponseListener response = getStatus(REPOSITORIO); LOGGER.info(response.getResponseBody()); // Vamos a crear un namespace si no existe final String NAMESPACE = "primerNamespace"; Properties properties = new Properties(); properties.setProperty("com.bigdata.rdf.sail.namespace", NAMESPACE); if (!namespaceExists(REPOSITORIO, NAMESPACE)) { LOGGER.info("Vamos a crear el namespace"); REPOSITORIO.createRepository(NAMESPACE, properties); LOGGER.info("Namespace creado"); } else { LOGGER.info(String.format("El namespace ya existe", NAMESPACE)); } // Vamos a obtener las propiedades del namespace LOGGER.info(String.format("Lista de propiedades del namespace", NAMESPACE)); response = getNamespaceProperties(REPOSITORIO, NAMESPACE); LOGGER.info(response.getResponseBody()); // Vamos a cargar los datos del fichero String documentoASubir = "src/main/resources/resources/datos.ttl"; loadDataFromResource(REPOSITORIO, NAMESPACE, documentoASubir); // Ejecutamos una query sobre los datos cargados TupleQueryResult result = REPOSITORIO.getRepositoryForNamespace(NAMESPACE) .prepareTupleQuery("SELECT * WHERE {?o ?p ?q}").evaluate(); // Procesamos los resultados try { while (result.hasNext()) { BindingSet bs = result.next(); LOGGER.info("-------------------->" + bs); } } finally { result.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { REPOSITORIO.close(); } catch (Exception e) { e.printStackTrace(); } } } private void loadDataFromResource(RemoteRepositoryManager pRepositorio, String pNamespace, String pDocumentoASubir) throws Exception { InputStream is= Blazegraph.class.getResourceAsStream(pDocumentoASubir); if(is==null){ throw new IOException("No se encuentra el archivo"); } try { pRepositorio.getRepositoryForNamespace(pNamespace).add( new RemoteRepository.AddOp(is, RDFFormat.TURTLE)); } finally { is.close(); } } private JettyResponseListener getNamespaceProperties(RemoteRepositoryManager pRepositorio, String pNamespace) throws Exception { ConnectOptions options = new ConnectOptions(PATH + "/namespace/" +pNamespace + "/properties"); options.method = "GET"; return pRepositorio.doConnect(options); } private boolean namespaceExists(RemoteRepositoryManager pRepositorio, String pNamespace) throws Exception { boolean existe=false; GraphQueryResult result = pRepositorio.getRepositoryDescriptions(); try { while (result.hasNext()) { Statement statement = result.next(); if (statement.getPredicate().toString().toString().equals(SD.KB_NAMESPACE.stringValue())) { if (pNamespace.equals(statement.getObject().stringValue())) { existe= true; } } } } finally { result.close(); } return existe; } private JettyResponseListener getStatus(RemoteRepositoryManager pRepositorio) throws Exception { ConnectOptions options = new ConnectOptions(PATH + "/status"); options.method = "GET"; return pRepositorio.doConnect(options); } } Thanks for your attention. Mishel Uchuari |