[Sesame] adds ternary data to the Triple Store.

Source: Internet
Author: User
Tags baseuri

I plan to write a series of articles about how to use the sesame database. This is the second article. For the first article, see here to explain how to build the sesame database.

There are many methods for adding triple triples to the Sesame database. Here we will explain two methods: single-entry addition and batch addition.

1. the Sesame database provides several data storage methods, including local database NativeStore, memory-based MemoryStore, remote database-based HTTP, and relational database-based MySQLStore.
Declare variables:
private Repository repo;private MemoryStore memStore;private NativeStore natStore;private File repoFile;private RepositoryConnection repoConn;

Memory-based MemoryStore:
/** * To get the repository within memory. */public RepoUtil() {repoFile = new File(Const.repoPath);memStore = new MemoryStore();repo = new SailRepository(memStore);}


Based on Local NativeStore:
/** * To get the repository on the disk. * @param repoPath the repository file path */public RepoUtil(String repoPath) {repoFile = new File(repoPath);natStore = new NativeStore(repoFile);repo = new SailRepository(natStore);}

Network-based HTTP Connection:
/** * To get the repository on the Http server. * @param server the server address * @param repoId the repository ID */public RepoUtil(String server, String repoId) {repo = new HTTPRepository(server, repoId);}

Based on Relational Database MySQL: refer to my other article.
1.1. initialize the database
try {repo.initialize();repoConn = repo.getConnection();//Get the connection from repository connection pool//repoConn.setAutoCommit(false);//why deprecate the setAutoCommit method?} catch(RepositoryException e) {e.printStackTrace();}


2. Add a single piece of Data 2.1. Generate a URI. Here we provide a function to generate a URI. You don't need to worry about this. Just understand the essentials of the URI generation method. Initialize URI and Literal generator ValueFactory:
ValueFactory valueFactory = new ValueFactoryImpl();

Then generate the URI:
/** * To get the URI of the specific string value * 1. if it is already a URI, then return; * 2. else translate it to URI format and return. * @param iden * @return the true URI */public URI getUri(String iden) {URI rtn = null;String url = null;StringBuilder strRtn = new StringBuilder(uriBuilder);if(isUri(iden)) {System.out.println("isUri");return valueFactory.createURI(iden);} else {try {url = URLEncoder.encode(iden,"utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}strRtn.append(url);rtn = valueFactory.createURI(strRtn.toString());return rtn;}}

The URI judgment function (from the Network) is attached here)
/** * To justify if the input string is  * in the format of URI. * @param obj * @return */public boolean isUri(String obj) {return obj.matches("(([a-zA-Z][0-9a-zA-Z+\\\\-\\\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\\\.\\\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\\\.\\\\-_!~*'()%]+)?");//return false;}

Generate a simplified version of the URI and Literal methods (some problems are ignored. We recommend that you use the above functions ):
URI creativeWork = vf.createURI(namespace+"CreativeWork");Literal about = vf.createLiteral(namespace+"about#"+"SomeString");

After establishing Connection, URI, and Literal, you can insert the triple:
/** * The URI-URI-Literal format SPO record. */public void addRecord(URI subj, URI pred, Literal obj) {try {//repoConn = repo.getConnection();repoConn.add(subj, pred, obj);//repoConn.close();} catch (RepositoryException e) {e.printStackTrace();} }/** * The URI-URI-URI format SPO record. */public void addRecord(URI subj, URI pred, URI obj) {try {//repoConn = repo.getConnection();repoConn.add(subj, pred, obj);//repoConn.close();} catch (RepositoryException e) {e.printStackTrace();}}

3. If a large amount of data has been saved in the file for batch import, you can directly use the batch import interface provided by Sesame without writing the code for reading or writing data manually.
File importFile = new File("segment"+j+".ttl");String baseURI = "http://rk.com/import/test/";RepositoryConnection con;try {FileReader fileReader = new FileReader(importFile);BufferedReader reader = new BufferedReader(fileReader);con = repo.getConnection();con.add(reader, baseURI, RDFFormat.TURTLE);System.out.println("Add "+j+" ends.");con.close();} catch (RepositoryException e) {e.printStackTrace();} catch (RDFParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} 

Note the memory size limit of Java Heap. You can see here to modify the memory limit of the Java Virtual Machine.
At this point, several methods for writing Sesame data have been completed. Next, we will introduce data export and data modification.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.