print console output in multiple sheets - java

I am using print stream to print my console output as a result. Below is my code:
printstream myconsole=new printstream(new fileoutpoutstream(filelocation));
system.setOut(myconsole)
myconsole.print("module"+","+"status"+","+"result");
Now this will create a new file with a single sheet and print module, status and result as a column header and all of my console will follow these; example:
search clicked pass will be following module status and result respectively.
I completed these for one of my module with 20 outputs. Now I wanna try it for another module. So I want to print my console output in same file but in different sheet. How should I do that? Could you please help me?

Related

Printing Spaces When Reading From a File

I am trying to print the contents of a file that is read using a while loop after the user selects it from the JChooser prompt window. The file is written in a program and contains an ID, first name, last name, title, and year of birth that is stored in a written data file as "000001BilboBagginsEsq.1020" I am trying to use another program to choose the data file print the contents to the console such that it displays as:
000001 Bilbo Baggins Esq. 1020
I want to use the printf statement to format the data that is read and stored in the "rec" variable in this manner, but when I pass in the data from the file it seems to ignore the printf format and prints the data like this in the console:
1 000001BilboBagginsEsq.1020
My program uses the JChooser to pick a file and print its contents using a while loop:
while(reader.ready())
{
rec = reader.readLine();
line++;
System.out.printf("\n%4d %-60s ", line, rec);
}
reader.close();
System.out.println("\n\nData file read!");
Any help on this would be appreciated.
I tried formatting the rec using printf, expecting the output to display:
000001 Bilbo Baggins Esq. 1020
But instead it still prints:
1 000001BilboBagginsEsq.1020

Apache Beam : Java : write results to multiple files using a variable in TextIO.write()

Need to write results to multiple output files and names of output files are generated at run time.
Example - Read from input csv that has students names and marks in each of the course. Output file has sum of marks for all the courses against student name.
Scenario 1 : Output results in a single csv - this is simple.
Input csv -
'''
ID,Name,Physics,Chemistry,Math,English,Biology,History
1001,James,56,60,78,67,50,70
1002,Robert,64,72,62,58,78,80
1003,Nina,72,70,83,72,75,85
Output csv -
Name,Total
Robert,414
Nina,457
James,381
Code snippet-
pipeline.apply(TextIO.read().from(options.getInputFilePath()))
.apply(ParDo.of(new FilterHeaderFn(CSV_HEADER)))
.apply(ParDo.of(new ComputeTotalScoresFn()))
.apply(ParDo.of(new ConvertToStringFn()))
.apply(TextIO.write().to(options.getOutputFilePath())
.withHeader("Name,Total").withNumShards(1));
Scenario 2: Output results in multiple files such that each student has a file created with their name and it contains total marks. Note - file names are generated at run time.
example -
Robert.csv => contains 414
Nina.csv => 457
James.csv => 381
I tried setting output file path inside
Code snippet -
`ProcessContext.getPipelineOptions().as(MyOptionClass.class).setOuputPath(output_path)`
but that doesn't work. Any help is appreciated.
You can use a dynamic file destination to achieve this, or a more generic Runtime value provider

Apache Beam - Reading JSON and Stream

I am writing Apache beam code, where I have to read a JSON file which has placed in the project folder, and read the data and Stream it.
This is the sample code to read JSON. Is this correct way of doing it?
PipelineOptions options = PipelineOptionsFactory.create();
options.setRunner(SparkRunner.class);
Pipeline p = Pipeline.create(options);
PCollection<String> lines = p.apply("ReadMyFile", TextIO.read().from("/Users/xyz/eclipse-workspace/beam-prototype/test.json"));
System.out.println("lines: " + lines);
or I should use,
p.apply(FileIO.match().filepattern("/Users/xyz/eclipse-workspace/beam-prototype/test.json"))
I just need to read the below json file. Read the complete testdata from this file and then Stream it.
{
“testdata":{
“siteOwner”:”xxx”,
“siteInfo”:{
“siteID”:”id_member",
"siteplatform”:”web”,
"siteType”:”soap”,
"siteURL”:”www”,
}
}
}
The above code is not reading the json file, it is printing like
lines: ReadMyFile/Read.out [PCollection]
, could you please guide me with sample reference?
This is the sample code to read JSON. Is this correct way of doing it?
To quickly answer your question, yes. Your sample code is the correct way to read a file containing JSON, where each line of the file contains a single JSON element. The TextIO input transform reads a file line by line, so if a single JSON element spans multiple lines, then it will not be parseable.
The second code sample has the same effect.
The above code is not reading the json file, it is printing like
The printed result is expected. The variable lines does not actually contain the JSON strings in the file. lines is a PCollection of Strings; it simply represents the state of the pipeline after a transform is applied. Accessing elements in the pipeline can be done by applying subsequent transforms. The actual JSON string can be access in the implementation of a transform.

printing the Decimal Value from table to CSV File

//Java Code to export as CSV the using FileWriter.
Java Code :
FileWriter fileWriterForCsv;
fileWriterForCsv.append(4.500);
fileWriterForCsv.append(",");
fileWriterForCsv.append("Mani");
fileWriterForCsv.append(",");
fileWriterForCsv.append("March");
// Content of the CSV File Exported mentioned below
Generated CSV
______________
Actual Result : 4.5,Mani,March
Expected Result : 4.500,Mani,March
Please let me know whether i need to change the java code?? or how to proceed to get the expected result as above mentioned
Also tried to change the column type as text in CSV template. Not getting the expected result.
Try formatting the number output. This should write the whole line with a CRLF at the end.
fileWriterForCsv.format("%.3f,%s,%s%n", 4.5, "Mani", "March");

How can I write the java console output in Excel sheet in same format

I am getting console output as below, I want to print in same format in Excel sheet. How can I do this?
Group ID Group Name Organization ID Organization Type User Limit
GP00000517001 SIPtest Site hostpool SIP00000517 Enterprise
GP8566747001 SIT mars test SIP te SIP8566747 Enterprise
You may use JExcelapi or apache POI, or any other library that is able to create excel files.
Just use this sample code to print your console text into excel.
Only thing you have to taken into consideration is that you have to print your text using delimters i.e comma(,).
int i = 0;
PrintStream printStream = new PrintStream(new FileOutputStream(new File("E:\\testconsole.csv")));
System.setOut(printStream);
while(i<1000){
System.out.print(i+++","+i+++","+i++);
System.out.println();
}
Please run above code you will get the idea. how you will achieve your scenario.

Categories