1

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

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();
}
}
4

1 回答 1

2

这实际上取决于您使用的目录服务器。某些服务器在某些条件下支持批量添加,因此您可能需要查看您正在使用的服务器是否属于这种情况。但是,如果您想要标准 LDAP,那么您最好的选择是使用多个线程来并行化将条目添加到服务器的过程。

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

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

于 2014-01-15T19:00:03.883 回答