Can't connect to arangodb using Java API - java

I have a problem when accessing arangodb using Java driver.
When i run my application, it runs but neither it shows any response and nor it throws any error. Then i think it must be something on my connection to the remote arangodb server, and i tried to connect using Python arangodb, and it's success to connect.
Anyone knows what's the problem with my arangodb Java application? Is there any difference between java connector and python to arango? I'm using java driver arango 5.0.1
Here's what I've tried:
Java Driver :
public static void main(String[] args) {
// TODO Auto-generated method stub
#SuppressWarnings("deprecation")
ArangoDB arangodb = new ArangoDB.Builder().timeout(100).host("10.22.21.70",8599).user("xxx").password("xxx").useProtocol(Protocol.VST).build();
System.out.println("Test Start");
ArangoDatabase db = arangodb.db("data");
boolean tes = db.exists();
System.out.println("success");
arangodb.shutdown();
}
This program continues to run like this, without being terminated and throwing an error :
enter image description here
Using Python Arango :
import json
from arango import ArangoClient
from pyArango.connection import *
USER = 'xxx'
PASS = 'xxx'
HOST = '10.22.21.70'
DATABASE = 'data'
PORTT = 8599
client = ArangoClient(host=HOST,port=PORTT)
db = client.db(DATABASE,username=USER,password=PASS)
query = 'FOR s IN Email FILTER s.date>= "2018-11-07" LIMIT 0,2 RETURN s'
queryResult = db.aql.execute(query,batch_size=2,count=True)
res = queryResult.batch()
print(res)
And also i've tried to use telnet to the remote server, and it shows blank screen, which means no problem with the connection between my local host with the remote host.
Thanks

I'm using java driver arango 5.0.1
I had the same issue. Using v4.2.2 solved the problem. If someone could explain why drivers are not compatible, that would be great.

You are setting a timeout of 100ms, maybe you have higher network latencies while connecting to the remote server? To double check if this is the problem you can try without setting the timeout.

Related

How to specify database in SQL query with Java

I am trying to upload some data to a MySQL database using Java, as I'm working on an Android app. So far, this code compiles and runs, but is uploading nothing to my database. I assume because I'm not accessing the database "streakly" anywhere, but when I've searched around for a way to solve this I haven't found anything.
Snippet of code I'm trying to upload
String addStreakName = chooseName.getText().toString();
String addCategory = prefs.getString("Category", "");
String addToday = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
if(prefs.getInt("todayOrChosen", 1) == 1){
int addStartedZero = 0;
streakList.add(new Streak(addStreakName, addCategory, addToday, addStartedZero));
try {
Connection con = DriverManager.getConnection("127.0.0.1", "[myusername]", "[mypassword]");
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO `users`(userId, addStreakName,addCategory,addToday,addStartedZero) VALUE ('"+addStreakName+"', '"+addCategory+"', '"+addToday+"', '"+addStartedZero+"', '"+userID+"')");
} catch (SQLException e) {
e.printStackTrace();
}
}
You are trying to connect to localhost, which in your case is the android device. I am pretty sure there's no MySQL server running on your device, thus the problem you see. You can probably connect to a MySQL server using JDBC driver, but I really wouldn't recommend it. Mobile apps live on users devices, and having direct access to your database server is not such a good idea. If you need a local database, you can use SQLite, Couchbase Lite, Firebase, Realm, etc. If you want to then synchronize with a server, you can either implement an application server and sync via REST for example, or use a solution like Couchbase Lite+Sync Gateway or Google's Firebase
Regards,
Vladimir

java ignores proxy settings

I have set up a local proxy server for request logging but my java code ignores it and connects directly (Windows XP, JDK 1.7). Web browsers work with it. So I wrote test code for discussion that seems to connect directly even if a (bogus) proxy is specified. With the bogus proxy, I would expect connection failure but the code succeeds, connecting directly:
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "12345");
System.setProperty("http.nonProxyHosts", "noNonProxyHost.com");
URL url = new URL("http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html");
InputStream in = url.openStream();
System.out.println("Connection via bogus proxy succeeded");
The code is run as standalone Java, no Maven, no applet, no container. I have a direct internet connection.
In your case using java.net.URL(), if the proxy server cannot be reached at http.proxyHost and http.proxyPort then it simply falls back and tries to do a direct connect. If that succeeds, you'll see no exception thrown which is why your code works without error. You should see a pause while it tries to find the proxy though.
This sample code below happily fetches the URL and displays it, without error, even when run with bogus proxy settings. -Dhttp.proxyHost=bogus -Dhttp.proxyPort=2345 but will talk to my local proxy localhost port 8888 if set correctly
import java.io.*;
import java.net.URL;
import java.util.*;
public class URLClient {
private static String sUrl = "http://www.apache.org/";
public static void main(String[] args) {
try {
URL url = new URL(sUrl);
InputStream is = url.openStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
String output = s.hasNext() ? s.next() : "";
System.out.println(output);
} catch(Throwable e) {
System.err.println("exception");
}
}
}
The problem I was originally having with http.proxyHost and http.proxyPort being ignored (Google led me to your question) was that those settings are completely ignored by apache.commons.httpClient because it uses its own sockets, as described here.
http://cephas.net/blog/2007/11/14/java-commons-http-client-and-http-proxies/
I have faced a similar problem recently. First of all, one part of the above answer from Daemon42 explains pretty well, why the bogus proxy server didn't lead to a failure of the program:
if the proxy server cannot be reached at http.proxyHost and http.proxyPort then it simply falls back and tries to do a direct connect. If that succeeds, you'll see no exception thrown which is why your code works without error. You should see a pause while it tries to find the proxy though.
Still, your actual question was, why the proxy server configured via the operating system is not used by the Java application. As stated in the Oracle documentation (https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html), the system proxy settings are not evaluated by Java by default. To do so, you have to set the value of the system property "java.net.useSystemProxies" to the value "true".
You can set that system property on the command line, or you can edit the JRE installation file jre/lib/net.properties, that way you have to change it only once on a given system.

How to drop a remote graph using Java in OrientDB

I am trying to setup a remote OrientDB Server and I am trying to enter vertices into it from local Java code.
When I try the following code:
private static final void dropDb() {
OrientGraphNoTx graph = new OrientGraphNoTx(ORIENT_URL);
graph.drop();
}
I get an Exception saying:
Exception in thread "main" com.orientechnologies.orient.core.exception.ODatabaseException: Cannot delete database
...
Caused by: java.lang.UnsupportedOperationException: Cannot delete a database in a remote server. Please use the console or the OServerAdmin class.
How do I go about deleting a remote graph, using Java, in OrientDB?
He jackofblades,
the Graph answers your question:
Please use the console or the OServerAdmin class
// CREATE A SERVER ADMIN CLIENT AGAINST A REMOTE SERVER
OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/GratefulDeadConcerts").connect("admin", "admin");
serverAdmin.dropDatabase("GratefulDeadConcerts");
This is taken from the OrientDB Wiki Page
Patrick

Java Cannot Connect to MySQL Server

I am trying to write a program in Java that in order to work, needs to have access to a MySQL database. Below is the code for the program so far, with the I.P. address, username, and password removed for security reasons. The problem with this code is that whenever it is run, it always fails to connect to the server, even though I know that it is running and that the password that the login information is correct. My friend found a program online that checks to see if your database can be connected to, and whenever he runs it, it always outputs "Where is your MySQL JDBC Driver?" What is the MySQL JDBC driver? I am assuming that it is the cause of my problem, but I don't know that for sure. Can anyone explain this to me?
import java.sql.*;
public class main
{
public static void main(String[] args)
{
// Store the information to connect to the MySQL server in handy variables.
String url = "jdbc:mysql://(IP REMOVED FOR SAFETY):3307/";
String dbName = "attendance";
String driver = "com.mysql.jdbc.Driver";
String userName = "(USERNAME REMOVED FOR SAFETY)";
String password = "(PASSWORD REMOVED FOR SAFETY)";
// Now let's connect!
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
} catch (Exception e) {
System.out.println("Could not connect to database!");
}
}
}
The problem could be that MySQL driver is not in your classpath.
Please look at this: http://dev.mysql.com/doc/connector-j/en/connector-j-installing-classpath.html
The MySQL JDBC driver is called MySQL Connector/J. This jar needs to be added to the classpath for your program to run.
The driver can be downloaded from: http://dev.mysql.com/downloads/connector/j/

CDH4, Sqoop2 and JDBC drivers: no suitable driver found

I am trying to use Sqoop 2 to import data from a MySQL database to HDFS, basically following the instructions here. However, the Sqoop server is unable to make a connection to the MySQL database due to appropriate drivers not found.
Setup:
Here is some background on my setup:
Hadoop cluster: I have a three machine Hadoop cluster running CDH 4.4.0. Sqoop 2 was configured through the Cloudera Manager, and is running on the same machine as the Namenode.
I am developing on a Windows machine which is also where my MySQL database lives. The Hadoop cluster is a set of three Ubuntu Server machines.
MySQL database: I have a MySQL database running on my Windows machine, and I have checked that the MySQL database can be accessed from each of the machines in my Hadoop cluster.
Client application: My client application is an Eclipse project on my Windows machine which basically opens up a Sqoop client corresponding to a Sqoop server (I have verified that the Sqoop server and client are running on my Namenode).
Here is the main class of my client application.
package com.fc.SqoopImport;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.sqoop.client.*;
import org.apache.sqoop.*;
import org.apache.sqoop.common.*;
import org.apache.sqoop.model.*;
import org.apache.sqoop.validation.Status;
import com.mysql.jdbc.*;
public class SqoopImport {
// utlity function to cycle through the connector and framework forms for errors
private static void printMessage(List<MForm> formList) {
for(MForm form : formList) {
List<MInput<?>> inputlist = form.getInputs();
if (form.getValidationMessage() != null) {
System.out.println("Form message: " + form.getValidationMessage());
}
for (MInput minput : inputlist) {
if (minput.getValidationStatus() == Status.ACCEPTABLE) {
System.out.println("Warning:" + minput.getValidationMessage());
} else if (minput.getValidationStatus() == Status.UNACCEPTABLE) {
System.out.println("Error:" + minput.getValidationMessage());
}
}
}
}
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
// location of the server running Sqoop 2 server
String urlSqoop2Server = "http://fc-01.fc.com:12000/sqoop/";
SqoopClient clientSqoop2 = new SqoopClient(urlSqoop2Server);
// dummy connection object
MConnection sqoopConnSAP = clientSqoop2.newConnection(1);
MConnectionForms sqoopConnSAPFrameworkForm = sqoopConnSAP.getFrameworkPart();
MConnectionForms sqoopConnSAPConnForm = sqoopConnSAP.getConnectorPart();
sqoopConnSAP.setName("SqoopConnSAP");
// Set the values for the connection form
sqoopConnSAPConnForm.getStringInput("connection.connectionString").setValue("jdbc:mysql://192.168.31.172:3306/dbsap");
sqoopConnSAPConnForm.getStringInput("connection.jdbcDriver").setValue("com.mysql.jdbc.Driver");
sqoopConnSAPConnForm.getStringInput("connection.username").setValue("root");
sqoopConnSAPConnForm.getStringInput("connection.password").setValue("1234");
sqoopConnSAPFrameworkForm.getIntegerInput("security.maxConnections").setValue(10);
Status statusConnSAP = clientSqoop2.createConnection(sqoopConnSAP);
if(statusConnSAP.canProceed()) {
System.out.println("Created. New connection ID: " + sqoopConnSAP.getPersistenceId());
} else {
System.out.println("Check for status and forms errors.");
printMessage(sqoopConnSAP.getConnectorPart().getForms());
printMessage(sqoopConnSAP.getFrameworkPart().getForms());
}
}
}
Error:
Running this project gives the following error:
Check for status and forms errors.
Form message: Can't connect to the database with given credentials: No suitable driver found for jdbc:mysql:192.168.31.172:3306/dbsap
Error:Can't load specified driver
Diagnosis:
The appropriate JDBC drivers (mysql-connector-java-5.1.26-bin.jar) is part of my Eclipse project, and for good measure, I have added this to the sqoop2 lib folder
/opt/cloudera/parcels/CDH-4.4.0-1.cdh4.4.0.p0.39/lib/sqoop2/client-lib
as well. However, this is the part I am not sure of, since the CDH4 documentation says]1 that in case Sqoop was installed using Cloudera Manager, the location of the appropriate JDBC driver should be added to HADOOP_CLASSPATH. So, I did
export HADOOP_CLASSPATH=/usr/lib/jdbcJars:HADOOP_CLASSPATH;
on my Hadoop Namenode, so that an echo $HADOOP_CLASSPATH gives /usr/lib/jdbcJars. Again, I am not entirely sure of the utility of this since my client application is not being developed on the Hadoop cluster.
The last thing that I have not tried yet is creating a new /usr/lib/sqoop/lib directory and adding the JDBC driver there.
Any help figuring this out would be appreciated.
Never ever alter content of parcel directory (/opt/cloudera/parcels/*). There are always different ways how to configure components. For example based on the official documentation, you need to copy the MySQL JDBC driver into /var/lib/sqoop2 directory on the node where you are running Sqoop2 server.
put the mysql-jdbc-driver into the dir:
/usr/lib/sqoop2/webapps/sqoop/WEB-INF/lib/mysql-connector-java-5.1.25.jar
and restart the sqoop2 server

Categories