[ckan-dev] [ckan-global-user-group] CKAN java library
Alessandro Portosa
alessandro.portosa at eng.it
Tue Jun 23 12:34:13 UTC 2015
Exactly. If you don't need (don't want to use...) Maven, you can easily
figure out which sources and libs are involved in ckan4j. For such small
project it is not a heavy task. I guess you can reorganize sources and
pull down jar in a really short time!
Anyway, I would also suggest to check this out:
http://opendatatrentino.github.io/jackan/
It has been made for the Open Data Portal of Trentino (Italy). I haven't
tested it (sadly I implemented my own Java client few months ago...),
but it seems to be quite good and updated. Plus, they provide a
non-maven download:
http://opendatatrentino.github.io/jackan/releases/jackan-0.3.3.zip
Have a look!
Alessandro.
Il 23/06/2015 11:29, Matthew Fullerton ha scritto:
> My advice with Maven is always to try Netbeans first; its quite good
> at just opening Maven projects as projects and figuring everything out.
>
> On 23 June 2015 at 10:44, Skaros Ilias <skaros.ilias at gmail.com
> <mailto:skaros.ilias at gmail.com>> wrote:
>
> Hi Matthew, Thanks for your reply.
> As a matter of fact i did try to use the python client using
> jython, but didnt work out for me. I finally turned to ckan4j,
> which is a java client, but my problem is that they use maven, and
> i am not familiar with it. I have some trouble setting it up, but
> it seems to be the best solution for me right now
>
> On Monday, June 22, 2015 at 2:35:32 PM UTC+3, Matthew Fullerton
> wrote:
>
> Me again. To answer your more specific issue with the Bad
> Request error: you need to send what you are sending as get
> parameters as keys within a JSON body. That is a POST request
> where the body is in JSON. You can play with the API using an
> extension like Postman in Chrome where you can set body type
> JSON and use the API docs as guidance. Whether httpget has a
> way to make post requests with JSON bodies, I don't know, but
> the code I sent can do it (append .post after the .header
> segment in the code:
> http://docs.oracle.com/javaee/7/api/javax/ws/rs/client/SyncInvoker.html#post(javax.ws.rs.client.Entity)
> <http://docs.oracle.com/javaee/7/api/javax/ws/rs/client/SyncInvoker.html#post%28javax.ws.rs.client.Entity%29>)
>
>
> --
> Matthew Fullerton
> Freelance Software Developer und EXIST Stipend holder with the
> start up project "Tapestry" - http://www.smartlane.de/
>
> On 22 June 2015 at 13:26, Matthew Fullerton
> <matt.fu... at gmail.com> wrote:
>
> ckan-dev in cc.
>
> Hmm, it might be simpler to just write a Java wrapper
> around https://github.com/ckan/ckanapi using Jython
> (http://www.jython.org/)
>
> However, if you want to start from scratch, I can maybe
> give you a little help to get started; this is how we
> access the API for the few things we need from inside Java
> (url is the API endpoint, you can include url arguments;
> apiKey is your API key, needed if the dataset is private):
>
> package package.name <http://package.name>;
>
> import javax.ws.rs.client.Client;
> import javax.ws.rs.client.ClientBuilder;
> import javax.ws.rs.client.WebTarget;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.Response;
>
> import org.json.JSONException;
> import org.json.JSONObject;
>
> public class CkanApiClient {
> private JSONObject getJsonResultForResource(String url,
> String apiKey) throws JSONException {
> JSONObject resourceProperties = null;
> Client client = ClientBuilder.newClient();
> WebTarget resourceTarget = client.target(url);
> //Set API key
> Response response =
> resourceTarget.request(MediaType.APPLICATION_JSON).header("X-CKAN-API-Key",
> apiKey).get();
> String responseString =
> response.readEntity(String.class);
> resourceProperties = new JSONObject(responseString);
> return resourceProperties;
> }
> }
>
> What's not in there is sending a JSON body which you will
> need for e.g. creating datasets.
>
> Best,
> Matt
>
> --
> Matthew Fullerton
> Freelance Software Developer und EXIST Stipend holder with
> the start up project "Tapestry" - http://www.smartlane.de/
>
>
> On 19 June 2015 at 23:33, Skaros Ilias
> <skaros... at gmail.com> wrote:
>
> Hi all,
> I am trying to create a java application that would
> eventually get use of the API and mainly upload
> files(csv and zip) to a ckan installation(currently a
> local installation under a VM).
> The problem is that i cant find any libraries that
> would help me do this. I tried to do it manually,
> using json and http commands but i hit a wall (more
> than once)
> I have found some that claim to be java libs, but
> either i cant get them to work, or they are not what
> they say they are.
>
> Mostly i think ckan clients are written in Python, the
> problem is that i dont know python. I would really
> love if there was a library that i could just use and
> do all the hard work for me.
>
> In case someone might be able to help this is the code
> I did in java
>
> |static String myApiKey="fa0499d1-ffda-4590-82b3-4afdb9c91576";
> static String uploadFileName="/home/ilias/2013/05/csv/2013_05_15.csv";
> public static void uploadFile()
>
> { HttpClient httpclient = new DefaultHttpClient();
>
> Date now=new Date();
> File file= new File(uploadFileName);
> httpclient= new DefaultHttpClient();
> try {
>
> SimpleDateFormat dateFormatGmt= new SimpleDateFormat("yyyyMMMddHHmmss");
> dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
> String date=dateFormatGmt.format(new Date());
> /* First we specify the data */
> HttpEntity reqEntity= MultipartEntityBuilder.create()
> .addTextBody("package_id","test2")
> .addTextBody("url",page)
> .setCharset(Charset.forName("utf-8"))
> .addTextBody("file", date+"/"+uploadFileName)
> .build();
> /* Set up the headers */
> HttpPost postRequest= new HttpPost(page+"/api/action/resource_create/");
> postRequest.setEntity(reqEntity);
> postRequest.setHeader("X-CKAN-API-Key", myApiKey);
> /* execute */
>
> System.out.println("request2:"+postRequest.toString()+"\n"+postRequest.getRequestLine().toString());
>
> Header[]a=postRequest.getAllHeaders();
> HttpResponse response= httpclient.execute(postRequest);
> /* get respond */
> int statusCode= response.getStatusLine().getStatusCode();
> BufferedReader br= new BufferedReader(
> new InputStreamReader((response.getEntity().getContent())));
>
> String line;
> while ((line= br.readLine()) != null) {
> System.out.println("__"+line);
> }
> if(statusCode!=200){
> System.out.println("statusCode ==" +statusCode);
> }
> }catch (IOException ioe) {
> System.out.println(ioe);
> } finally {
> httpclient.getConnectionManager().shutdown();
> }
>
> /*Upload the file */
> file= new File(uploadFileName);
> httpclient= new DefaultHttpClient();
> try {
> FileBody bin= new FileBody(file,ContentType.TEXT_PLAIN,now.toString()+file.getName());
>
> /* add the data */
> HttpEntity reqEntity= MultipartEntityBuilder.create()
> .addPart("file", bin)
> .addTextBody("key", now.toString()+file.getName())
> .build();
>
> HttpPost postRequest= new HttpPost(page+"/api/action/resource_create/");
> postRequest.setEntity(reqEntity);
> postRequest.setHeader("X-CKAN-API-Key", myApiKey);
> HttpResponse response= httpclient.execute(postRequest);
> int statusCode= response.getStatusLine().getStatusCode();
> BufferedReader br= new BufferedReader(
> new InputStreamReader((response.getEntity().getContent())));
>
> String line;
> while ((line= br.readLine()) != null) {
> System.out.println("+"+line);
> }
> if(statusCode!=200){
> System.out.println("statusCode =!=" +statusCode);
> }
> }catch (IOException ioe) {
> System.out.println(ioe);
> } finally {
> httpclient.getConnectionManager().shutdown();
> }
> }
>
>
> And the error i was getting is a 301 Error
>
> I also tried this, using httpGet
> ||URIBuilder builder= new URIBuilder();
> builder.setScheme("http").setHost("192.168.1.1:5000 <http://192.168.1.1:5000>").setPath("/api/action/resource_create/")
> .setCharset(Charset.forName("utf-8"))
> .setParameter("package_id", "test2")
> .setParameter("url", "http://www.example.com");
>
> URI uri= builder.build();
> HttpGet postRequest= new HttpGet(uri);
> postRequest.setHeader("X-CKAN-API-Key", myApiKey);
>
> Which returned a more understandable error of
> |
>
> "Bad request - JSON Error: No request body data"
>
> Any help/guideline is appreciated
>
>
> --
> You received this message because you are subscribed
> to the Google Groups "CKAN Global User Group" group.
> To unsubscribe from this group and stop receiving
> emails from it, send an email to
> ckan-global-user-group+unsubscribe at googlegroups.com.
> To post to this group, send email to
> ckan-global... at googlegroups.com.
> Visit this group at
> http://groups.google.com/group/ckan-global-user-group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com
> <https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
> https://groups.google.com/d/optout.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "CKAN Global User Group" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to
> ckan-global-user-group+unsubscribe at googlegroups.com
> <mailto:ckan-global-user-group+unsubscribe at googlegroups.com>.
> To post to this group, send email to
> ckan-global-user-group at googlegroups.com
> <mailto:ckan-global-user-group at googlegroups.com>.
> Visit this group at
> http://groups.google.com/group/ckan-global-user-group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/ckan-global-user-group/50232711-f7a4-4b81-975d-d9dcbafbedda%40googlegroups.com
> <https://groups.google.com/d/msgid/ckan-global-user-group/50232711-f7a4-4b81-975d-d9dcbafbedda%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
--
--
*Alessandro Portosa*
SpagoBI Consultant
*SpagoBI Labs
Engineering Group*
Via Marconi, 10 - 40125 Bologna - Italy
Tel. + 39 051 0435090
Skype: alessandro.portosa
www.spagobi.org <http://www.spagobi.org> - www.eng.it
<http://www.eng.it/web/eng_en/home>
Respect the environment. Please don't print this e-mail unless you
really need to.
The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material. Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited. If you
received this in error, please contact the sender and delete the
material from any computer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20150623/989adcd0/attachment-0003.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: spagobi.png
Type: image/png
Size: 3639 bytes
Desc: not available
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20150623/989adcd0/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.jpg
Type: image/jpeg
Size: 1379 bytes
Desc: not available
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20150623/989adcd0/attachment-0003.jpg>
More information about the ckan-dev
mailing list