Java & Pig - Passing arguments to a pig script with PigServer - java

I have this java program, which executes a pig script in MapReduce mode. Here is the code:
import java.io.IOException;
import java.util.Properties;
import org.apache.pig.ExecType;
import org.apache.pig.PigServer;
import org.apache.pig.backend.executionengine.ExecException;
public class pigCV {
public static void main(String args[]){
PigServer pigServer;
try {
Properties props = new Properties();
props.setProperty("fs.default.name", "hdfs://hdfs://localhost:8022");
props.setProperty("mapred.job.tracker", "localhost:8021");
pigServer = new PigServer(ExecType.MAPREDUCE, props);
pigServer.registerScript("Desktop/text_v3.pig");
}
catch (ExecException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
}
Via the linux command line, I'm able to pass arguments to the pig script with a command like this:
pig -f "Desktop/text_v3.pig" -param param1=value1 -param2=value2
But with PigServer, I did not find how to do it.
Do you know how to resolve the problem ?
Thank you.

You can use this version of the registerScript method:
public void registerScript(String fileName, Map<String,String> params)
The java docs explanation is the following: "Register a pig script file. The parameters in the file will be substituted with the values in params."

Related

Tesseract image to searchable pdf in java

I am trying to convert the image to a searchable pdf using tesseract. The below command line option working fine for me.
Exploring a similar option in java. But not sure what to pass in the arguments. Below is my java code
import java.io.File;
import java.util.Arrays;
import java.util.List;
import net.sf.saxon.expr.instruct.ValueOf;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
public class Mask2 {
public static void main(String[] args) {
File image = new File("D:\\ML\\Java\\img3.PNG");
Tesseract tesseract = new Tesseract();
tesseract.setDatapath("C://Program Files//Tesseract-OCR//tessdata");
tesseract.setLanguage("eng");
tesseract.setPageSegMode(1);
tesseract.setOcrEngineMode(1);
try {
// Not sure what to pass in arguments
tesseract.createDocumentsWithResults()
} catch (TesseractException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any Suggestions / Solutions would be much helpful.
you can create a list of renderFormats like this ( you can add others)
List<RenderedFormat> renderFormats = new ArrayList<RenderedFormat>();
renderFormats.add(RenderedFormat.PDF);
and then you can pass the path of the input filename (PDF or IMG), the path of the output filename with no extension, and the render format you want to use.
tesseract.createDocuments("a/b/c/inputfile.PNG", "a/b/c/outputfile", renderFormats);
Ciao!

How can I execute python script which converts xml-json and vice versa in Java

I have written a java code which can execute the python script. Also, I have the Python script that converts xml-json and vice versa. how to pass the python xml method and python xml path in java code .Can someone help me with the java code to resolve this issue.
Below is the Py script and the java code which I am trying to execute to call the python script and its methods. Please help me with the java code.
Python script:
import json
from json import loads
import xmltodict
from dicttoxml import dicttoxml
def ConvertXMLtoJSON(xmltxt):
my_dict=xmltodict.parse(xmltxt)
json_data=json.dumps(my_dict)
print(json_data)
return json_data
def ConvertJSONtoXML(jsontxt):
json_obj = jsontxt
xml = dicttoxml(loads(json_obj), attr_type=False)
print(xml)
return xml
Java code:
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
interface PyFunction{
String XMLPath= ".xml file path";
public void ConvertXMLtoJSON(String XMLPath);
}
public class Conversion {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
try {
interpreter.exec("from PythonScript import ConvertXMLtoJSON");
PyObject ConvertXMLtoJSON = interpreter.get("ConvertXMLtoJSON");
PyFunction function = (PyFunction) ConvertXMLtoJSON.__tojava__(PyFunction.class);
function.ConvertXMLtoJSON("python script path");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
e.toString();
}

How to run NPM Command in Java using Process Builder

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class TestUnZip {
public static void main(String[] args) throws IOException, InterruptedException{
String destFolder="E:\\TestScript";
/*
* Location where the Nodejs Project is Present
*/
System.out.println(destFolder);
String cmdPrompt="cmd";
String path="/c";
String npmUpdate="npm update";
String npm="npm";
String update="update";
File jsFile=new File(destFolder);
List<String> updateCommand=new ArrayList<String>();
updateCommand.add(cmdPrompt);
updateCommand.add(path);
updateCommand.add(npmUpdate);
runExecution(updateCommand,jsFile);
}
public static void runExecution(List<String> command, File navigatePath) throws IOException, InterruptedException{
System.out.println(command);
ProcessBuilder executeProcess=new ProcessBuilder(command);
executeProcess.directory(navigatePath);
Process resultExecution=executeProcess.start();
BufferedReader br=new BufferedReader(new InputStreamReader(resultExecution.getInputStream()));
StringBuffer sb=new StringBuffer();
String line;
while((line=br.readLine())!=null){
sb.append(line+System.getProperty("line.separator"));
}
br.close();
int resultStatust=resultExecution.waitFor();
System.out.println("Result of Execution"+(resultStatust==0?"\tSuccess":"\tFailure"));
}
}
The Above Program works fine, but this program is depend on Windows Machine, I want to run the same program in other Machine as well.
1) NPM is a Command comes as a bundle of NodeJS. (I run NodeJS as a service, I have defined the Environment Variable, so I can run npm update command from any folder)
2) I can't find a work around to run the npm update command without using the "cmd", "/c". If I do I get following error
Exception in thread "main" java.io.IOException: Cannot run program "npm update" (in directory "E:\TestScript"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
3) Do we have option of Running the npm update command as a parameter of Node.exe. If so can anyone provide me the proper work around.
4) Same as I like, I use mocha framework to run the test script and result generates the .xml file.
5) I want mocha command also being invoked using process builder.
The problem is that ProcessBuilder does not respect the PATHEXT variable on Windows.
It's true there is no npm binary on Windows, there's a npm.cmd. My best solution is to check the platform. Something like this:
static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
static String npm = isWindows() ? "npm.cmd" : "npm";
static void run() {
Process process = new ProcessBuilder(npm, "update")
.directory(navigatePath)
.start()
}
In Unix or Linux os , the PathBuilder takes the default environment path , so we have to change the environment path and run the npm command through the bash.
import java.io.File;
import java.util.Map;
public class CommandExecutor {
public void exceuteCommand(String commandString,String
directoryToExecuteCommand) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(new String{"bash", "-c",commandString});
Map<String, String> env = processBuilder.environment();
processBuilder.directory(new File(directoryToExecuteCommand));
String envPath="/home/admin123/.nvm/versions/node/v10.15.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin";
env.put("PATH",envPath);
processBuilder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
CommandExecutor commandExecutor=new CommandExecutor();
commandExecutor.exceuteCommand("npm install", "/home/admin123/Desktop");
}
}

How to change the path in command prompt using java

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executor;
import com.jacob.com.LibraryLoader;
import autoitx4java.AutoItX;
public class SilentInstallation {
public static void main(String[] args) throws IOException, InterruptedException {
String[] cmd = { "C:\\WINDOWS\\system32\\cmd.exe", "/c", "start" };
try {
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec(cmd);
}
catch (java.io.IOException exception) {
System.out.println("Caught IOException: " + exception.getMessage());
}
}
}
Here is my code in which I am running command prompt using java. But the problem here I am facing is I can't be able to change the path in command prompt using java code.
Since this code is using in Automation, so is there any command or method in java that can be used to change the path in the command prompt.
I have also used ProcessBuilder to change the directory path.
Any Recommendations.....
This should be enough:
Process p = ...
p.getOutputStream().write("cd d:\\/r/n".getBytes());

Java Apache Commons CLI does not find setArgs()

I am trying to get multiple arguments passed to a java program. The Apache Commons CLI interface has been set up correctly and works. However, when I try to use
setArgs(Option.UNLIMITED_VALUES), it gives me an error
The method setArgs(int) is undefined for the type Options.
My code looks like this:
import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import org.apache.commons.cli.*;
public class main {
public static void main(String[] args) {
#SuppressWarnings("deprecation")
CommandLineParser parser = new BasicParser();
Options options = new Options();
options.setArgs(Option.UNLIMITED_VALUES);
options.addOption("p", true, "Program under test");
options.addOption("o", true, "Oracle");
options.addOption("n", true, "Number of test cases");
try {
CommandLine commandline = parser.parse(options, args);
System.out.println(commandline.getOptionValue("p"));
} catch (ParseException e) {
System.out.println("Command line failed.");
e.printStackTrace();
}
}
}
setArgs is a method related to Option not Options

Categories