error: com.fasterxml.jackson.databind.JsonMappingException: No serializer found
error: com.fasterxml.jackson.databind.JsonMappingException: No serializer found
我正在使用 getQueryResults 从 bigquery 作业中检索列表,并且(对我而言)似乎转换为 JSON 失败。
我找到了 this 并尝试使用如下注释:
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
虽然没有成功。
这里是一段代码:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;
import com.google.api.services.bigquery.model.*;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Query {
private String id;
private String sql;
private List<TableRow> results;
private static Bigquery createAuthorizedClient() throws IOException {
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
// Depending on the environment that provides the default credentials (e.g. Compute Engine, App
// Engine), the credentials may require us to specify the scopes we need explicitly.
// Check for this case, and inject the Bigquery scope if required.
if (credential.createScopedRequired()) {
credential = credential.createScoped(BigqueryScopes.all());
}
return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName(" ...OMITTED... ").build();
}
public String getId() { return this.id;}
public void setId(String id) { this.id = id; }
public String getSql() { return this.sql; }
public void setSql(String sql) { this.sql = sql;}
public List<TableRow> getResults() { return this.results; }
public void setResults(List<TableRow> r) { this.results = r; }
public void update() {
this.results = new ArrayList<TableRow>();
if (!this.sql.isEmpty()){
try {
Bigquery bq = createAuthorizedClient();
this.results = executeQuery(bq, this.sql);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private List<TableRow> executeQuery(Bigquery bq, String querySql) throws IOException {
QueryResponse query = bq.jobs().query(" ...OMITTED... ", new QueryRequest().setQuery(querySql)).execute();
// Execute it
GetQueryResultsResponse queryResult = bq.jobs()
.getQueryResults(query.getJobReference().getProjectId(), query.getJobReference().getJobId()).execute();
List<TableRow> rows = queryResult.getRows();
return rows;
}
我不熟悉注释,我不知道如何将 bigquery 的回复转换为 json,有人可以帮忙吗?
您必须自己构建 JSON 对象...
public void update() {
String r = "{\"rows\": [";
List<TableRow> bqresults;
if (!this.sql.isEmpty()){
try {
Bigquery bq = createAuthorizedClient();
bqresults = executeQuery(bq, this.sql);
int rowIx = 0;
int rowMax = bqresults.size() - 1;
for(TableRow row : bqresults) {
r += row.toString();
rowIx++;
if (rowIx <= rowMax) r += ",";
}
r += "]}";
this.results = r;
} catch (IOException e) {
e.printStackTrace();
}
}
}
我正在使用 getQueryResults 从 bigquery 作业中检索列表,并且(对我而言)似乎转换为 JSON 失败。 我找到了 this 并尝试使用如下注释:
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
虽然没有成功。
这里是一段代码:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;
import com.google.api.services.bigquery.model.*;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Query {
private String id;
private String sql;
private List<TableRow> results;
private static Bigquery createAuthorizedClient() throws IOException {
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
// Depending on the environment that provides the default credentials (e.g. Compute Engine, App
// Engine), the credentials may require us to specify the scopes we need explicitly.
// Check for this case, and inject the Bigquery scope if required.
if (credential.createScopedRequired()) {
credential = credential.createScoped(BigqueryScopes.all());
}
return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName(" ...OMITTED... ").build();
}
public String getId() { return this.id;}
public void setId(String id) { this.id = id; }
public String getSql() { return this.sql; }
public void setSql(String sql) { this.sql = sql;}
public List<TableRow> getResults() { return this.results; }
public void setResults(List<TableRow> r) { this.results = r; }
public void update() {
this.results = new ArrayList<TableRow>();
if (!this.sql.isEmpty()){
try {
Bigquery bq = createAuthorizedClient();
this.results = executeQuery(bq, this.sql);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private List<TableRow> executeQuery(Bigquery bq, String querySql) throws IOException {
QueryResponse query = bq.jobs().query(" ...OMITTED... ", new QueryRequest().setQuery(querySql)).execute();
// Execute it
GetQueryResultsResponse queryResult = bq.jobs()
.getQueryResults(query.getJobReference().getProjectId(), query.getJobReference().getJobId()).execute();
List<TableRow> rows = queryResult.getRows();
return rows;
}
我不熟悉注释,我不知道如何将 bigquery 的回复转换为 json,有人可以帮忙吗?
您必须自己构建 JSON 对象...
public void update() {
String r = "{\"rows\": [";
List<TableRow> bqresults;
if (!this.sql.isEmpty()){
try {
Bigquery bq = createAuthorizedClient();
bqresults = executeQuery(bq, this.sql);
int rowIx = 0;
int rowMax = bqresults.size() - 1;
for(TableRow row : bqresults) {
r += row.toString();
rowIx++;
if (rowIx <= rowMax) r += ",";
}
r += "]}";
this.results = r;
} catch (IOException e) {
e.printStackTrace();
}
}
}