WritableStringObjectInspector cannot be cast to BooleanObjectInspector - java

Every time i run a hive query which has an OR condition from my java program, I get the following error,
Caused by: java.lang.ClassCastException: org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector
at org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPOr.initialize(GenericUDFOPOr.java:53)
at org.apache.hadoop.hive.ql.udf.generic.GenericUDF.initializeAndFoldConstants(GenericUDF.java:117)
at org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc.newInstance(ExprNodeGenericFuncDesc.java:232)
at org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory$DefaultExprProcessor.getXpathOrFuncExprNodeDesc(TypeCheckProcFactory.java:958)
at org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory$DefaultExprProcessor.process(TypeCheckProcFactory.java:1175)
at org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher.dispatch(DefaultRuleDispatcher.java:90)
at org.apache.hadoop.hive.ql.lib.DefaultGraphWalker.dispatchAndReturn(DefaultGraphWalker.java:94)
at org.apache.hadoop.hive.ql.lib.DefaultGraphWalker.dispatch(DefaultGraphWalker.java:78)
at org.apache.hadoop.hive.ql.lib.DefaultGraphWalker.walk(DefaultGraphWalker.java:132)
at org.apache.hadoop.hive.ql.lib.DefaultGraphWalker.startWalking(DefaultGraphWalker.java:109)
at org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory.genExprNode(TypeCheckProcFactory.java:192)
at org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory.genExprNode(TypeCheckProcFactory.java:145)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genAllExprNodeDesc(SemanticAnalyzer.java:10650)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genExprNodeDesc(SemanticAnalyzer.java:10606)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genExprNodeDesc(SemanticAnalyzer.java:10577)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genFilterPlan(SemanticAnalyzer.java:2736)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genFilterPlan(SemanticAnalyzer.java:2717)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genBodyPlan(SemanticAnalyzer.java:8972)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genPlan(SemanticAnalyzer.java:9884)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genPlan(SemanticAnalyzer.java:9777)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genOPTree(SemanticAnalyzer.java:10250)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeInternal(SemanticAnalyzer.java:10261)
at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeInternal(SemanticAnalyzer.java:10141)
at org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.analyze(BaseSemanticAnalyzer.java:222)
at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:430)
at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:305)
at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1123)
at org.apache.hadoop.hive.ql.Driver.compileAndRespond(Driver.java:1110)
at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:99)
... 26 more
But when i tried to run the following java program just to test the casting, it is cast successfully.
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector;
public class MyClass {
public static void main(String[] args) {
ObjectInspector[] arguments = new ObjectInspector[2];
BooleanObjectInspector boi0 = (BooleanObjectInspector) arguments[0];
BooleanObjectInspector boi1 = (BooleanObjectInspector) arguments[1];
System.out.println(boi1);
}
}
I have only one jar file which is hive-exec-5.5.4 in my classpath. So i dont see any jar conflicts. Any other solutions?

My Bad,
The Right end of the OR clause in the query did not have a condition after key. I have solved the issue.

Related

how to unit test code that should cause compile error

I have a class that take a ArrayList as parameter:
public class Foo {
private ArrayList<Bar> bars;
public Foo(ArrayList barList) {
bars = barList;
}
}
there is a bug that I can pass any ArrayList into the constructor:
// should compile error with this line
Foo foo = new Foo(new ArrayList<String>());
the problem is if I add this case to test suite, when the bug fixed, I can not compile it.
is there anyway to test this case?
I don't know of any language/unit test framework that will let you "test" for code that should not compile. If you can't compile it, there is nothing to test. You can however turn on all compiler warnings when building. I'm pretty sure passing an unparameterized collection is a big warning in any JVM after JDK5.
I feel it is bad practise and I don't really see a use for it, but see this example for how to test for compilations errors:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
#RunWith(JUnit4.class)
public class CompileTest {
//A very naive way checking for any error
#Test(expected=java.lang.Error.class)
public void testForError() throws Exception {
this.is.not.a.statement;
}
//An example which check that we only find errors for regarding compilation errors.
#Test
public void expectNotCompilableMethod() {
try {
uncompilableMethod();
fail("No compile error detected");
} catch (Error e) {
assertTrue("Check for compile error message", e.getMessage().startsWith("Unresolved compilation problems"));
}
}
private void uncompilableMethod() {
do.bad.things;
}
}
Edit:
1) I am not sure how this may behave together with build-tools like maven. As far as I know maven breaks the build at compile-errors, so probably the tests will not even be executed.
Fix your method signature:
public Foo(ArrayList<Bar> barList) {
bars = barList;
}
Problem solved. (You probably also want to check for null.)

call Windows Color System from Java

I need to call Windows Color System functions from Java. Following this tutorial i tried to call DLL function using Java Native Access. All examples from this tutorial works fine. When i try to load and use Mscms.dll (one of the WCS libraries) that DLL seems to be loaded successfully, but i can not call any functions. List of functions is here.
I got a message:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetColorDirectory'
What's wrong with my code? Could you help me please?
import com.sun.jna.Library;
import com.sun.jna.Native;
public class WCS_test {
public interface Mscms extends Library {
// http://msdn.microsoft.com/en-us/library/dd316928%28v=vs.85%29.aspx
boolean GetColorDirectory(String pMachineName, String[] pBuffer, int pdwSize);
}
private static Mscms mscms = (Mscms) Native.loadLibrary("C:/Windows/system32/Mscms.dll", Mscms.class);
public static void main(String[] args) {
if (mscms != null)
System.out.println("Library loaded\n");
else
System.err.println("Library loading error\n");
String[] pBuffer = new String[1024];
mscms.GetColorDirectory(null, pBuffer, pBuffer.length);
}
}
When you get a java.lang.UnsatisfiedLinkError that means that it could not find the function 'GetColorDirectory' inside of the Mscms.dll. Looking at the link from your source code http://msdn.microsoft.com/en-us/library/dd316928%28v=vs.85%29.aspx you should try the Unicode name GetColorDirectoryW.

Oculus Rift, simple example in java failing

I wrote this simple code:
public class Test {
public static void main(String args[]) {
OculusRift oculusRift = new OculusRift();
oculusRift.init();
HMDInfo hdmInfo = oculusRift.getHMDInfo();
System.out.println(hdmInfo);
// while(oculusRift.isInitialized()){
//
// }
oculusRift.destroy();
}
}
But I get this error
Exception in thread "main" java.lang.UnsatisfiedLinkError: de.fruitfly.ovr.OculusRift._initSubsystem()Z
at de.fruitfly.ovr.OculusRift._initSubsystem(Native Method)
at de.fruitfly.ovr.OculusRift.init(OculusRift.java:82)
at ec.test.test3.Test.main(Test.java:21)
Java Result: 1
I created and included a Library based on:
JRift-0.2.5.1.jar
JRiftLibrary-0.2.5.1.jar
JRiftLibrary-0.2.5.1-natives-windows.jar
From here
What is missing?
Edit:
Forced by extracting JRiftLibrary64.dll and adding its path explicitly to the java path
System.loadLibrary("JRiftLibrary64");
It works.. why does not work on the other way?
Have you tried JOVR? https://github.com/jherico/jovr
Here a complete example: https://github.com/jherico/jocular-examples
This library is simple, very solid and compatible with the current Oculus Runtime 1.8
Regards

Java can't find symbol from .class in the same directory

I have two files: MyStringLog.java (which extends ArrayStringLog) and TestDriver.java in the same directory. Why doesn't TestDriver.java recognize the MyStringLog constructor from the MyStringLog class? In TestDriver.java I declare:
MyStringLog animals = new MyStringLog("animals");
This is supposed to construct a new MyStringLog object named animals, but I get 2 errors when I compile, both, that MyStringLog symbol is not found.
ArrayStringLog.java: http://pastebin.com/Z624DRcm
MyStringLog.java:http://pastebin.com/zaH2S3yg
TestDriver.java:
public class TestDriver {
public static void main(String[] args) {
MyStringLog animals = new MyStringLog("animals");
animals.insert("dog");
}
}
I got this to roughly work. To do so...
for the file ArrayStringLog.java, I removed the implements StringLogInterface because I simply don't have access to that interface. After doing so, I removed the package ch02.stringLogs;. When I compiled this file with the package... still there, I recieved the error: Exception in thread "main" java.lang.NoClassDefFoundError: ArrayStringLog (wrong name: ch02/stringLogs/ArrayStringLog)
Then in MyStringLog.java, I removed the import ch02.stringLogs.*;. I then saved and compiled the code, and ran the TestDriver, to which I received no compilation errors.
This leads me to believe that your error stems from the package statement in ArrayStringLog.java.
To finally get a compilation, I put all four files (ArrayStringLog, MyStringLog, StringLogInterface, TestDriver) into the same directory, removed any package... statements, added back implements StringLogInterface to ArrayStringLog.java, compiled each one, and then ran TestDriver with an added toString method from which the output was:
Log: animals
1. dog
2. cat
Here was the test driver:
public class TestDriver {
public static void main(String[] args) {
MyStringLog animals = new MyStringLog("animals");
animals.insert("dog");
animals.insert("cat");
System.out.println(animals.toString());
}
}
To make clear, ArrayStringLog begins with:
public class ArrayStringLog implements StringLogInterface
try adding import MyStringLog; to your file.
Also are you using packages? In which case it should be import <package_name>.MyStringLog;

Post data from Matlab to Pachube (Cosm) using Java Methods

I am using JPachube.jar and Matlab in order to send data to my datastream. This java code works on my machine:
package smartclassroom;
import Pachube.Data;
import Pachube.Feed;
//import Pachube.FeedFactory;
import Pachube.Pachube;
import Pachube.PachubeException;
public class SendFeed {
public static void main(String arsg[]) throws InterruptedException{
SendFeed s = new SendFeed(0.0);
s.setZainteresovanost(0.3);
double output = s.getZainteresovanost();
System.out.println("zainteresovanost " + output);
try {
Pachube p = new Pachube("MYAPIKEY");
Feed f = p.getFeed(MYFEED);
f.updateDatastream(0, output);
} catch (PachubeException e) {
System.out.println(e.errorMessage);
}
}
private double zainteresovanost;
public SendFeed(double vrednost) {
zainteresovanost = vrednost;
}
public void setZainteresovanost(double vrednost) {
zainteresovanost = vrednost;
}
public double getZainteresovanost() {
return zainteresovanost;
}
}
but I need to do this from Matlab. I have tried rewriting example (example from link is working on my machine): I have compile java class with javac and added JPachube.jar and SendFeed.class into path and then utilize this code in Matlab:
javaaddpath('C:\work')
javaMethod('main','SendFeed','');
pachubeValue = SendFeed(0.42);
I get an error:
??? Error using ==> javaMethod
No class SendFeed can be located on Java class path
Error in ==> post_to_pachube2 at 6
javaMethod('main','SendFeed','');
This is strange because, as I said example from the link is working.
Afterwards, I decided to include JPachube directly in Matlab code and to write equivalent code in Matlab:
javaaddpath('c:\work\JPachube.jar')
import Pachube.Data.*
import Pachube.Feed.*
import Pachube.Pachube.*
import Pachube.PachubeException.*
pachube = Pachube.Pachube('MYAPIKEY');
feed = pachube.getFeed(MYFEED);
feed.updateDatastream(0, 0.54);
And I get this error:
??? No method 'updateDatastream' with matching signature found for class 'Pachube.Feed'.
Error in ==> post_to_pachube2 at 12
feed.updateDatastream(0, 0.54);
So I have tried almost everything and nothing! Any method making this work will be fine for me. Thanks for help in advance!
This done trick for me (answer from here)
javaaddpath('c:\work\httpcore-4.2.2.jar');
javaaddpath('c:\work\httpclient-4.2.3.jar');
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
httpclient = DefaultHttpClient();
httppost = HttpPost('http://api.cosm.com/v2/feeds/FEEDID/datastreams/0.csv?_method=put');
httppost.addHeader('Content-Type','text/plain');
httppost.addHeader('X-ApiKey','APIKEY');
params = StringEntity('0.7');
httppost.setEntity(params);
response = httpclient.execute(httppost);
I would rather use built-in methods. Matlab hasurlread/urlwrite, which could work if all you wish to do is request some CSV data from Cosm API. If you do need to use JSON, it can be handled in Matlab via a plugin.
Passissing the Cosm API key, that can be done via key parameter like so:
cosm_feed_url = "https://api.cosm.com/v2/feeds/61916.csv?key=<API_KEY>"
cosm_feed_csv = urlread(cosm_feed_url)
However, the standard library methods urlread/urlwrite are rather limited. In fact, the urlwrite function is only designed for file input, and I cannot even see any official example of how one could use a formatted string instead. Creating a temporary file would reasonable, unless it's only a few lines of CSV.
You will probably need to use urlread2 for anything more serious.
UPDATE: it appears that urlread2 can be problematic.

Categories