Skip to content

Instantly share code, notes, and snippets.

@tomblench
Last active March 8, 2016 13:19
Show Gist options
  • Save tomblench/da3709236282c52b18fb to your computer and use it in GitHub Desktop.
Save tomblench/da3709236282c52b18fb to your computer and use it in GitHub Desktop.
package cloudant.com.testsensorupload;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for showing messages to user
final TextView textView = ((TextView)findViewById(R.id.text_view));
// cloudant account settings
final String dbName = "my-database";
final String accountName = "my-account-name";
final String apiKey = "my-api-key";
// this is what our sensor readings look like
final String template = "{\"sensor-name\": \"gyro\", \"timestamp\": %d, \"value\": {\"att1\": %d, \"att2\": %d, \"att3\": %d}}";
textView.setText("Running...");
final Date then = new Date();
final int nRecords = 10000;
final Integer[] ids = new Integer[nRecords];
// prepare some ids 0..nRecords, these stand in for our real sensor data
for (int i=0; i<nRecords; i++) {
ids[i] = i;
}
new AsyncTask<Integer, Void, String>() {
@Override
protected String doInBackground(Integer... ids) {
// build up 0..nRecords sensor data in a StringBuilder
// (http://docs.couchdb.org/en/1.6.1/api/database/bulk-api.html#db-bulk-docs)
StringBuilder bulk = new StringBuilder();
bulk.append("{\"docs\": [");
for (int i=0; i<ids.length;i++) {
int id = ids[i];
if (i!=0) {
bulk.append(",");
}
bulk.append(String.format(template, new Date().getTime(), id, id * 2, id * id));
}
bulk.append("]}");
// POST to _bulk_docs
final String postUrl = String.format("https://%s.cloudant.com/%s/_bulk_docs", accountName, dbName);
final String auth = String.format("%s:%s", accountName, apiKey);
final String authEncode = new String(Base64.encode(auth.getBytes(), 0));
try {
final byte[] uploadJsonBytes = bulk.toString().getBytes("UTF-8");
HttpURLConnection connection = (HttpURLConnection) new URL(postUrl).openConnection();
connection.setRequestProperty("Authorization", String.format("Basic %s", authEncode));
connection.setRequestProperty("User-Agent", "home made uploader 0.1");
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(uploadJsonBytes.length);
connection.setRequestProperty("Content-type", "application/json");
connection.setDoOutput(true);
connection.getOutputStream().write(uploadJsonBytes);
// did we get a 201, 202?
if (connection.getResponseCode() / 100 != 2) {
return "Error, got code " + connection.getResponseCode();
}
} catch (Exception e) {
return "Exception "+e.getMessage();
}
// report timings
Date now = new Date();
double seconds = ((double)(now.getTime() - then.getTime())) / 1000.0;
double rate = nRecords / seconds;
return (String.format("Done, rate was %f docs/s", rate));
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}.execute(ids);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment