![](/img/trans.png)
[英]Mapping primitive classes (String, Boolean, etc) to each other with Dozer
[英]Get the types (String, Boolean , Int, Nested etc ) of each field in the mapping with Java API
我想使用Java API獲取映射中所有字段的類型。
這樣,我可以檢索所有字段:
ClusterState cs = Client.admin().cluster().prepareState().execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(customerName);
MappingMetaData mmd = imd.mapping(type);
Map<String, Object> source = mmd.sourceAsMap();
for (String i : source.keySet()) {
JSONObject jsonSource = null;
try {
jsonSource = new JSONObject(source.get(i).toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<?> iterator = jsonSource.keys();
while (iterator.hasNext()) {
listFields.add(iterator.next().toString());
}
}
我在MappingMetaData中尋找方法,但沒有任何可以提供字段類型的信息(例如:字符串,浮點數,整數等)。 我只需要在字段列表中檢索那些屬於核心類型的字段(而不是嵌套或內部對象)
您可以使用Jackson的JsonNode:
MappingMetaData mmd = imd.mapping(type);
CompressedString source = mmd.source();
JsonNode mappingNode = new ObjectMapper().readTree(source));
JsonNode propertiesNode = mappingNode.get("properties");
Iterator<Entry<String, JsonNode>> properties = propertiesNode.fields();
while (properties.hasNext()) {
Entry<String, JsonNode> node = properties.next();
String name = node.getKey();
JsonNode valueNode = node.getValue();
if (valueNode != null) {
String type = valueNode.get("type").asText();//gives you the type
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.