使用UnboundID LDAP SDK导入LDIF时如何解决条目顺序问题?
我基于UnboundID LDAP SDK的内存目录服务器文档,使用以下代码启动内存LDAP服务器:
// Create the configuration to use for the server. InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com"); config.addAdditionalBindCredentials("cn=Directory Manager", "password"); config.setSchema(null); //!!! 不加这行会报错,参考Stack Overflow解决方案 // Create the directory server instance, populate it with data from the // "test-data.ldif" file, and start listening for client connections. InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); ds.importFromLDIF(true, "test-data.ldif"); ds.startListening(); // Get a client connection to the server and use it to perform various // operations. LDAPConnection conn = ds.getConnection(); SearchResultEntry entry = conn.getEntry("dc=example,dc=com"); // Do more stuff here.... // Disconnect from the server and cause the server to shut down. conn.close(); ds.shutDown(true);
我需要从现有LDAP服务器导入结构,使用Apache Directory Studio完成导出操作,将导出内容保存为test-data.ldif文件后,运行上述代码时出现错误:
Exception in thread "main" LDAPException(resultCode=32 (no such object), errorMessage='Unable to add entry 'CN=8437C3D8-7689-4200-BF38-79E4AC33DFA0,CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com' because its parent entry 'CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com' does not exist in the server.', matchedDN='DC=example,DC=com', ldapSDKVersion=6.0.9, revision=42839ddf0d77d954805fbbe3cce73a792af40474)
排查后发现,问题出在导出的LDIF文件中,子条目排在了父条目前面:
CN=8437C3D8-7689-4200-BF38-79E4AC33DFA0,CN=Operations,CN=DomainUpdates,CN=System,DC=example,DC=com
手动调整条目顺序,确保父条目先于子条目定义(如先定义DC=example,DC=com,再依次定义CN=System、CN=DomainUpdates、CN=Operations,最后定义子条目)后,导入成功。
现需求:
- 要么UnboundID LDAP SDK支持智能导入,自动按父条目优先的顺序处理LDIF文件
- 要么Apache Directory Studio支持导出时自动按父条目优先排序
内容的提问来源于stack exchange,提问作者gstackoverflow

