使用 Java 8 流 API 解析 JSONNode
Parse a JSONNode using Java 8 stream API
我正在使用 Java 从 Salesforce 环境中读取所有对象,它工作正常但下面的代码需要 10 分钟才能将 JSON 转换为 Java ArrayList。我在想我是否可以使用 Java - 8 stream API 来并行解析逻辑。以下是我的工作代码,欢迎任何建议。
/**
* @Desc : Get All available objects(tables) from salesforce
* @return : List<SalesforceObject>
* */
public List<SalesforceObject> getAllsObjects() {
List<SalesforceObject> listsObject = new ArrayList<SalesforceObject>();
try {
// query Salesforce
final URIBuilder builder = new URIBuilder(this.sfAccess.instanceURL);
builder.setPath(appProp.salesforceObjectPath);
final HttpGet get = new HttpGet(builder.build());
get.setHeader("Authorization", "Bearer " + this.sfAccess.token);
final CloseableHttpClient httpclient = HttpClients.createDefault();
final HttpResponse queryResponse = httpclient.execute(get);
// parse
final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
final JsonNode queryResults = mapper.readValue(queryResponse.getEntity().getContent(), JsonNode.class);
System.out.println(queryResults);
// This line takes - 10 mins
listsObject.addAll(mapper.convertValue(queryResults.get("sobjects"), new TypeReference<List<SalesforceObject>>(){}));
return listsObject;
} catch(IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
您正在查看,
return StreamSupport.stream(queryResults.get("sobjects").spliterator(), true)
.map(sObj -> mapper.convertValue(sObj, SalesforceObject.class))
.collect(Collectors.toList());
请注意,您的并发性仍将受到服务器 CPU 核心数量的限制。
我正在使用 Java 从 Salesforce 环境中读取所有对象,它工作正常但下面的代码需要 10 分钟才能将 JSON 转换为 Java ArrayList。我在想我是否可以使用 Java - 8 stream API 来并行解析逻辑。以下是我的工作代码,欢迎任何建议。
/**
* @Desc : Get All available objects(tables) from salesforce
* @return : List<SalesforceObject>
* */
public List<SalesforceObject> getAllsObjects() {
List<SalesforceObject> listsObject = new ArrayList<SalesforceObject>();
try {
// query Salesforce
final URIBuilder builder = new URIBuilder(this.sfAccess.instanceURL);
builder.setPath(appProp.salesforceObjectPath);
final HttpGet get = new HttpGet(builder.build());
get.setHeader("Authorization", "Bearer " + this.sfAccess.token);
final CloseableHttpClient httpclient = HttpClients.createDefault();
final HttpResponse queryResponse = httpclient.execute(get);
// parse
final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
final JsonNode queryResults = mapper.readValue(queryResponse.getEntity().getContent(), JsonNode.class);
System.out.println(queryResults);
// This line takes - 10 mins
listsObject.addAll(mapper.convertValue(queryResults.get("sobjects"), new TypeReference<List<SalesforceObject>>(){}));
return listsObject;
} catch(IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
您正在查看,
return StreamSupport.stream(queryResults.get("sobjects").spliterator(), true)
.map(sObj -> mapper.convertValue(sObj, SalesforceObject.class))
.collect(Collectors.toList());
请注意,您的并发性仍将受到服务器 CPU 核心数量的限制。