sumitagrawl commented on code in PR #7465: URL: https://github.com/apache/ozone/pull/7465#discussion_r1853317064
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/KeyPathRetriever.java: ########## @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.debug; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.stream.Stream; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hdds.cli.SubcommandWithParent; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.db.Table.KeyValue; +import org.apache.hadoop.hdds.utils.db.TableIterator; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.helpers.OmBucketInfo; +import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.kohsuke.MetaInfServices; +import picocli.CommandLine; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +/** + * Tool that retrieve key full path from OM db file table with pattern match. + */ [email protected]( + name = "retrieve-key-fullpath", + description = "retrieve full key path with matching criteria") +@MetaInfServices(SubcommandWithParent.class) +public class KeyPathRetriever implements Callable<Void>, SubcommandWithParent { + @CommandLine.Spec + private static CommandLine.Model.CommandSpec spec; + + @CommandLine.Option(names = {"--db"}, + required = true, + description = "Database File") + private String dbFile; + + @CommandLine.Option(names = {"--containers"}, + required = false, + description = "Comma separated Container Ids") + private String strContainerIds; + + @CommandLine.Option(names = {"--container-file"}, + required = false, + description = "file with container id in new line") + private String containerFileName; + + @CommandLine.Option(names = {"--out", "-o"}, + required = false, + description = "out file name") + private String fileName; + + @Override + public Class<?> getParentType() { + return OzoneDebug.class; + } + + @Override + public Void call() throws Exception { + // get containerIds filter + Set<Long> containerIds = new HashSet<>(); + if (!StringUtils.isEmpty(strContainerIds)) { + if (!StringUtils.isEmpty(strContainerIds)) { + String[] split = strContainerIds.split(","); + for (String id : split) { + containerIds.add(Long.parseLong(id)); + } + } + } else if (!StringUtils.isEmpty(containerFileName)) { + try (Stream<String> stream = Files.lines(Paths.get(containerFileName))) { + stream.forEach(e -> containerIds.add(Long.parseLong(e))); + } + } + if (containerIds.isEmpty()) { + System.out.println("No containers to be filtered"); + return null; + } + // out stream + PrintWriter writer; + if (StringUtils.isEmpty(fileName)) { + writer = out(); + } else { + writer = new PrintWriter(new BufferedWriter(new PrintWriter(fileName, UTF_8.name()))); + } + + // db handler + OMMetadataManager metadataManager = getOmMetadataManager(dbFile); + if (metadataManager == null) { + writer.close(); + return null; + } + + try { + retrieve(metadataManager, writer, containerIds); + } finally { + writer.close(); + metadataManager.stop(); + } + return null; + } + + public void retrieve( + OMMetadataManager metadataManager, PrintWriter writer, Set<Long> containerIds) { Review Comment: https://issues.apache.org/jira/browse/HDDS-11777 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
