Update Dataset from java


#1

i already having json data from other existing rest webservice.
I try to use below method and try to PUT data into existing dataset with json data. but below not
update the dataset. please let me know what i am doing wrong here.

public static void updateToDataset(){
try {

		    String url = "https://api.geckoboard.com/datasets/<datasetname>";
		 
		    URL obj = new URL(url);
		    HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
		 
		    conn.setRequestProperty("Content-Type", "application/json");
		  
		    conn.setDoOutput(true);
		 
		    conn.setRequestMethod("PUT");
		    conn.addRequestProperty("app_Id:", "my app id here");
		    
		  			   
		    final OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
		    String jsonObj = 	"{\"employerscreated\": 904,\"jobseekerscreated\": 1995,\"resumescreated\": 903}";


                 
                    
		    osw.write(jsonObj.toString());
		    osw.close();
		
		    } catch (Exception e) {
		    e.printStackTrace();
		    }
		 
}

#2

Hi @manivijayanand, thanks for asking. I see 3 issues.

		    String url = "https://api.geckoboard.com/datasets/<datasetname>";

This is the URL to create or delete a dataset. The URL to append/replace data should be

		    String url = "https://api.geckoboard.com/datasets/<datasetname>/data";
  1. For this line,
		    conn.addRequestProperty("app_Id:", "my app id here");

Are you trying to set the Basic Auth header here? If so, then you’ll want to do something like this instead:

String apiKey = "<your API key here>:";
String basicAuth = "Basic " + new String(Base64.encode(apiKey.getBytes(), Base64.NO_WRAP));
conn.addRequestProperty("Authorization", basicAuth);
  1. The payload you send should be structured as '{"data":[...records...]}' so this line:
		    String jsonObj = 	"{\"employerscreated\": 904,\"jobseekerscreated\": 1995,\"resumescreated\": 903}";

should be:

		    String jsonObj = 	"{\"data\":[{\"employerscreated\": 904,\"jobseekerscreated\": 1995,\"resumescreated\": 903}]}";

Give that a try and let me know how it works for you!


#3

Thanks Jason. This works as expected. properly updated dataset.


#4

Greetings!

I am facing a similar problem while creating a dataset from Java code (1.7). I get a 400 Bad request HTTP code. Here is my snippet:

    String datasetName = "TestDataset";
    String apiKey = config.getString("apikey");
    String jsonRequest = "{\"fields\":{\"amount\":{\"type\":\"number\",\"name\":\"Amount\",\"optional\":false},\"timestamp\":{\"type\":\"datetime\",\"name\":\"Date\"}},\"unique_by\":[\"timestamp\"]}";
    HttpURLConnection conn;
    try
    {
        URL url = new URL("https://api.geckoboard.com/datasets/" + datasetName);

        conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        
        String basicAuth = "Basic " + new String(DatatypeConverter.printBase64Binary(apiKey.getBytes()));
        conn.addRequestProperty("Authorization", basicAuth);
        
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(jsonRequest);
        out.flush();
        out.close();

        // read response
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        in.close();

        // close the connection
        conn.disconnect();
        conn = null;
    }

What might be the problem? I have noticed that a colon is added at the end of the API key before it is encoded. I have tried both with and without the colon and got the same result.


#5

Hi @andrejs.dasko,

In theory, I can’t see anything wrong with your code. When I copied it and ran it in my own IDE (albeit with my API key) it worked fine for me.

Would you mind running a test with entering your API key directly as the value for String apiKey = "" rather than using a method to collect it to see whether the issue is occurring there?

The 400 bad request error does imply that the issue is with the URL or request properties and these did work for me (I even ran it through a parsing system to see whether I’d altered anything else but all I changed was the API key).

The colon at the end of the API key can be added to indicate that there is no password required and the code should work fine either with or without it.

Lisa


#6

One other thing did just occur to me: Dataset names must be unique and you can only create a Dataset once so if TestDataset already exists within your account, you would see an error due to this.


#7

I am trying to get the API working to update a dataset with Java. I seem to have made a connection but I am getting a 404 error when trying to work with the demo dataset. Is anyone able to see what I am doing wrong?

This is my code

    final String encodedKey = Base64.getEncoder().encodeToString(APIKEY.getBytes(StandardCharsets.UTF_8));
	final String basicAuthorization = "Basic " + encodedKey;
	
	LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
	CloseableHttpClient httpClient = HttpClients.custom().setRedirectStrategy(redirectStrategy).build();
	
	String jsonUpdate = "{\"data\":[{\"category\":\"Biological\",\"cost\":27,\"destination\":\"turnpaugh\",\"gross_margin\":62,\"mass\":1200,\"order_time\":\"2016-01-01T12:00:00Z\",\"profit_margin\":0.35,\"revenue\":5,\"shipping_date\":\"2016-01-01T12:00:00Z\"}]}";
	String urlUpdate = "https://api.geckoboard.com/datasets/geckoboard.space.cargo/data";

	try {
		StringEntity jsonEntity = new StringEntity(jsonUpdate);
		HttpPut httpPost = new HttpPut(urlUpdate);
		httpPost.addHeader("Authorization", basicAuthorization);
		httpPost.setEntity(jsonEntity);
	
		System.out.println("Post URI Contents: " + httpPost.getURI());
		System.out.println("Post Entity Contents: " + EntityUtils.toString(httpPost.getEntity()));
		
		HttpResponse postResponse = httpClient.execute(httpPost);
		
		System.out.println("Response Status Line: " + postResponse.getStatusLine());
		System.out.println("Response Entity Contents: " + EntityUtils.toString(postResponse.getEntity()));

And this is my output.

Post URI Contents: https://api.geckoboard.com/datasets/geckoboard.space.cargo/data
Post Entity Contents: {“data”:[{“category”:“Biological”,“cost”:27,“destination”:“turnpaugh”,“gross_margin”:62,“mass”:1200,“order_time”:“2016-01-01T12:00:00Z”,“profit_margin”:0.35,“revenue”:5,“shipping_date”:“2016-01-01T12:00:00Z”}]}
Response Status Line: HTTP/1.1 404 Not Found
Response Entity Contents: {“error”:{“message”:“Dataset not found”}}

I don’t see why I am getting Dataset not found.

Any assistance would be appreciated.


#8

Hey @mturnpaugh, you need to first create your dataset, by sending a PUT request to https://api.geckoboard.com/datasets/:datasetid with the dataset schema. You can see more about doing that here – https://developer.geckoboard.com/api-reference/curl/#create

If you however have already done that, let me know and I can investigate further what might be going wrong!


#9

Good Morning @jason, I attempted an HttpPut and I am receiving an error 307 (Temporary Redirect).
Here is the connection info:
String urlCreate = "https://api.geckoboard.com/datasets/build.times/";
String jsonCreate = "{\"id\":\"build.times\",\"fields\":{\"buildtime\":{\"type\": \"number\",\"name\": \"Build Time\",\"optional\":true}}}";


#10

Hey @mturnpaugh, sorry about that! You’ll need to remove the trailing /

i.e.

 String urlCreate = "https://api.geckoboard.com/datasets/build.times";

I’ll see about fixing that, though!


#11

Response Status Line: HTTP/1.1 201 Created

I knew it was going to be something small. Thank you so much.


#12

Yay :slight_smile: Glad it’s working.