Harry Sharma Harry Sharma - 29 days ago 8
Android Question

IO exception while inserting data in table on Big Query

I am trying to insert data in a table on big query.I followed few docs and my final code looks like

HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
List<String> SCOPES = Arrays
.asList(BigqueryScopes.BIGQUERY);

GoogleCredential credential = null;
AssetManager am = this.getAssets();
InputStream inputStream = null;
try {
inputStream = am.open("hbhjb-8f79f6642470.p12");
} catch (IOException e2) {
e2.printStackTrace();
}


File file = createFileFromInputStream(inputStream, this);


try {
credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("projectnum-*******************3ee3130770087ceab09a482173@developer.gserviceaccount.com")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(file)
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Bigquery bigquery = new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("BigQuery-Service-Accounts/0.1")
.setHttpRequestInitializer(credential).build();

TableRow data = new TableRow();
data.set("callerNumber", "123456789");


TableDataInsertAllRequest.Rows row = new TableDataInsertAllRequest.Rows();
row.setInsertId(System.currentTimeMillis()+"");
row.setJson(data);
TableDataInsertAllRequest requests = new TableDataInsertAllRequest();
requests.setRows(Arrays.asList(row));

TableDataInsertAllResponse response = null;
try {
response = bigquery.tabledata().insertAll("projectNum", "Data", "CallersData", requests).execute();
Log.d("big query response", response.toString());
} catch (IOException e) {
e.printStackTrace();
}

for (TableDataInsertAllResponse.InsertErrors err: response.getInsertErrors()) {
for (ErrorProto ep: err.getErrors()) {
Log.e("error",ep.getReason() + " : " + ep.getMessage() + " at " + ep.getLocation());
}
}
}


now tthe problem is that i am getting IO exception while inserting data i.e on this line

response = bigquery.tabledata().insertAll("projectNum", "Data", "CallersData", requests).execute();


I am trying to resolve this from last 2 days.If anyone here has implemented it successful please help me to find the issue.

Thanks in advance.

Following is the stacktrace

result = {java.io.IOException@830026649024} Method threw 'java.io.IOException' exception.
cause = {java.io.IOException@830026649024} "java.io.IOException"
detailMessage = null
stackState = null
stackTrace = {java.lang.StackTraceElement[30]@830026473856}
suppressedExceptions = {java.util.Collections$EmptyList@830023143656} size = 0

Answer

Try using the same code inside a different thread or a Asynctask. Let me know if you still face any issue.