2014-01-15 51 views
1

我希望能够将LDIF文件批量导入LDAP服务器。我有一个使用UnboundID LDAP SDK的工作实现(见下文)。问题在于它循环遍历LDIF中的每个条目,并且对于大型文件(数百万条目)会非常缓慢。是否有任何工具/ SDK可用于高速导入?我需要能够实现这个编程(最好是Java)。谢谢!以编程方式批量导入LDIF

public static void importLdif(){ 
try{ 
    LDAPConnection connection = new LDAPConnection("ldapserver.com", 389, 
      "uid=admin,ou=system", "secret"); 
    LDIFReader ldifReader = new LDIFReader("C:/Users/ejamcud/Desktop/LDAP/ldifs/Sailors.ldif"); 

    int entriesRead = 0; 
    int entriesAdded = 0; 
    int errorsEncountered = 0; 
    Entry entry; 
    LDAPResult addResult; 
    while (true) 
    { 
     try 
     { 
      entry = ldifReader.readEntry(); 
      if (entry == null) 
      { 
       System.out.println("All entries have been read."); 
       break; 
      } 

      entriesRead++; 
     } 
     catch (LDIFException le) 
     { 
      errorsEncountered++; 
      if (le.mayContinueReading()) 
      { 
       // A recoverable error occurred while attempting to read a change 
       // record, at or near line number le.getLineNumber() 
       // The entry will be skipped, but we'll try to keep reading from the 
       // LDIF file. 
       continue; 
      } 
      else 
      { 
       // An unrecoverable error occurred while attempting to read an entry 
       // at or near line number le.getLineNumber() 
       // No further LDIF processing will be performed. 
       break; 
      } 
     } 
     catch (IOException ioe) 
     { 
      // An I/O error occurred while attempting to read from the LDIF file. 
      // No further LDIF processing will be performed. 
      errorsEncountered++; 
      break; 
     } 

     try 
     { 
      addResult = connection.add(entry); 
      // If we got here, then the change should have been processed 
      // successfully. 
      System.out.println(entry.toLDIFString()); 

      entriesAdded++; 
     } 
     catch (LDAPException le) 
     { 
      // If we got here, then the change attempt failed. 
      le.printStackTrace(); 
      addResult = le.toLDAPResult(); 
      errorsEncountered++; 
     } 
    } 

}catch(IOException ioe){ 
    ioe.printStackTrace(); 
} 
catch(LDAPException lde){ 
    lde.printStackTrace(); 
}finally{ 
    //ldifReader.close(); 
} 
} 
+0

您可以将OpenLDAP客户端工具与任何符合要求的LDAP服务器一起使用。 – EJP

回答

2

它确实取决于您使用的目录服务器。某些服务器在特定条件下提供对批量添加的支持,因此您可能需要考虑您使用的服务器是否属于这种情况。但是如果你想要一些标准的LDAP,那么你最好的选择是使用多个线程来并行化向服务器添加条目的过程。

如果您添加的条目已经存在所有父条目(即,您没有添加分层结构,但只添加了叶条目),那么使用UnboundID LDAP SDK来并行化流程会非常简单跨多个线程。 LDIF阅读器已经支持使用多个线程来并行读取和解码LDIF记录的过程(使用允许指定解析线程数的LDIFReader构造函数),您可以将其与执行LDAP添加的LDIFReaderEntryTranslator结合使用已读过的条目。

如果您需要添加层次结构的数据,那么并行化该过程会更加复杂,因为您无法添加子项直到添加了其父项。但是,通过跟踪您当前添加的条目并使用某种锁定机制,您仍然可以实现相当好的并行性,因此,只有在完成添加父项之后才能添加子项。这可能不会像您不需要任何锁定一样快,但您仍然可以在不同的子树中并行添加。

+0

我目前使用Apacheds,但未来可能会使用其他几种类型的服务器。 – Sionnach733