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+"\t");    
            System.out.print(" 謂語 " + predicate+"\t");        
            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 .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM