Search in sources :

Example 6 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project zeppelin by apache.

the class PigInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    // remember the origial stdout, because we will redirect stdout to capture
    // the pig dump output.
    PrintStream originalStdOut = System.out;
    ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
    File tmpFile = null;
    try {
        pigServer.setJobName(createJobName(cmd, contextInterpreter));
        tmpFile = PigUtils.createTempPigScript(cmd);
        System.setOut(new PrintStream(bytesOutput));
        // each thread should its own ScriptState & PigStats
        ScriptState.start(pigServer.getPigContext().getExecutionEngine().instantiateScriptState());
        // reset PigStats, otherwise you may get the PigStats of last job in the same thread
        // because PigStats is ThreadLocal variable
        PigStats.start(pigServer.getPigContext().getExecutionEngine().instantiatePigStats());
        PigScriptListener scriptListener = new PigScriptListener();
        ScriptState.get().registerListener(scriptListener);
        listenerMap.put(contextInterpreter.getParagraphId(), scriptListener);
        pigServer.registerScript(tmpFile.getAbsolutePath());
    } catch (IOException e) {
        if (e instanceof FrontendException) {
            FrontendException fe = (FrontendException) e;
            if (!fe.getMessage().contains("Backend error :")) {
                // If the error message contains "Backend error :", that means the exception is from
                // backend.
                LOGGER.error("Fail to run pig script.", e);
                return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
            }
        }
        PigStats stats = PigStats.get();
        if (stats != null) {
            String errorMsg = PigUtils.extactJobStats(stats);
            if (errorMsg != null) {
                LOGGER.error("Fail to run pig script, " + errorMsg);
                return new InterpreterResult(Code.ERROR, errorMsg);
            }
        }
        LOGGER.error("Fail to run pig script.", e);
        return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
    } finally {
        System.setOut(originalStdOut);
        listenerMap.remove(contextInterpreter.getParagraphId());
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
    StringBuilder outputBuilder = new StringBuilder();
    PigStats stats = PigStats.get();
    if (stats != null && includeJobStats) {
        String jobStats = PigUtils.extactJobStats(stats);
        if (jobStats != null) {
            outputBuilder.append(jobStats);
        }
    }
    outputBuilder.append(bytesOutput.toString());
    return new InterpreterResult(Code.SUCCESS, outputBuilder.toString());
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException)

Example 7 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project eureka by Netflix.

the class DiscoveryJerseyProviderTest method testEncodingDecoding.

private void testEncodingDecoding(MediaType mediaType) throws IOException {
    // Write
    assertThat(jerseyProvider.isWriteable(InstanceInfo.class, InstanceInfo.class, null, mediaType), is(true));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    jerseyProvider.writeTo(INSTANCE, InstanceInfo.class, InstanceInfo.class, null, mediaType, null, out);
    // Read
    assertThat(jerseyProvider.isReadable(InstanceInfo.class, InstanceInfo.class, null, mediaType), is(true));
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    InstanceInfo decodedInstance = (InstanceInfo) jerseyProvider.readFrom(InstanceInfo.class, InstanceInfo.class, null, mediaType, null, in);
    assertThat(decodedInstance, is(equalTo(INSTANCE)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 8 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project quickstarts by jboss-switchyard.

the class JAXBUtil method marshal.

/**
     * Returns string representation of {@link GreetingRequest}.
     * 
     * @param rq Request.
     * @return XML representation.
     * @throws JAXBException
     */
public static String marshal(GreetingRequest rq) throws JAXBException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Marshaller marshaller = CONTEXT.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(rq, bos);
    return new String(bos.toByteArray());
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 9 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project k-9 by k9mail.

the class DeferredFileBodyTest method withShortData__writeTo__shouldWriteData.

@Test
public void withShortData__writeTo__shouldWriteData() throws Exception {
    writeShortTestData();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    deferredFileBody.writeTo(baos);
    assertArrayEquals(TEST_DATA_SHORT, baos.toByteArray());
}
Also used : ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Example 10 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.

the class ConsoleNote method encodeToBytes.

private ByteArrayOutputStream encodeToBytes() throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(buf));
    oos.writeObject(this);
    oos.close();
    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2, true, -1, null));
    buf2.write(PREAMBLE);
    dos.writeInt(buf.size());
    buf.writeTo(dos);
    dos.close();
    buf2.write(POSTAMBLE);
    return buf2;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream)

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)80 Test (org.junit.Test)36 DataOutputStream (java.io.DataOutputStream)15 IOException (java.io.IOException)14 HashSet (java.util.HashSet)13 ArrayList (java.util.ArrayList)12 Configuration (org.apache.hadoop.conf.Configuration)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 SparkConf (org.apache.spark.SparkConf)10 Edge (uk.gov.gchq.gaffer.data.element.Edge)10 Element (uk.gov.gchq.gaffer.data.element.Element)10 Entity (uk.gov.gchq.gaffer.data.element.Entity)10 Graph (uk.gov.gchq.gaffer.graph.Graph)10 User (uk.gov.gchq.gaffer.user.User)10 PrintStream (java.io.PrintStream)9 HashMap (java.util.HashMap)9 File (java.io.File)8 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)6