All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.oodt.cas.pge.writers.CsvConfigFileWriter Maven / Gradle / Ivy

Go to download

Allows data processing jobs not written in conformance with the PCS PGE (Production Generation Executive) interface to be run within the PCS.

There is a newer version: 1.9.1
Show newest version
/*
 * 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.oodt.cas.pge.writers;

//Google static imports
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

//JDK imports
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.logging.Logger;

//Google imports
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

//OODT imports
import org.apache.oodt.cas.metadata.Metadata;

/**
 * CSV {@link SciPgeConfigFileWriter} which takes a comma separted list of
 * {@link String}s as the first index of the args and generates a CSV file from
 * the metadata fields listed. For example, if the list of {@link String}s given
 * is:
 *
 * 
 *    InputFiles,IsText
 * 
* * And: * *
 *    InputFiles=File1.txt,File2.dat,File3.xml
 *    IsText=true,false,true
 * 
* * Then the CSV file create will look like: * *
 *    InputFiles,IsText
 *    File1.text,true
 *    File2.dat,false
 *    File3.xml,true
 * 
* * @author bfoster (Brian Foster) */ public class CsvConfigFileWriter extends DynamicConfigFileWriter { private static final int HEADER_INDEX = 0; private static final int DELIM_INDEX = 0; private static final String DEFAULT_DELIM = ","; @Override public File generateFile(String filePath, Metadata metadata, Logger logger, Object... customArgs) throws Exception { checkArgument(customArgs.length > 0, CsvConfigFileWriter.class.getCanonicalName() + " has no args specified"); List header = checkNotNull( Lists.newArrayList(Splitter.on(",").split( (String) customArgs[HEADER_INDEX])), "Must specify CSV header in args at index = '" + HEADER_INDEX + "'"); String delim = DEFAULT_DELIM; if (customArgs.length > DELIM_INDEX) { delim = (String) customArgs[DELIM_INDEX]; } return writeCsvFile(filePath, header, generateRows(header, metadata), delim); } @VisibleForTesting protected List> generateRows(List header, Metadata meatadata) { List> rows = Lists.newArrayList(); int index = 0; TOP: while (true) { for (String columnName : header) { List values = meatadata.getAllMetadata(columnName); if (values.size() <= index) { break TOP; } List row = rows.get(index); if (row == null) { row = Lists.newArrayList(); } row.add(values.get(index)); rows.set(index, row); } } return rows; } @VisibleForTesting protected File writeCsvFile(String file, List header, List> rows, String delim) throws IOException { File outputFile = new File(file); PrintStream ps = null; try { ps = new PrintStream(new FileOutputStream(outputFile)); ps.println(Joiner.on(delim).join(header)); for (List row : rows) { ps.println(Joiner.on(delim).join(row)); } return outputFile; } catch (Exception e) { throw new IOException("Failed to write CSV file '" + file + "' : " + e.getMessage(), e); } finally { try { ps.close(); } catch (Exception ignore) { } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy