Java 以编程方式进行LDIF批量导入

Java 以编程方式进行LDIF批量导入,java,ldap,unboundid-ldap-sdk,Java,Ldap,Unboundid Ldap Sdk,我希望能够从LDIF文件到LDAP服务器进行批量导入。我有一个使用未绑定LDAP SDK的工作实现(如下)。问题是它在LDIF中循环遍历每个条目,对于大文件(数百万条条目)来说速度非常慢。是否有可用于高速导入的工具/SDK?我需要能够通过编程实现这一点(最好是java) 这实际上取决于您使用的目录服务器。某些服务器在某些情况下提供对批量添加的支持,因此您可能需要研究您正在使用的服务器是否支持批量添加。但是如果您想要标准的LDAP,那么最好的选择是使用多个线程来并行化向服务器添加条目的过程 如果要

我希望能够从LDIF文件到LDAP服务器进行批量导入。我有一个使用未绑定LDAP SDK的工作实现(如下)。问题是它在LDIF中循环遍历每个条目,对于大文件(数百万条条目)来说速度非常慢。是否有可用于高速导入的工具/SDK?我需要能够通过编程实现这一点(最好是java)


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

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


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

您可以将OpenLDAP客户端工具用于任何符合要求的LDAP服务器。目前我正在使用Apacheds,但将来可能会使用其他几种类型的服务器。
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();
}
}