weka DatabaseSaver类分析

数据库写入操作类,执行模式在父类中定义
public class DatabaseSaver extends AbstractSaver

建表方法,由写入程序调用
private void writeStructure()

新建配置SQL语句
 StringBuffer query = new StringBuffer();
获取建表数据集
 Instances structure = getInstances();
开始配置SQL语句
 query.append("CREATE TABLE ");
表名获取并加入sql
      if(m_tabName || m_tableName.equals(""))
        m_tableName = m_DataBaseConnection.maskKeyword(structure.relationName());
      if(m_DataBaseConnection.getUpperCase()){
        m_tableName = m_tableName.toUpperCase();
        m_createInt = m_createInt.toUpperCase();
        m_createDouble = m_createDouble.toUpperCase();
        m_createText = m_createText.toUpperCase();
        m_createDate = m_createDate.toUpperCase();
      }
      m_tableName = m_tableName.replaceAll("[^\\w]","_");
      m_tableName = m_DataBaseConnection.maskKeyword(m_tableName);
      query.append(m_tableName);
如果数据集有值,则开始字段配置
      if(structure.numAttributes() == 0)
          throw new Exception("Instances have no attribute.");
      query.append(" ( ");
如果数据集拥有主键,则开始配置主键
      if(m_id){
        if(m_DataBaseConnection.getUpperCase())
              m_idColumn = m_idColumn.toUpperCase();
        query.append(m_DataBaseConnection.maskKeyword(m_idColumn));
        query.append(" ");
        query.append(m_createInt);
        query.append(" PRIMARY KEY,");
      }
开始并完成字段配置
      for(int i = 0;i < structure.numAttributes(); i++){
          Attribute att = structure.attribute(i);
          String attName = att.name();
          attName = attName.replaceAll("[^\\w]","_");
          attName = m_DataBaseConnection.maskKeyword(attName);
          if(m_DataBaseConnection.getUpperCase())
              query.append(attName.toUpperCase());
          else
              query.append(attName);
          if(att.isDate())
              query.append(" " + m_createDate);
          else{
              if(att.isNumeric())
                  query.append(" "+m_createDouble);
              else
                  query.append(" "+m_createText);
          }
          if(i != structure.numAttributes()-1)
              query.append(", ");
      }
      query.append(" )");
执行sql语句,创建空表
      m_DataBaseConnection.update(query.toString());
关闭连接
      m_DataBaseConnection.close();
如果没有找到表,抛出异常
      if(!m_DataBaseConnection.tableExists(m_tableName)){
          throw new IOException("Table cannot be built.");
      }


增量插入方法
public void writeIncremental(Instance inst)

批插入方法
public void writeBatch()


  public void writeBatch() throws IOException {
 
获取数据集后,进行三次检查,确保数据集不等于空,数据库连接不等于空,执行模式不等于增量模式
      Instances instances = getInstances();
      if(instances == null)
          throw new IOException("No instances to save");
      if(getRetrieval() == INCREMENTAL)
          throw new IOException("Batch and incremental saving cannot be mixed.");
      if(m_DataBaseConnection == null)
           throw new IOException("No database has been set up.");

设置执行模式为批量写入
      setRetrieval(BATCH);
      try{
如果未连接,获取连接
          if(!m_DataBaseConnection.isConnected())
              connectToDatabase();
设置执行方式为写入
          setWriteMode(WRITE);
调用方法建表
          writeStructure();
遍历数据写入表
          for(int i = 0; i < instances.numInstances(); i++){
            writeInstance(instances.instance(i));
          }
关闭连接
          m_DataBaseConnection.disconnectFromDatabase();
设置执行方式为等待
          setWriteMode(WAIT);
将数据集指向空,施放内存
          resetStructure();
字段指针指向首位
          m_count = 1;
      } catch(Exception ex) {
            printException(ex);
       }   
  }

 

 

 

 

 


 

posted @ 2013-09-05 11:28  传说中那只猫  阅读(302)  评论(0编辑  收藏  举报