Skip to content

Instantly share code, notes, and snippets.

@gquintana

gquintana/DataToBulkJSONProvider.java Secret

Last active Dec 16, 2015
Embed
What would you like to do?
Générateur de données au format Bulk ElasticSearch.
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import com.github.tlrx.elasticsearch.test.provider.JSONProvider;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
public class DataToBulkJSONProvider implements JSONProvider {
private final JSONProvider dataProvider;
public DataToBulkJSONProvider(JSONProvider dataProvider) {
this.dataProvider = dataProvider;
}
@Override
public String toJson() {
StringWriter bulkWriter=new StringWriter();
PrintWriter printWriter=new PrintWriter(bulkWriter);
try {
// Analyse la source
Map<String,Object> rootObject=JsonXContent.jsonXContent.createParser(dataProvider.toJson()).mapAndClose();
String command=(String) rootObject.get("command");
String index=(String) rootObject.get("index");
String type=(String) rootObject.get("type");
List<Object> sourcesArray=(List<Object>) rootObject.get("sources");
// Produit le résultat au format bulk
for(Object source :sourcesArray) {
Map<String,Object> sourceObject=(Map<String,Object>) source;
Object id=sourceObject.remove("_id");
printWriter.println(createOrder(command, index, type, id));
printWriter.println(createData(sourceObject));
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
printWriter.close();
}
return bulkWriter.toString();
}
/**
* Crée un ligne d'ordre
*/
private String createOrder(String command, String index, String type, Object id) throws IOException{
XContentBuilder jsonXContentBuilder=JsonXContent.contentBuilder().startObject()
.startObject(command).field("_index", index);
if (type!=null) jsonXContentBuilder.field("_type",type);
if (id!=null) jsonXContentBuilder.field("_id",id);
return jsonXContentBuilder.endObject().endObject().string();
}
/**
* Crée une ligne de données
*/
private String createData(Map<String, Object> map) throws IOException {
return JsonXContent.contentBuilder().map(map).string();
}
public static void main(String[] args) {
DataToBulkJSONProvider jsonProvider=new DataToBulkJSONProvider(new JSONProvider() {
@Override
public String toJson() {
return "{ \"command\":\"index\"," +
"\"index\":\"contact\"," +
"\"type\":\"simple\"," +
"\"sources\":[" +
"{\"_id\":\"123\",\"foo\":\"456\",\"bar.qix\":\"789\"}," +
"{\"_id\":\"456\",\"foo\":\"789\",\"bar.qix\":\"012\"}" +
"]}";
}
});
System.out.println(jsonProvider.toJson());
}
}
@gquintana

This comment has been minimized.

Copy link
Owner Author

@gquintana gquintana commented Apr 22, 2013

Convertit

{ "command":"index","index":"contact","type":"simple","sources":[
    {"_id":"123","foo":"456","bar.qix":"789"},
    {"_id":"456","foo":"789","bar.qix":"012"}
]}

En

{ "index":{"_index":"contact","_type":"simple","_id":"123"}}
{"foo":"456","bar.qix":"789"}
{ "index":{"_index":"contact","_type":"simple","_id":"456"}}
{"foo":"789","bar.qix":"012"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment