[转帖]使用jena写入多个本体文件到mysql_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1922 | 回复: 0   主题: [转帖]使用jena写入多个本体文件到mysql        下一篇 
huan.liu
注册用户
等级:新兵
经验:72
发帖:58
精华:0
注册:2011-11-23
状态:离线
发送短消息息给huan.liu 加好友    发送短消息息给huan.liu 发消息
发表于: IP:您无权察看 2015-3-18 16:49:17 | [全部帖] [楼主帖] 楼主

代码如下:

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不清楚存放在哪,后续试验会做)。



--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论