Search in sources :

Example 6 with PrintStream

use of java.io.PrintStream in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkWithPrefix.

@Test
public void testPrintSinkWithPrefix() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream stream = new PrintStream(baos);
    System.setOut(stream);
    final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
    Mockito.when(ctx.getNumberOfParallelSubtasks()).thenReturn(2);
    Mockito.when(ctx.getIndexOfThisSubtask()).thenReturn(1);
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
    printSink.setRuntimeContext(ctx);
    try {
        printSink.open(new Configuration());
    } catch (Exception e) {
        Assert.fail();
    }
    printSink.setTargetToStandardErr();
    printSink.invoke("hello world!");
    assertEquals("Print to System.err", printSink.toString());
    assertEquals("2> hello world!" + line, baos.toString());
    printSink.close();
    stream.close();
}
Also used : PrintStream(java.io.PrintStream) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 7 with PrintStream

use of java.io.PrintStream in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkStdOut.

@Test
public void testPrintSinkStdOut() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream stream = new PrintStream(baos);
    System.setOut(stream);
    final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
    printSink.setRuntimeContext(ctx);
    try {
        printSink.open(new Configuration());
    } catch (Exception e) {
        Assert.fail();
    }
    printSink.setTargetToStandardOut();
    printSink.invoke("hello world!");
    assertEquals("Print to System.out", printSink.toString());
    assertEquals("hello world!" + line, baos.toString());
    printSink.close();
    stream.close();
}
Also used : PrintStream(java.io.PrintStream) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 8 with PrintStream

use of java.io.PrintStream in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkStdErr.

@Test
public void testPrintSinkStdErr() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream stream = new PrintStream(baos);
    System.setOut(stream);
    final StreamingRuntimeContext ctx = Mockito.mock(StreamingRuntimeContext.class);
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
    printSink.setRuntimeContext(ctx);
    try {
        printSink.open(new Configuration());
    } catch (Exception e) {
        Assert.fail();
    }
    printSink.setTargetToStandardErr();
    printSink.invoke("hello world!");
    assertEquals("Print to System.err", printSink.toString());
    assertEquals("hello world!" + line, baos.toString());
    printSink.close();
    stream.close();
}
Also used : PrintStream(java.io.PrintStream) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 9 with PrintStream

use of java.io.PrintStream in project hadoop by apache.

the class ReflectionUtils method logThreadInfo.

/**
   * Log the current thread stacks at INFO level.
   * @param log the logger that logs the stack trace
   * @param title a descriptive title for the call stacks
   * @param minInterval the minimum time from the last 
   */
public static void logThreadInfo(Log log, String title, long minInterval) {
    boolean dumpStack = false;
    if (log.isInfoEnabled()) {
        synchronized (ReflectionUtils.class) {
            long now = Time.now();
            if (now - previousLogTime >= minInterval * 1000) {
                previousLogTime = now;
                dumpStack = true;
            }
        }
        if (dumpStack) {
            try {
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title);
                log.info(buffer.toString(Charset.defaultCharset().name()));
            } catch (UnsupportedEncodingException ignored) {
            }
        }
    }
}
Also used : PrintStream(java.io.PrintStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with PrintStream

use of java.io.PrintStream in project flink by apache.

the class TextInputFormatTest method testSimpleRead.

@Test
public void testSimpleRead() {
    final String FIRST = "First line";
    final String SECOND = "Second line";
    try {
        // create input file
        File tempFile = File.createTempFile("TextInputFormatTest", "tmp");
        tempFile.deleteOnExit();
        tempFile.setWritable(true);
        PrintStream ps = new PrintStream(tempFile);
        ps.println(FIRST);
        ps.println(SECOND);
        ps.close();
        TextInputFormat inputFormat = new TextInputFormat(new Path(tempFile.toURI().toString()));
        Configuration parameters = new Configuration();
        inputFormat.configure(parameters);
        FileInputSplit[] splits = inputFormat.createInputSplits(1);
        assertTrue("expected at least one input split", splits.length >= 1);
        inputFormat.open(splits[0]);
        String result = "";
        assertFalse(inputFormat.reachedEnd());
        result = inputFormat.nextRecord("");
        assertNotNull("Expecting first record here", result);
        assertEquals(FIRST, result);
        assertFalse(inputFormat.reachedEnd());
        result = inputFormat.nextRecord(result);
        assertNotNull("Expecting second record here", result);
        assertEquals(SECOND, result);
        assertTrue(inputFormat.reachedEnd() || null == inputFormat.nextRecord(result));
    } catch (Throwable t) {
        System.err.println("test failed with exception: " + t.getMessage());
        t.printStackTrace(System.err);
        fail("Test erroneous");
    }
}
Also used : Path(org.apache.flink.core.fs.Path) PrintStream(java.io.PrintStream) FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) File(java.io.File) Test(org.junit.Test)

Aggregations

PrintStream (java.io.PrintStream)1582 ByteArrayOutputStream (java.io.ByteArrayOutputStream)687 Test (org.junit.Test)481 File (java.io.File)276 IOException (java.io.IOException)257 FileOutputStream (java.io.FileOutputStream)177 ArrayList (java.util.ArrayList)78 FileNotFoundException (java.io.FileNotFoundException)75 OutputStream (java.io.OutputStream)72 Before (org.junit.Before)57 BufferedReader (java.io.BufferedReader)50 Date (java.util.Date)44 Map (java.util.Map)44 BufferedOutputStream (java.io.BufferedOutputStream)41 Path (org.apache.hadoop.fs.Path)41 UnsupportedEncodingException (java.io.UnsupportedEncodingException)40 Matchers.anyString (org.mockito.Matchers.anyString)37 InputStreamReader (java.io.InputStreamReader)35 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)35 HashMap (java.util.HashMap)32