List of usage examples for com.mongodb Mongo setWriteConcern
@Deprecated public void setWriteConcern(final WriteConcern writeConcern)
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToAscii.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); // options.addOption(OptionBuilder // .withLongOpt("tags") // .withArgName( "property=value" ) // .hasArgs(2) // .withValueSeparator() // .withDescription( // "used to tag files when uploading.") // .create( "t" )); if (args.length == 0) { printHelpExit(options);//w w w . j a v a2 s . co m } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); } else { throw new IllegalArgumentException("You must specify an output filename"); } } catch (ParseException e) { e.printStackTrace(); printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); // boolean auth = db.authenticate(myUserName, myPassword); exportAscii(m, db, graphName, graphBranch, new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToGDF.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("c", "color-properties", true, "An (optional) '.properties' file that contains node type -> RGB mappings."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); // options.addOption(OptionBuilder // .withLongOpt("tags") // .withArgName( "property=value" ) // .hasArgs(2) // .withValueSeparator() // .withDescription( // "used to tag files when uploading.") // .create( "t" )); if (args.length == 0) { printHelpExit(options);// w w w .j ava 2 s .co m } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String colorPropsFilename = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); } else { throw new IllegalArgumentException("You must specify an output filename"); } if (line.hasOption("color-properties")) { colorPropsFilename = line.getOptionValue("color-properties", null); } } catch (ParseException e) { e.printStackTrace(); printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); // boolean auth = db.authenticate(myUserName, myPassword); Map<String, Color> nodeColorMappings = new HashMap<>(); if (colorPropsFilename != null) { nodeColorMappings.putAll(loadColorMappings(new File(colorPropsFilename))); } exportGdf(m, db, nodeColorMappings, graphName, graphBranch, new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToGephi.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); if (args.length == 0) { printHelpExit(options);// w w w . j a va2s .c o m } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); if (!outputFilename.contains(".gexf") && outputFilename.contains(".pdf") && outputFilename.contains(".svg") && outputFilename.contains(".gdf")) { throw new IllegalArgumentException( "You must specify an output filename with an extension " + "of [.pdf|.svg|.gexf|gdf]"); } } else { throw new IllegalArgumentException( "You must specify an output filename with an extension " + "of [.pdf|.svg|.gexf|gdf]"); } } catch (ParseException e) { printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); GraphCheckoutNamingScheme collectionNamer = new GraphCheckoutNamingScheme(graphName, graphBranch); DBCollection nodeCol = db.getCollection(collectionNamer.getNodeCollectionName()); DBCollection edgeCol = db.getCollection(collectionNamer.getEdgeCollectionName()); NodeDAO nodeDao = GraphDAOFactory.createDefaultNodeDAO(classLoader, m, db, nodeCol, edgeCol); EdgeDAO edgeDao = GraphDAOFactory.createDefaultEdgeDAO(classLoader, m, db, nodeCol, edgeCol); RevisionLog log = new RevisionLogDirectToMongoDbImpl(classLoader, m, db); MongoGraphToGephi exporter = new MongoGraphToGephi(nodeDao, edgeDao); exporter.exportGexf(new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.WriteTestObjects.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, JsonSerializerException, DbObjectMarshallerException { Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or//w w w . j av a 2 s . co m // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB("aries-test"); // boolean auth = db.authenticate(myUserName, myPassword); DBCollection testCol = db.getCollection("testCollection"); // BasicDBObject newDoc = new BasicDBObject(); // newDoc. // testCol.insert(newDoc); Node node = new Node(); // node.setNumericalId(3); node.setUid(UidGenerator.generateUid()); // node.setGraphUniqueId(UidGenerator.generateUid()); // node.getProperties().put("An_integer", 1); // node.getProperties().put("A String", "Foo"); Map<Integer, String> subMap = new HashMap<>(); subMap.put(1, "one"); subMap.put(2, "two"); subMap.put(3, "three"); // node.getProperties().put("A map", subMap); DbObjectMarshaller marshaller = ObjectMarshallerFactory.create(WriteTestObjects.class.getClassLoader()); DBObject dbObject = marshaller.serialize(node); testCol.insert(dbObject); System.out.println("Listing collections:"); listCollections(db); System.out.println("\n\nFindOne:"); findOne(testCol); System.out.println("\n\nCursor iteration:"); cursorIterateAllDocs(testCol); System.out.println("\n\nDone."); }