Skip to content

Instantly share code, notes, and snippets.

@chrismetcalf
Created July 17, 2012 21:26
Show Gist options
  • Save chrismetcalf/3132216 to your computer and use it in GitHub Desktop.
Save chrismetcalf/3132216 to your computer and use it in GitHub Desktop.
Hawaii Developer Training Example
package gov.hawaii.data.demo;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.socrata.api.Connection;
import com.socrata.api.HttpConnection;
import com.socrata.api.RequestException;
import com.socrata.data.View;
import com.socrata.data.View.NewRow;
public class Demo {
/**
* @param args
* @throws RequestException
*/
public static void main(String[] args) throws RequestException {
Connection c = new HttpConnection("soda.demo.socrata.com", "chris@chrismetcalf.net", "training", "cvRn76AXhp6egL9w2OLSsDRhP");
View v = View.find("s4tk-4xpj", c);
// Create the working copy
v = v.copy(c);
// Create a new earthquake
List<NewRow> update = new ArrayList<View.NewRow>();
NewRow row = new NewRow();
System.out.println("occurred_at " + v.getColumnByApiIdentifier("occurred_at"));
row.putDataField(v.getColumnByApiIdentifier("source"), "socrata");
row.putDataField(v.getColumnByApiIdentifier("earthquake_id"), "1234567890");
// row.putDataField(v.getColumnByApiIdentifier("occurred_at"), new Date());
row.putDataField(v.getColumnByApiIdentifier("magnitude"), 3.4);
row.putDataField(v.getColumnByApiIdentifier("depth"), 112);
row.putDataField(v.getColumnByApiIdentifier("number_of_stations"), 1);
row.putDataField(v.getColumnByApiIdentifier("region"), "Seattle, WA (Socrata Offices, Please Save Us!!)");
Map<String, Double> location = new HashMap<String, Double>();
location.put("latitude", 47.60356369780794);
location.put("longitude", -122.32944013500594);
row.putDataField(v.getColumnByApiIdentifier("location"), location);
update.add(row);
// Call the API
View.BulkResults results = v.upsert(update, c);
System.out.println("Updates: " + results.getRowsUpdated());
System.out.println("Creates: " + results.getRowsCreated());
System.out.println("Deleted: " + results.getRowsDeleted());
System.out.println("Errors: " + results.getErrors());
// Publish our changes
v = v.publish(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment