如何将gzip压缩文件加载到rdf4j存储库?

时间:2017-03-20 17:23:51

标签: java gzip rdf gz rdf4j

我想将gzip压缩文件加载到org.eclipse.rdf4j.repository.Repository。在上载期间,必须将状态消息记录到控制台。我的rdf文件的大小是大约1GB的未压缩或大约50MB的压缩数据。

2 个答案:

答案 0 :(得分:2)

变式1

以下示例将带有gzip压缩数据的InputStream加载到内存中的rdf存储库中。 rdf4j直接支持压缩格式。 每个第100000个语句将使用RepositoryConnectionListenerAdapter打印到标准输出。

import java.io.InputStream;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.event.base.NotifyingRepositoryConnectionWrapper;
import org.eclipse.rdf4j.repository.event.base.RepositoryConnectionListenerAdapter;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.sail.memory.MemoryStore;

public class MyTripleStore {

    Repository repo;

    /**
     * Creates an inmemory triple store
     * 
     */
    public MyTripleStore() {
        repo = new SailRepository(new MemoryStore());
        repo.initialize();
    }

    /**
     * @param in gzip compressed data on an inputstream
     * @param format the format of the streamed data
     */
    public void loadZippedFile(InputStream in, RDFFormat format) {
        System.out.println("Load zip file of format " + format);
        try (NotifyingRepositoryConnectionWrapper con =
                new NotifyingRepositoryConnectionWrapper(repo, repo.getConnection());) {
            RepositoryConnectionListenerAdapter myListener =
                    new RepositoryConnectionListenerAdapter() {
                        private long count = 0;
                        @Override
                        public void add(RepositoryConnection arg0, Resource arg1, IRI arg2, 
                                         Value arg3, Resource... arg4) {
                            count++;
                            if (count % 100000 == 0)
                                    System.out.println("Add statement number " + count + "\n" 
                                    + arg1+ " " + arg2 + " " + arg3);
                        }
                    };
            con.addRepositoryConnectionListener(myListener);
            con.add(in, "", format);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

变式2

此变体实现AbstractRDFHandler以提供报告。

import java.io.InputStream;

import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.util.RDFInserter;
import org.eclipse.rdf4j.repository.util.RDFLoader;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler;
import org.eclipse.rdf4j.sail.memory.MemoryStore;

public class MyTripleStore {
    Repository repo;

    /**
      * Creates an inmemory triple store
      * 
      */
    public MyTripleStore() {
        repo = new SailRepository(new MemoryStore());
        repo.initialize();
   }

    /**
     * @param in gzip compressed data on an inputstream
     * @param format the format of the streamed data
     */
    public void loadZippedFile1(InputStream in, RDFFormat format) {
        try (RepositoryConnection con = repo.getConnection()) {
            MyRdfInserter inserter = new MyRdfInserter(con);
            RDFLoader loader =
                    new RDFLoader(con.getParserConfig(), con.getValueFactory());
            loader.load(in, "", RDFFormat.NTRIPLES, inserter);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    class MyRdfInserter extends AbstractRDFHandler {
        RDFInserter rdfInserter;
        int count = 0;

        public MyRdfInserter(RepositoryConnection con) {
            rdfInserter = new RDFInserter(con);
        }

        @Override
        public void handleStatement(Statement st) {
            count++;
            if (count % 100000 == 0)
                System.out.println("Add statement number " + count + "\n"
                        + st.getSubject().stringValue() + " "
                        + st.getPredicate().stringValue() + " "
                        + st.getObject().stringValue());
            rdfInserter.handleStatement(st);
        }
    }
}

这是如何调用代码

MyTripleStore ts = new MyTripleStore();
ts.loadZippedFile(new FileInputStream("your-ntriples-zipped.gz"),
            RDFFormat.NTRIPLES);

答案 1 :(得分:2)

实际上,RDF4J存储库将自动正确处理压缩(zip / gzip)文件。所以你可以这样做:

   RepositoryConnection conn = ... ; // your store connection
   conn.add(new File("file.zip"), null, RDFFormat.NTRIPLES):

如果您想要包含报告,则另一种(稍微简单一些)方法是将org.eclipse.rdf4j.repository.util.RDFLoader类与RDFInserter结合使用:

RepositoryConnection conn = ... ; // your store connection
RDFInsert inserter = new RDFInserter(conn);
RDFLoader loader = new RDFLoader(conn.getParserConfig(), conn.getValueFactory());

loader.load(new File("file.zip"), RDFFormat.NTRIPLES, inserter));

RDFLoader负责正确解压缩(zip或gzip)文件。

要获得中间报告,您可以将RDFInserter包装在您自己的自定义AbstractRDFHandler中,进行计数和报告(在传递给包装器之前)。