使用jena写入多个本体文件到mysql
代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
public class JenaDBOpration {
// path of driver class
public static final String strDriver = "com.mysql.jdbc.Driver";
// The URL of the MySql database
// this can be modified when the server location changes
public static final String strURL = "jdbc:mysql://localhost/ontologydb";
// database user id
public static final String strUser = "root";
// database password
public static final String strPassWord = "nicky";
// database type
public static final String strDB = "MySQL";
public static final String strFilePath1 = "ontologymetadatamodel.owl/";
public static final String strFilePath2 = "1dhdwqm.owl/";
public static final String strOntologyName1 = "One";
public static final String strOntologyName2 = "Two";
public static boolean TestMySQLDriver(String MySQL_Driver) {
boolean result = false;
// 加载数据库驱动类,需要处理异常
try {
Class.forName(MySQL_Driver);
result = true;
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
System.out.println("Driver is not available...");
}
return result;
}
public static InputStreamReader ReadFile(String FilePath) {
FileInputStream inputSreamfile = null;
try {
File file = new File(FilePath);
inputSreamfile = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Ontology File is not available...");
}
InputStreamReader in = null;
try {
in = new InputStreamReader(inputSreamfile, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return in;
}
// 建立一个到mysql的连接
public static IDBConnection connectDB(String DB_URL, String DB_USER,
String DB_PASSWARD, String DB_NAME) {
return new DBConnection(DB_URL, DB_USER, DB_PASSWARD, DB_NAME);
}
/* 从文件读取本体并将其存入数据库 */
public static boolean createDBModelFromFile(IDBConnection con,
String Ontology_Name, String filePath) {
try {
ModelMaker maker = ModelFactory.createModelRDBMaker(con);
Model base = maker.createModel(Ontology_Name);
base.read(ReadFile(filePath), null);
base.commit();
} catch (Exception E) {
System.out.println(E.getMessage());
}
return true;
}
/* 从数据库中得到已存入本体 */
public static OntModel getModelFromDB(IDBConnection con,
String Ontology_Name) {
ModelMaker maker = ModelFactory.createModelRDBMaker(con);
Model base = maker.getModel(Ontology_Name);
OntModel newmodel = ModelFactory.createOntologyModel(
getModelSpec(maker), base);
return newmodel;
}
public static OntModelSpec getModelSpec(ModelMaker maker) {
OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
spec.setImportModelMaker(maker);
return spec;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
boolean test = false;
try {
test = JenaDBOpration.TestMySQLDriver(strDriver);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
if (test) {
try {
IDBConnection idbc = JenaDBOpration.connectDB(strURL,
strUser, strPassWord, strDB);
JenaDBOpration.createDBModelFromFile(idbc,
strOntologyName1, strFilePath1);
JenaDBOpration.createDBModelFromFile(idbc,
strOntologyName2, strFilePath2);
} catch (Exception ex2) {
System.out.println(ex2.getMessage());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
存储到Mysql后,注意是两个不同名称的本体文件,共计生成了9张表
在7张表的基础上追加了2涨,分别为Jena_g2t0_reif和Jena_g2t1_stmt,
另外,Jena_graph更新了一条记录,Jena_long_lit中存放的是rdf:label的数据,Jena_sys_stmt中追加了若干记录;
Jena_g1t1_stmt和Jena_g2t1_stmt中村法规的是本体文件的数据信息,包括有class、properties(其它的暂时还不清楚,因为我的本体里面暂时只有这些,instance不清楚存放在哪,后续试验会做)。
所使用的本体文件 http://files.cnblogs.com/wang-ss/%E4%B8%80%E7%BB%B4%E6%B0%B4%E8%B4%A8%E6%A8%A1%E5%9E%8B%E6%9C%AC%E4%BD%93.rar
所使用的本体文件2 http://files.cnblogs.com/wang-ss/%E6%9C%AC%E4%BD%93%E5%85%83%E6%95%B0%E6%8D%AE%E6%A8%A1%E5%9E%8B%E6%9C%AC%E4%BD%93.rar