|
| 1 | +package hello.container; |
| 2 | + |
| 3 | +import org.apache.commons.logging.Log; |
| 4 | +import org.apache.commons.logging.LogFactory; |
| 5 | +import org.apache.hadoop.conf.Configuration; |
| 6 | +import org.apache.hadoop.fs.FileSystem; |
| 7 | +import org.apache.hadoop.fs.Path; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.yarn.annotation.OnContainerStart; |
| 10 | +import org.springframework.yarn.annotation.YarnComponent; |
| 11 | + |
| 12 | +import java.io.BufferedWriter; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.OutputStream; |
| 15 | +import java.io.OutputStreamWriter; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +@YarnComponent |
| 21 | +public class DigitsGeneratorSorter { |
| 22 | + |
| 23 | + private static final int NUMBER_OF_GENERATED_RANDOM_DIGITS = 100000; |
| 24 | + private static final int NUMBER_OF_DIGITS_WRITE_TO_HDFS = 100; |
| 25 | + private static final Log LOGGER = LogFactory.getLog(DigitsGeneratorSorter.class); |
| 26 | + private static final String OUTPUT_FILE_NAME = "result"; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private Configuration configuration; |
| 30 | + |
| 31 | + @OnContainerStart |
| 32 | + public void onContainerStart() throws Exception { |
| 33 | + List<Integer> integerList = createAndSortDigits(NUMBER_OF_GENERATED_RANDOM_DIGITS); |
| 34 | + writeRecordsToHDFS(integerList, NUMBER_OF_DIGITS_WRITE_TO_HDFS); |
| 35 | + } |
| 36 | + |
| 37 | + private List<Integer> createAndSortDigits(int numberOfDigits) { |
| 38 | + LOGGER.info("Creating list of " + NUMBER_OF_GENERATED_RANDOM_DIGITS + " sorted digits ..."); |
| 39 | + Random random = new Random(); |
| 40 | + List<Integer> list = new ArrayList<Integer>(); |
| 41 | + for (int i = 0; i < numberOfDigits; i++) { |
| 42 | + list.add(Math.abs(random.nextInt())); |
| 43 | + } |
| 44 | + |
| 45 | + LOGGER.info("Sorting created list of " + NUMBER_OF_GENERATED_RANDOM_DIGITS + " ..."); |
| 46 | + Collections.sort(list); |
| 47 | + LOGGER.info("Sorting created list of " + NUMBER_OF_GENERATED_RANDOM_DIGITS + " complete"); |
| 48 | + |
| 49 | + LOGGER.info("Creation list of " + NUMBER_OF_GENERATED_RANDOM_DIGITS + " sorted digits complete"); |
| 50 | + return list; |
| 51 | + } |
| 52 | + |
| 53 | + private void writeRecordsToHDFS(List<Integer> integerList, int numberOfIntegerToWrite) throws URISyntaxException, IOException { |
| 54 | + LOGGER.info("Writing " + numberOfIntegerToWrite + " in sorted list to HDFS in /" + OUTPUT_FILE_NAME + " ..."); |
| 55 | +// TODO can be enhanced via |
| 56 | +// String hostname = System.getenv("HOSTNAME"); |
| 57 | + |
| 58 | + String hostname = "172.17.0.2"; |
| 59 | + Configuration configuration = new Configuration(); |
| 60 | + FileSystem fileSystem = FileSystem.get(new URI("hdfs://" + hostname + ":9000"), configuration); |
| 61 | + Path file = new Path("hdfs://" + hostname + ":9000/" + OUTPUT_FILE_NAME); |
| 62 | + if (fileSystem.exists(file)) { |
| 63 | + fileSystem.delete(file, true); |
| 64 | + } |
| 65 | + |
| 66 | + OutputStream outputStream = fileSystem.create(file); |
| 67 | + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); |
| 68 | + // TODO can be rewritten via streams |
| 69 | + for (int i = 0; i < numberOfIntegerToWrite; i++) { |
| 70 | + bufferedWriter.write(String.valueOf(integerList.get(i))); |
| 71 | + bufferedWriter.write("\n"); |
| 72 | + } |
| 73 | + |
| 74 | + bufferedWriter.close(); |
| 75 | + fileSystem.close(); |
| 76 | + |
| 77 | + LOGGER.info("Writing " + numberOfIntegerToWrite + " in sorted list to HDFS in /" + OUTPUT_FILE_NAME + " complete"); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments