-
-
Save gquintana/20d761cf087c8e4d940b to your computer and use it in GitHub Desktop.
Générateur de données au format Bulk ElasticSearch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convertit
En