Why does Jackson does not finish json creation from stream? - java

I am trying to write json from stream to another stream with base64 field like that:
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.File;
import java.io.FileInputStream;
public class Exmaple {
public static void main (String[] args) throws Exception {
final JsonGenerator generator = new JsonFactory().createGenerator(System.out);
generator.writeStartObject();
generator.writeFieldName("data");
generator.writeBinary(new FileInputStream(new File("D:\\somePath\\pdf.pdf")), -1);
generator.writeEndObject();
}
}
Instead of pdf.pdf you can use any file.
But on outut I got:
{"data":"JVBERi0xLjMKJcfsj6IKMzAgMCBvYmoKPD
No " at the end despite generator.writeEndObject() is called
No = or == at the end of base64
No } at the end of object
Why does it happen? How to fix this???

It is needed to add generator.close() call which leads to flush stream.

Related

Access and write data to an existing file in java

I hava the following class which creates a .txt file
and writes data in this file.
package junitexport;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.runner.JUnitCore;
public class ExecuteWithRunListener {
public static void main(String[] args) throws IOException {
String filename = "report.txt";
FileWriter fw = new FileWriter(filename,true);
fw.write("add a line");
fw.close();
JUnitCore runner = new JUnitCore();
runner.addListener(new ExecutionListener());
runner.run(MyUnitTest.class);
}
}
My question is how can I access the same file from another class.
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import junitexport.ExecuteWithRunListener;
public class ExecutionListener extends RunListener
{
/*
* Called when an atomic test is about to be started.
*/
public void testStarted(Description dsc) throws java.lang.Exception
{
//I want to write additional data to the previous file here
}
}
BR
As it is a text file, you can use Files.write()
public void testStarted(Description dsc) throws java.lang.Exception
{
Files.write(
Paths.get("report.txt"),
"the text you want to insert".getBytes(),
StandardOpenOption.APPEND
);
}
Use OO principles. Create a class to manage this functionality. Move your code from main into a public method and call this from both places.
Suggest a method to create the file and one to update the existing file
In your Unit Test source file, simple create a new instance of your newly created class and call the methods on this to test it.

Apache Camel - not moving file

I cannot figure out what I am doing wrong here. I have tried all sorts of things, including absolute paths, relative, enabling logging (which also doesnt seem to be working, using Main, using DefaultCamelContext, adding threadsleep, but I cannot get camel to move a file from one folder to another.
Here is my code:
package scratchpad;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.beanio.BeanIODataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.main.Main;
import org.apache.camel.spi.DataFormat;
public class CamelMain {
private static Main main;
public static void main(String[] args) throws Exception {
main = new Main();
main.addRouteBuilder( new RouteBuilder() {
#Override
public void configure() throws Exception {
// DataFormat format = new BeanIODataFormat(
// "org/apache/camel/dataformat/beanio/mappings.xml",
// "orderFile");
System.out.println("starting route");
// a route which uses the bean io data format to format a CSV data
// to java objects
from("file://input?noop=true&startingDirectoryMustExist=true")
.to("file://output");
}
});
//main.run();
main.start();
Thread.sleep(5000);
main.stop();
}
}
Can someone spot anything wrong with the above?
Thanks
You can for example read from the free chapter 1 for the Camel in Action book, as it has a file copied example it covers from top to bottom.
The pdf can be downloaded here: http://manning.com/ibsen/

Run a String through Java using Pig

I have a UDF jar which takes in a String as an input through Pig. This java file works through pig fine as running a 'hard coded' string such as this command
B = foreach f generate URL_UDF.mathUDF('stack.overflow');
Will give me the output I expect
My question is I am trying to get information from a text file and use my UDF with it. I load a file and want to pass data within that file which I have loaded to the UDF.
LoadData = load 'data.csv' using PigStorage(',');
f = foreach LoadData generate $0 as col0, $1 as chararray
$1 is the column I needed and researching data types (http://pig.apache.org/docs/r0.7.0/piglatin_ref2.html#Data+Types) a char array is used.
I then tryed using the following command
B = foreach f generate URL_UDF.mathUDF($1);
to pass the data into the jar which fails stating
java.lang.ClassCastException: org.apache.pig.data.DataByteArray cannot be cast to java.lang.String
If anybody has any solution to this that would be great.
The java code I am running is as follows
package URL_UDF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.pig.FilterFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.EvalFunc;
import org.apache.pig.PigWarning;
import org.apache.pig.data.Tuple;
import org.apache.commons.logging.Log;
import org.apache.*;
public class mathUDF extends EvalFunc<String> {
public String exec(Tuple arg0) throws IOException {
// TODO Auto-generated method stub
try{
String urlToCheck = (String) arg0.get(0);
return urlToCheck;
}catch (Exception e) {
// Throwing an exception will cause the task to fail.
throw new IOException("Something bad happened!", e);
}
}
}
Thanks
You can specify the schema with LOAD as follows
LoadData = load 'data.csv' using PigStorage(',') AS (col0: chararray, col1:chararray);
and pass col1 to the UDF.
Or
B = foreach LoadData generate (chararray)$1 AS col1:chararray;
Actually, this is a bug (PIG-2315) in Pig which will be fixed in 0.12.1. The AS clause in foreach does not work as one would expect.

NullPointerException when adding keys in BloomFilter

I used the hadoop apache to create a counting Bloom Filter. However I get a NullPointerException when I am trying to add keys in it. I tried to change the class structure in many ways but still I get the same result.
Here is the code I did:
package package_name;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.hadoop.util.bloom.*;
public class CBF {
public static CountingBloomFilter CBF = new CountingBloomFilter();
public static void countingFilter (ArrayList<byte[]> CBF_Keys) throws IOException{
CBF_Keys= Keys.keyStringArray;
Iterator<byte[]> iter = CBF_Keys.iterator();
while (iter.hasNext()) {
byte[] temp = iter.next();
Key hadoop_key = new Key(temp, 2.0);
CBF.add(hadoop_key);
}
}
}
problem is CBF = new CountingBloomFilter(). We should use CountingBloomFilter(int vectorSize, int nbHash, int hashType) instead here, otherwise the HashFunction will not be constructed in parent class Filter.

Error: Could not find or load main class- Novice

Hi I am a novice in JAVA. I have been getting this file not found exception inspite of the file existing in the very location I have specified in the path which is
Initially I had the issue of file not found. However, after performing a clean and re-run, now I am having an issue which says
Error: Could not find or load main class main.main
import Message.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
public class main{
public static void main(String[] args) {
Message msg=new Message("bob","alice","request","Data####");
MPasser passerObj=new MPasser("C:\\Workspace\\config.txt","process1");
}
}
Also in the MPasser Constructor the following piece of relevant code is there
public class MPasser(String file_name,String someVariable){
InputStream input;
try {
input =new RandomAccessFile(file_name,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Yaml yaml = new Yaml();
Map<String, String> Object = (Map<String, String>) yaml.load(input);
}
Sorry I have made edits from initial query so that it is more clear
On this line:
input = RandomAccessFile("C:\Workspace\conf.txt",'r');
You need to escape the \'s
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
"C:\Workspace\conf.txt"
Those are escape sequences. You probably meant:
"C:\\Workspace\\conf.txt"
You also appear to call it config.txt in one snippet and conf.txt in the other?
Make sure the java process has permissions to read the file.
You have to escape the backslash.
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and also
input = new RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and why you have two different filename conf.txt and config.txt.

Categories