001 /* 002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package de.kumpe.hadooptimizer.examples.analysis; 017 018 import java.io.PrintWriter; 019 020 import org.apache.commons.cli.Option; 021 import org.apache.commons.cli.Options; 022 import org.apache.hadoop.fs.FileSystem; 023 import org.apache.hadoop.fs.Path; 024 import org.apache.hadoop.io.LongWritable; 025 import org.apache.hadoop.io.ObjectWritable; 026 import org.apache.hadoop.io.SequenceFile; 027 import org.apache.hadoop.mapreduce.Reducer; 028 029 import de.kumpe.hadooptimizer.examples.Example; 030 import de.kumpe.hadooptimizer.examples.painting.SvgRenderer; 031 032 /** 033 * Convert the {@link Reducer} results from {@link SvgRenderer} to a text-based 034 * csv-file. 035 * 036 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a> 037 */ 038 public class Convert extends Example { 039 private static final String OPTION_FORCE = "f"; 040 041 @Override 042 public Options createOptions() { 043 final Options options = new Options(); 044 final Option overwriteOption = new Option(OPTION_FORCE, "force", false, 045 "overwrite existing outputfiles"); 046 options.addOption(overwriteOption); 047 return options; 048 } 049 050 @Override 051 protected void execute() throws Exception { 052 if (commandLine.getArgList().isEmpty()) { 053 throw new IllegalArgumentException("No input-file specified..."); 054 } 055 056 // read command-line 057 final boolean overwrite = commandLine.hasOption(OPTION_FORCE); 058 final String input = (String) commandLine.getArgList().get(0); 059 final String output = input + ".csv"; 060 061 log.info("Converting " + input + " to " + output); 062 063 // open input 064 final Path inputPath = new Path(input); 065 final FileSystem inputFileSystem = inputPath.getFileSystem(getConf()); 066 final SequenceFile.Reader reader = new SequenceFile.Reader( 067 inputFileSystem, inputPath, getConf()); 068 069 // open output 070 final Path outputPath = new Path(output); 071 final FileSystem outputFileSystem = outputPath.getFileSystem(getConf()); 072 final PrintWriter writer = new PrintWriter(outputFileSystem.create( 073 outputPath, overwrite)); 074 075 // read records and write lines 076 final LongWritable key = new LongWritable(); 077 final ObjectWritable value = new ObjectWritable(); 078 while (reader.next(key, value)) { 079 final double[] doubles = (double[]) value.get(); 080 for (final double d : doubles) { 081 writer.print(d); 082 writer.print(' '); 083 } 084 writer.println(); 085 } 086 writer.close(); 087 reader.close(); 088 } 089 090 @Override 091 protected String getVersionInfo() { 092 return "$Id: Convert.java 3985 2011-05-05 18:40:10Z baumbart $"; 093 } 094 }