How to run MapReduceIndexerTool job from Java? - java

I want to run MapReduceIndexerTool from Java.
Right now I do it from command line using hadoop jar as you can see here, but I want to check it's status (to see if it's finalized, in progress, etc.) from Java code.
So basically I want to run it from Java in order to be able to check it's status from Java. Is there a way to run it from command line and check it's status from Java?
Also, there is a way to make Map Reduce to send an event (on a callback for example) when a job is completed? Something like a webhook?

As far as I know Tool interface exposes only int run(String[] args) method, so in general you would create new instance, form proper argument string and call that method.
From other hand, MapReduceIndexerTool has int run(Options options) method, that could be used to run it without forming shell-style argument. However, this method is protected, so this will need to have calling class to be created in same package as MapReduceIndexerTool.

Related

Can a Jmeter Java Request can have Summary Report?

Is it possible for a Java request to have a summary report to it. I tried attaching
TPS listener, results tree, results table but could not see the report populated after running in jmeter.
It is not explicitly mentioned jmeter docs, but i assume, It should be supported.But i am not able to see it even after successful run of the test as seen from logs (runTest() method gets called successfully)
It is
The runTest() function is supposed to return a SampleResult and it's your job to call the necessary functions like:
create a new instance
call sampleStart() function when you want to start the measurement
call sampleEnd() function when you want to stop the measurement
call setSuccessful() function to mark the sampler as passed or failed
call setResponsecode() and setResponseData() functions to set response code/response body if needed
See JavaTest and SleepTest example implementations for reference.
You may also find JSR223 Sampler with Groovy easier to use (Java syntax should work in the majority of cases)

Verify behaviour based on app argument JUnit

What would be the correct way to verify that one behaviour is triggered when there is an argument being passed, and another behaviour is triggered when there are no arguments being passed when running a java app from cmd?
Since the main method is static it's a little tricky to verify, but I also feel that introducing PowerMock is a bit over the top just for that.
Basically I want to create an object with a constructor with no arguments if there are no cmd arguments, and create an object with a String argument constructor if there are passed arguments to the app.
I do not see your code, so I can only imagine how it looks like.
I can imagine that within the main method some logic is triggered, which results in one or another event.
I suggest thinking about moving the processing of the arguments to another class (ArgumentProcessor) which can be fed with a builder object or factory object in the constructor and it could have a process(String [] args) method that returns a runnable or whatever you want to achieve.
If you then feed the ArgumentProcessor with a stubbed builder/factory than I think it should be possible to check if the logic has been processed in the right way.

Passing context to the execute method in command design pattern

Going through the Command design pattern I understand that we need to create a Command by setting the context via constructor and then calling the execute method to perform some action on the context. Example:
public class Command implements ICommand {
Device device;
public Command(Device device) {
this.device = device;
}
public void execute() {
this.device.turnOn()
}
}
I was wondering that with this approach we will be required to create a new Command object for every device object we create. Will it be ok to pass the context and some parameters to the execute method? I am looking for something like:
public class Command implements ICommand {
public void execute(Device device) {
this.device.turnOn();
}
}
Are there any issues with this approach?
The idea behind the Command pattern is that it should encapsulate all the information needed to perform an action. This lets you do things like delay the execution of the action until a later time, or even undo the action after it has been executed.
For a concrete example, consider the "Undo" feature in a word processor.
Each time you type in the document, the application uses the Command pattern to record the action.
If you hit "Undo", the text you typed disappears.
Then, when you hit "Redo", the typed text reappears, without the application needing to ask for the input again. It does this by replaying the command that it stored in step 1, which contains all the information about the text you typed.
If your command object requires additional parameters in order to perform the action, it doesn't really implement the Command pattern. It loses most of the advantages of the Command pattern, because the caller can't execute the action without additional information. In your example, the caller would need to know which device is being turned on.
However, that doesn't mean that you have to stick rigidly to the pattern. If it's more useful in your case for the execute method to accept a Device parameter, that's what you should do! If you do that, you should consider renaming the interface, though. Referring to it as a Command pattern when it doesn't follow the pattern exactly could confuse other readers of the code.
When deciding whether to take an object in as a method parameter or a constructor parameter, one of the things I find most helpful is to consider how I'm going to test the application. Objects that form part of the initial setup of the test get passed in as constructor parameters, while objects that form the inputs of the test, or the test vector, are method parameters. I find that following that guideline helps to produce maintainable code.

Testcases implementation for Java similar to topcoder/codecheff

I wanted to implement a testcase which will run particular java class and then, provide input to it and takes output from that.
So, there are three questions here:
1) How do we run another java class from junit? Do we need to run command line to do this?
2) After java class is run, how do we provide input to it? Do we print that on console?
3) How to read output of program, do we read console here?
I haven't written many test cases, written simple test cases which will call a method in any java class and asserts that. Can anyone help me in doing this?
Note: this is neither a college/school home work nor this is related to company work...... :p
1) Testing code written in another class from a junit test method involves either creating an instance of that class and then calling its methods, or calling static methods of the class. If you want to run that class's main method (the method that gets run automatically when you run your java program from the command line), then just call MyClass.main(someArgs), where someArgs is the fake commandline arguments given as an array of Strings.
2) After main is called your program will run like normal. You can enter your input manually to test your program if you want. But since you are writing test methods you probably want to automate this...
3) You can use System.setIn and System.setOut to redirect input and output to your own InputStream and OutputStream. Then you can send "user" input straight from your test method, and make assertions based on the content of the OutputStream.

Server String Command Programming Architecture for simple game server..!

I am creating a board game with Project RedDwarf framework (old project DarkStar).
My question is this:
I need to have commands sent back and forth from the server to the client and reverse, and I need a solid programming architecture to incorporate on the command messaging service.
I thought of having a Command interface, and each subcommand would be an implementation of it (which holds the command String).
For instance lets say we need to check if a user is online. We have an interface called Command, then an interface called Check which extends Command, and finally we have the implementation called OnlineCheck.
OnlineCheck could have a method called getCommand and would return the commands String.
Ok till now.. BUT what I really wanna do is, include the possible replies on the same implementation class, so that I can check what the client replied to me, based on one of the pre defined replies.
How should I go about doing this?
Firstly, appreciate the use of command pattern.
OnlineCheck could have a method called getCommand and would return the commands String.
Command interface should have an entry point like execute() or run() that would implement what OnlineCheck should do. So there needn't be any getCommand() that in turn returns the command string which again needs to be interpreted.
Once you go down this path, the command instance (an instance of OnlineCheck in this case) could hold the response and can be sent back to the client. The client in turn would give this response back to the code block that generated it as it would be the best one to know what type of responses to expect for this command and can interpret them appropriately.
My two cents!

Categories

Resources