Jena解析rdf、nt、ttl格式数据

比如有一个ttl格式的文件名为cco.ttl
package com.jena;

import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;

public class test
{
    public static void main(String args[])
    {
        //String inputFileName = "E:\Pattern Mining\test.rdf";
        //String inputFileName = "E:\Pattern Mining\test.nt";
        String inputFileName = "E:\ChEMBL\cco.ttl";
        
        Model model = ModelFactory.createDefaultModel();

        InputStream in = FileManager.get().open(inputFileName);
        if (in == null) 
        {
            throw new IllegalArgumentException("File: " + inputFileName + " not found");
        }

        //model.read(in, "","RDF/XML");//根据文件格式选用参数即可解析不同类型
        //model.read(in, "","N3");
        model.read(in, "","TTL");
        System.out.println("开始");
        // list the statements in the graph
        StmtIterator iter = model.listStatements();

        // print out the predicate, subject and object of each statement
        while (iter.hasNext()) 
        {
            Statement stmt = iter.nextStatement(); // get next statement
            //Resource subject = stmt.getSubject(); // get the subject
            //Property predicate = stmt.getPredicate(); // get the predicate
            //RDFNode object = stmt.getObject(); // get the object

            String subject = stmt.getSubject().toString(); // get the subject
            String predicate = stmt.getPredicate().toString(); // get the predicate
            RDFNode object = stmt.getObject(); // get the object
            
            
            System.out.print("主语 " + subject+"	");    
            System.out.print(" 谓语 " + predicate+"	");        
            if (object instanceof Resource) 
            {
                System.out.print(" 宾语 " + object);
            }
            else {// object is a literal
                System.out.print("宾语 "" + object.toString() + """);
            }
            System.out.println(" ."); 
        }
    }
}

运行结果

主语 http://rdf.ebi.ac.uk/terms/chembl#NonMolecular     谓语 http://www.w3.org/2004/02/skos/core#prefLabel    宾语 "ChEMBL NonMolecular Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#NonMolecular     谓语 http://www.w3.org/2000/01/rdf-schema#subClassOf     宾语 http://rdf.ebi.ac.uk/terms/chembl#Target .
主语 http://rdf.ebi.ac.uk/terms/chembl#NonMolecular     谓语 http://www.w3.org/2000/01/rdf-schema#label    宾语 "ChEMBL NonMolecular Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#NonMolecular     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#type     宾语 http://www.w3.org/2002/07/owl#Class .
主语 http://rdf.ebi.ac.uk/terms/chembl#PubchemThomPharmRef     谓语 http://www.w3.org/2004/02/skos/core#prefLabel    宾语 "Pubchem Thomson Pharma Subset Cross Reference^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#PubchemThomPharmRef     谓语 http://www.w3.org/2000/01/rdf-schema#subClassOf     宾语 http://rdf.ebi.ac.uk/terms/chembl#MoleculeDbRef .
主语 http://rdf.ebi.ac.uk/terms/chembl#PubchemThomPharmRef     谓语 http://www.w3.org/2000/01/rdf-schema#label    宾语 "Pubchem Thomson Pharma Subset Cross Reference^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#PubchemThomPharmRef     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#type     宾语 http://www.w3.org/2002/07/owl#Class .
主语 http://rdf.ebi.ac.uk/terms/chembl#Activity     谓语 http://www.w3.org/2004/02/skos/core#prefLabel    宾语 "ChEMBL Activity Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#Activity     谓语 http://www.w3.org/2000/01/rdf-schema#subClassOf     宾语 http://rdf.ebi.ac.uk/terms/chembl#ChEMBL .
主语 http://rdf.ebi.ac.uk/terms/chembl#Activity     谓语 http://www.w3.org/2000/01/rdf-schema#label    宾语 "ChEMBL Activity Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#Activity     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#type     宾语 http://www.w3.org/2002/07/owl#Class .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://www.w3.org/2004/02/skos/core#prefLabel    宾语 "ChEMBL Assay Subcellular Fraction ^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://purl.org/dc/elements/1.1/description    宾语 "Name of subcellular fraction used in the assay system (e.g., microsomes, mitochondria).^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://www.w3.org/2000/01/rdf-schema#range     宾语 http://www.w3.org/2001/XMLSchema#string .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://www.w3.org/2000/01/rdf-schema#label    宾语 "ChEMBL Assay Subcellular Fraction ^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://www.w3.org/2000/01/rdf-schema#domain     宾语 http://rdf.ebi.ac.uk/terms/chembl#Assay .
主语 http://rdf.ebi.ac.uk/terms/chembl#assaySubCellFrac     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#type     宾语 http://www.w3.org/2002/07/owl#DatatypeProperty .
主语 -71367f46:15678e1eebc:-7ffb     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#rest     宾语 -71367f46:15678e1eebc:-7ffa .
主语 -71367f46:15678e1eebc:-7ffb     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#first     宾语 http://rdf.ebi.ac.uk/terms/chembl#Source .
主语 http://rdf.ebi.ac.uk/terms/chembl#FabPrime     谓语 http://www.w3.org/2004/02/skos/core#prefLabel    宾语 "ChEMBL FabPrime Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#FabPrime     谓语 http://purl.org/dc/elements/1.1/description    宾语 "Fragment, antigen-binding, including hinge region (one arm)^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#FabPrime     谓语 http://www.w3.org/2000/01/rdf-schema#subClassOf     宾语 http://rdf.ebi.ac.uk/terms/chembl#Antibody .
主语 http://rdf.ebi.ac.uk/terms/chembl#FabPrime     谓语 http://www.w3.org/2000/01/rdf-schema#label    宾语 "ChEMBL FabPrime Class^^http://www.w3.org/2001/XMLSchema#string" .
主语 http://rdf.ebi.ac.uk/terms/chembl#FabPrime     谓语 http://www.w3.org/1999/02/22-rdf-syntax-ns#type     宾语 http://www.w3.org/2002/07/owl#Class .
原文地址:https://www.cnblogs.com/Donnnnnn/p/5761788.html