in gremlin/neptune-social-media-utils/src/main/java/com/aws/neptune/utils/generator/CSVGenerator.java [146:242]
public void writeEdgeCSVs(EdgeTypeBean type, String s3Bucket, String bucketFolder ) {
ArrayList<String> header = new ArrayList<String>();
S3Uploader s3Uploader = new S3Uploader();
//header.add("Left");
//header.add("Right");
//ejazs - per Amazon Neptune format
header.add("~id");
header.add("~label");
header.add("~from");
header.add("~to");
IdBean ids;
if (type.columns != null) {
header.addAll( type.columns.keySet());
}
try {
for (RelationBean relation: type.relations) {
/*Ex: <left-label>_<edgeType>_<right-label>_edges.csv */
String fileName = String.join("_",
relation.left,
type.name,
relation.right,
"edges.csv");
String filePath= "/tmp/" + fileName;
CSVPrinter csvFilePrinter = new CSVPrinter(new FileWriter(filePath), csvFileFormat);
//ejazs - printing the csv header
csvFilePrinter.printRecord(header);
IdStore idStore = new IdStore(idFactory, relation, type.multiplicity);
for (int i = 0; i < relation.row; i++) {
ArrayList<Object> record = new ArrayList<Object>();
ids = idStore.getRandomPairIdForRelation();
//ejazs - record list will be converted/flattened to a single record
//adding a unique id for each relationship to the list first
record.add(relation.left+ids.toArrayList().get(0)+type.name+relation.right+ids.toArrayList().get(1));
//then adding label for the relation
record.add(type.name);
//adding from and to node Ids
record.addAll(ids.toArrayList());
//properties if any for the edge/relation
if (type.columns != null) {
record.addAll(generateOneRecord(type.columns));
}
csvFilePrinter.printRecord(record);
}
//add supernodes if applicable
//these are defined only for select relations in .conf file provided as an argument
if (relation.supernode != null){
int numSuperV = relation.supernode.get("vertices");
int numE = relation.supernode.get("edges");
int minId = idFactory.getMinId(relation.left);
if ( numSuperV > 0 && numE > 0){
for ( int v = minId; v < minId + numSuperV; v ++){
for (int e = 0; e < numE; e++){
ArrayList<Object> record = new ArrayList<Object>();
ids = idStore.getRandomIdForRelation(v);
//ejazs- adding unique id
record.add(relation.left+ids.toArrayList().get(0)+type.name+relation.right+ ids.toArrayList().get(1));
//ejazs- adding label
record.add(type.name);
//adding from and to node Ids
record.addAll(ids.toArrayList());
//finally also appending properties on the edge/relation
if (type.columns != null) {
record.addAll(generateOneRecord(type.columns));
}
csvFilePrinter.printRecord(record);
}
}
}
}
csvFilePrinter.close();
System.out.println("Generated edge file: "+ filePath);
File data = new File(filePath);
s3Uploader.upload(data, s3Bucket, (bucketFolder == null ? fileName : bucketFolder+"/"+fileName));
System.out.println("Uploaded " + filePath + " to S3 location " + s3Bucket + (bucketFolder == null ? "" : "/"+bucketFolder));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}