Not able to access freebase using GWT Application - java

I am developing a GWT application to get the query results from the Freebase. Now I am using the following code in my Service Implementation Class.
import com.freebase.api.Freebase;
import com.freebase.json.JSON;
import com.google.tracker.client.FreebaseService;
import com.google.tracker.client.freebaseapi.Freebase;
import com.google.tracker.client.freebasejson.JSON;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class FreebaseServiceImpl extends RemoteServiceServlet implements FreebaseService{
public String getDirectorName() throws IllegalArgumentException{
Freebase freebase = Freebase.getFreebase();
String query_str = "{" +
"'id': null," +
"'type': '/film/film'," +
"'name': 'Blade Runner'," +
"'directed_by': [{" +
"'id': null," +
"'name': null" +
"}]" +
"}​".replace('\'', '"');
JSON query = new JSON(query_str);
JSON result = freebase.mqlread(query);
#SuppressWarnings("unused")
String director = result.get("result").get("directed_by").get(0).get("name").string();
return director;
}
}
I am getting following error on running the application :
500 The call failed on the server; see server log for details.
What could be the possible reasons for these?

That code isn't even going to compile because you've got name conflicts with your imports (duplicate Freebase, JSON). You'll need to fix that before you can even get started.
Google doesn't, as far as I know, have anything that uses the namespace com.google.tracker. If that's your code from this question, you should change the package name to something in a namespace you control.
The client library that you're using uses the deprecated Freebase APIs. Since you're doing new development, you should be using the new APIs.
If you're still having problems after you fix all the basic stuff, update your question or post a new one.

You can use this client library to use the Freebase APIs
http://code.google.com/p/google-api-java-client/
Where did you get the library you are using ?
Documentation on the APIs is available here - note that you need to use the new APIs:
http://wiki.freebase.com/wiki/API

Related

Need to have console output stored in a database table

I created a code that counts the number of files in a zipfile. I am currently outputting the information onto the console. I am not sure how to get started in putting the outputted information into a database table in Microsoft SQL server. I essentially just need to have it output to a table in Microsoft SQL server instead of outputting it to the console. I have the code below:
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class KZF
{
static int findNumberOfFiles(File file) {
try (ZipFile zipFile = new ZipFile(file)) {
return (int) zipFile.stream().filter(z -> !z.isDirectory()).count();
} catch (Exception e) {
return -1;
}
}
static String createInfo(File file) {
int tot = findNumberOfFiles(file) - 1;
return (file.getName() + ": " + (tot >= 0 ? tot + " files" : "Error reading zip file"));
}
public static void main(String[] args) throws IOException {
String dirLocation = "C:\\Users\\username\\Documents\\Temp\\AllKo";
try (Stream<Path> files = Files.list(Paths.get(dirLocation))) {
files
.filter(path -> path.toFile().isFile())
.filter(path -> path.toString().toLowerCase().endsWith(".zip"))
.map(Path::toFile)
.map(KZF::createInfo)
.forEach(System.out::println);
}
}
To interact with SQL-based databases in java, the 'base layer' is a library called JDBC. This works as follows:
JDBC itself is part of plain java just as much as java.io.File is. However, this is just the basic API you use to interact with Databases, it doesn't include support for any specific database. Here is the API.
You then need a so-called JDBC Driver; you'd need the JDBC driver for Microsoft SQL server. This driver needs to be on the classpath when you run your app; you don't need to reference any particular class file or 'load' it, just... make sure it's on the classpath, that's all you need. This jar, if on the classpath, automatically tells the JDBC system about its existence, and the JDBC system will then use it when you ask the JDBC system to connect to your microsoft sql database. Hence, nothing required except for this to be present on the classpath.
JDBC is intentionally a convoluted and hard to use API from the point of view of interacting with DBs from plain jane java code: It's the lowest denominator; the 'machine code' aspect. It needs to expose all possible DB functionality for all possible SQL-based database engines and give you the tools to run it in all possible modes. Thus, I strongly advise you not to program direct JDBC. Instead, use libraries that are built on top of JDBC and give you a nice, easy to understand API: Use JDBI or JOOQ, but I believe JOOQ is not free unless you use it with a free DB, and microsoft SQL isn't free, so be aware you may need to pay a license fee for JOOQ. JDBI is free.
In other words:
in your build system, add the com.microsoft.sqlserver :: mssql-jdbc :: 9.2.1.jre11 dependency.
in your build system, add the org.jdbi :: jdbi3-core :: 3.20.0 dependency.
Read the Microsoft SQL Server JDBC connector URL docs on how to build the so-called 'JDBC URL' which tells java how to connect to your microsoft SQL server.
Read the JDBI documentation. It's not hard - right on the front page you see the basic layout for how to send INSERT statements. (the URL you learned about in the previous doc? You pass that to the Jdbi.create() call).
Much easier, you can use the entries() method to get an Enumeration of the ZipEntry-s in the zip-file, and check each one to see if it isDirectory():
int countRegularFiles(final ZipFile zipFile) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
int numRegularFiles = 0;
while (entries.hasMoreElements()) {
if (! entries.nextElement().isDirectory()) {
++numRegularFiles;
}
}
return numRegularFiles;
}

Does InfluxDB provide a Java API?

I can run DolphinDB scripts with DolphinDB Java API just like this:
import java.io.IOException;
import com.xxdb.DBConnection;
import com.xxdb.data.Vector;
public class HelloDolphinDB {
public static void main(String[] args) {
try{
DBConnection conn=new DBConnection();
boolean success=conn.connect("localhost",1220,"admin","123456");
System.out.println(success);
String script="n=10\n" +
"x=rand(10, n)\n" +
"y=rand(10, n)*2\n" +
"add(x,y) ";
Vector result = (Vector)conn.run(script);
System.out.println(result.getString());
}
catch(Exception e){}
}
}
and get the result:
true
[14,2,21,14,5,12,0,3,17,16]
wondering if InfluxDB provides a Java API for a quick connection to InfluxDB and run scripts easily.
Check the official documentation here for all programming languages and specifically for java check this out. Please note that these are community provided libraries for different programming languages so the version of InfluxDB supported is also worth considering.
AFAIK InfluxDB exposes a http API for interacting with it and these all libraries are wrapper over that.
For influx db 1.x you can use: https://github.com/influxdata/influxdb-java
For influx db 2.x use https://github.com/influxdata/influxdb-client-java

Unable to load AWS credentials Error when accessing dynamoDB (local) with java

I have installed the local version of dynamoDB, and set up a maven java project to access the DB. When i run the code i get the below error. Since i have installed the server in local (it runs son localhost:8000), i dont have any credentials to provide...
Any idea how to solve it?
import java.util.Iterator;
import org.apache.commons.cli.ParseException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.TableCollection;
import com.amazonaws.services.dynamodbv2.exceptions.DynamoDBLocalServiceException;
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
public class Test {
public static void main(String[] args) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
// we can use any region here
new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
//dynamoDB.listTables();
TableCollection<ListTablesResult> list = dynamoDB.listTables();
Iterator<Table> iterator = list.iterator();
System.out.println("Listing table names");
while (iterator.hasNext()) {
Table table = iterator.next();
System.out.println(table.getTableName());
}
System.out.println("over");
}
}
Error is
Exception in thread "main" com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain
at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:131)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1115)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:764)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:728)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:1831)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1807)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1123)
at com.amazonaws.services.dynamodbv2.document.internal.ListTablesCollection.firstPage(ListTablesCollection.java:46)
at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)
at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.nextResource(IteratorSupport.java:87)
at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.hasNext(IteratorSupport.java:55)
Stumbled upon this when I was searching for the same problem. After half a day of wasting time, managed to solve the issue. Posting here in case anyone ever stumbles upon such situation again.
And the worst part? The solution I had to pierce through and experiment after going through thousands of pages, you would expect there to be some info about the problem. At the least, the documentation should have mentioned some note!
The solution :
Configuring AWS Credentials : go through that to set up some credential. Configure it as any random thing, it doesn't really matter.
Yeah, this was it!!
And for those ppl who are still lazy (like me ;-) ) to go through that, just follow the easiest of the methods :
Open the default config file : ~/.aws/credentials
Change the values in it to anything (like empty string here)
[default]
aws_access_key_id=''
aws_secret_access_key=''
Run the program. You can thank me later :D
I had a similar issue. To get around when running tests locally, I have added few lines to set java System properties.
System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, "accesskey"); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, "secretkey");
As per the Amazon Web Services documentation, Working with AWS Credentials
Official supported Java system properties are:
aws.accessKeyId
aws.secretKey
The following sets these system properties:
System.setProperty("aws.accessKeyId", "super-access-key");
System.setProperty("aws.secretKey", "super-secret-key");
This needs to be set before creating the Amazon DynamoDB client.

Unable to import java.net.URLEncoder in groovy

I'm pretty new in groovy, trying to encode a URL in my first piece of groovy code.
Here is a section of my code:
import java.net.URLEncoder
String url = baseUrl + URLEncoder.encode(parameter);
It looks quite the same as many examples I check online but it throws error as below.
General error during canonicalization: Importing [java.net.URLEncoder] is not allowed
java.lang.SecurityException: Importing [java.net.URLEncoder] is not allowed
I have also tried to use the class directly as illustrated here
String url = baseUrl + java.net.URLEncoder.encode(parameter);
and another version:
String url = baseUrl + URLEncoder.encode(parameter);
both throw me error:
General error during canonicalization: Indirect import checks prevents usage of expression
java.lang.SecurityException: Indirect import checks prevents usage of expression
appreciate if any guru can help clear the doubts.
According to http://groovy.codehaus.org/Differences+from+Java, java.net.* package in Groovy is imported by default, which means java.net.URLEncoder is also imported. Use it without import.
Edit: For me, using this Groovy code:
println URLEncoder.encode("URL encoding fine!")
prints URL+encoding+fine%21

Getting "XQueryException", Unexpected token syntax error

I have created Xdbc database connection and run the sample program it was successful.
I have created class markLogics.java and imported the jar file marklogic-xcc-4.0.1.jar
Code Snapshot:
package com.marklogic;
import java.net.URI;
import java.net.URISyntaxException;
import com.marklogic.xcc.ContentSource;
import com.marklogic.xcc.ContentSourceFactory;
import com.marklogic.xcc.Session;
import com.marklogic.xcc.Request;
import com.marklogic.xcc.ResultSequence;
import com.marklogic.xcc.exceptions.RequestException;
import com.marklogic.xcc.exceptions.XccConfigException;**
class markLogics {
public static void main(String args[]) throws XccConfigException,
RequestException {
URI uri = null;
try {
uri = new URI("xcc://user:pwd#localhost:8008/Marklogics");
// uri=new URI("");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String queryResult = "";
ContentSource contentSource = ContentSourceFactory
.newContentSource(uri);
Session session = contentSource.newSession();
String queryStr = "let $uri := xdmp:get-request-field(\"uri\")"
+ "return"
+ "if (empty($uri) or $uri eq\"\") then"
+ "("
+ " xdmp:set-response-content-type(\"\text/html\"),"
+ "<ul>"
+ "{for $i in collection()"
+ "let $doc := document-uri($i) return"
+ "<li>"
+ "<a href="
+ "\"view.xqy?uri={xdmp:url-encode($doc)}\""
+ " >{$doc}</a></li>"
+ "}</ul>)"
+ "else ( xdmp:set-response-content-type(\"text/xml\"), if (empty(doc($uri)))"
+ "then <error>No content" + "</error> else doc($uri) )";
// String
// queryStr="let $uri := xdmp:get-request-field(\"uri\")for $v in $doc//uri $a in $doc//play";
Request request = session.newAdhocQuery(queryStr);
try {
ResultSequence rs = session.submitRequest(request);
System.out.println(rs.asString());
} catch (Exception e) {
e.printStackTrace();
}
session.close();
}
}
While executing the code, I get that exception :
com.marklogic.xcc.exceptions.XQueryException: XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax error, unexpected QName_on line 1
expr:
at com.marklogic.xcc.impl.handlers.ServerExceptionHandler.handleResponse(ServerExceptionHandler.java:31)
at com.marklogic.xcc.impl.handlers.EvalRequestController.serverDialog(EvalRequestController.java:68)
at com.marklogic.xcc.impl.handlers.AbstractRequestController.runRequest(AbstractRequestController.java:72)
at com.marklogic.xcc.impl.SessionImpl.submitRequest(SessionImpl.java:280)
at com.marklogic.markLogics.main(test.java:34)
Understanding:
According to my understanding in the query I have call use the view.xqy file that is throwing the exception.
Approach Follows:
I have tried the following approach to overcome this by archiving the the view.xqy and added into build path of the project, but it does not help me out.
Could you give me some guidance to overcome the hiccups?
At the moment I suspect that your problems have to do with Java string concatenation: for example, there is no whitespace between "return" and "if..." in queryStr. Each component of queryStr probably needs to begin or end with some whitespace.
But it may be better to take a step back and try a simpler approach, with native HTTP instead of Java. It looks to me like you are trying to build a web application, with an index page that renders a list of links. You will probably find that easier to do in pure HTTP.
So I would move your queryStr XQuery into an index.xqy file, and place that file in the modules root of an HTTPServer. Place view.xqy in the same location. Use an ordinary web browser to connect to http://HOSTNAME:PORT/, filling in the correct HOSTNAME and PORT.
Possibly you will need to integrate Java later on, but I think it will help your confidence if you get a simple HTTP application working first.
More reading:
http://developer.marklogic.com/learn/2009-01-get-started-apps
http://developer.marklogic.com/learn/2009-07-search-api-walkthrough
http://developer.marklogic.com/code/bill
I agree with the idea above that you are MUCH better off storing XQuery on the server and invoking it via HTTP or XCC.
The typical approach for XQuery generally and MarkLogic in particular is to store the code in the "root" area pointed to by an Application Server, then simply invoke the XQuery. Concatenating ad-hoc xquery as a string has a few problems:
It is hard to type - no syntax highlighting or other tooling
The XQuery must be re-parsed every time, rather than cached in an efficient form
You can't build a well-thought-out XQuery application that way. XQuery has modules for code organization, and you can search your XQuery separately for particular XML elements and attributes, as well as invoked functions if you separate your XQuery code and keep it on the server
XQuery is purely functional, so coding is naturally interactive and iterative, which is much easier in an interactive query buffer such as cq or queryConsole, which are packaged with MarkLogic.

Categories

Resources