List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:com.jaspersoft.mongodb.importer.MongoDbSimpleImporter.java
License:Open Source License
private void populate(MongoDbConnection connection, String collectionName, Resource scriptResource) throws JRException { DBCollection collection = null; DB mongoDatabase = null;/* w w w . ja v a2 s . co m*/ try { mongoDatabase = connection.getMongoDatabase(); if (!mongoDatabase.collectionExists(collectionName)) { logger.info("Collection \"" + collectionName + "\" doesn't exist"); DBObject options = new BasicDBObject("capped", false); collection = mongoDatabase.createCollection(collectionName, options); } else { logger.info("Collection \"" + collectionName + "\" exists"); collection = mongoDatabase.getCollectionFromString(collectionName); collection.drop(); logger.info("Collection \"" + collectionName + "\" was cleaned up"); } } catch (MongoException e) { logger.error(e); } if (mongoDatabase == null) { throw new JRException( "Failed connection to mongoDB database: " + connection.getMongoURIObject().getDatabase()); } FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; try { inputStreamReader = new InputStreamReader(scriptResource.getInputStream()); reader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String currentLine; while ((currentLine = reader.readLine()) != null) { stringBuilder.append(currentLine); } Object parseResult = JSON.parse(stringBuilder.toString()); if (!(parseResult instanceof BasicDBList)) { throw new JRException( "Unsupported type: " + parseResult.getClass().getName() + ". It must be a list"); } BasicDBList list = (BasicDBList) parseResult; List<DBObject> objectsList = new ArrayList<DBObject>(); for (int index = 0; index < list.size(); index++) { objectsList.add((DBObject) list.get(index)); } collection.insert(objectsList); logger.info("Collection count: " + collection.count() + "\nSuccessfully populated collection: " + collectionName); } catch (UnsupportedEncodingException e) { logger.error(e); } catch (IOException e) { logger.error(e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { logger.error(e); } } if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.javamongodb.application.JAVAMongoDBApplication.java
public JAVAMongoDBApplication() { panel.setLayout(new MigLayout()); lblTitle.setFont(font);//from w w w . java2 s.c om panel.add(lblTitle, "gapleft 4, wrap 25"); panel.add(lblFirstName, "gapleft 5, wrap"); panel.add(txtFirstName, "gapleft 30, wrap"); //, pushx, growx"); panel.add(lblMiddleName, "gapleft 5, wrap"); panel.add(txtMiddleName, "gapleft 30, wrap"); //, pushx, growx"); panel.add(lblLastName, "gapleft 5, wrap"); panel.add(txtLastName, "gapleft 30, wrap"); panel.add(lblGender, "gapleft 5, wrap"); maleRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { valueGender = ae.getActionCommand(); } }); femaleRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { valueGender = ae.getActionCommand(); } }); bg.add(maleRadioButton); bg.add(femaleRadioButton); panel.add(maleRadioButton, "gapleft 30, split2"); panel.add(femaleRadioButton, "wrap"); panel.add(lblCity, "gapleft 5, wrap"); panel.add(txtCity, "gapleft 30, wrap"); panel.add(lblStreet, "gapleft 5, wrap"); panel.add(txtStreet, "gapleft 30, wrap"); panel.add(lblBlockNumber, "gapleft 5, wrap"); panel.add(txtBlockNumber, "gapleft 30, wrap"); panel.add(lblCountry, "gapleft 5, wrap"); panel.add(txtCountry, "gapleft 30, wrap"); panel.add(lblEmailAddress, "gapleft 5, wrap"); panel.add(txtEmailAddress, "gapleft 30, wrap"); panel.add(lblMobileNumber, "gapleft 5, wrap"); panel.add(txtMobileNumber, "gapleft 30, wrap"); panel.add(lblHomeContact, "gapleft 5, wrap"); panel.add(txtHomeContact, "gapleft 30, wrap 25"); showButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { final JPanel pn = new JPanel(); pn.setLayout(new MigLayout()); JLabel label = new JLabel(messages.getString("enter.master.password")); JPasswordField pf = new JPasswordField(24); pf.addAncestorListener(new RequestFocusListener()); pn.add(label, "wrap"); pn.add(pf); boolean flag = false; while (!flag) { int okCxl = JOptionPane.showConfirmDialog(null, pn, "Password Required", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (okCxl == JOptionPane.OK_OPTION) { String password = new String(pf.getPassword()); if (password.equals("rs")) { flag = true; RecordsActivity recordsActivity = new RecordsActivity(); } else { JOptionPane.showMessageDialog(new JFrame(), "Incorrect password", "Conformation", JOptionPane.OK_OPTION); } } else { flag = true; } } } }); panel.add(showButton, "gapleft 3, split 3"); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { valueFirstName = txtFirstName.getText().trim(); valueMiddleName = txtMiddleName.getText().trim(); valueLastName = txtLastName.getText().trim(); valueCity = txtCity.getText().trim(); valueStreet = txtStreet.getText().trim(); valueBlockNumber = txtBlockNumber.getText().trim(); valueCountry = txtCountry.getText().trim(); valueEmailAddress = txtEmailAddress.getText().trim(); valueMobileNumber = txtMobileNumber.getText().trim(); valueHomeContact = txtHomeContact.getText().trim(); if (valueFirstName.isEmpty() || valueMobileNumber.isEmpty()) { JOptionPane.showMessageDialog(null, messages.getString("enter.minimum.field.values"), "Input Fields Empty", JOptionPane.WARNING_MESSAGE); } else { try { logger.debug("Opening database connection"); DBCollection collection = DatabaseUtils.openDBConnection(); BasicDBObject doc = new BasicDBObject("FirstName", valueFirstName) .append("MiddleName", valueMiddleName).append("LastName", valueLastName) .append("Gender", valueGender).append("City", valueCity) .append("Street", valueStreet).append("BlockNumber", valueBlockNumber) .append("Country", valueCountry).append("EmailAddress", valueEmailAddress) .append("MobileNumber", valueMobileNumber).append("HomeContact", valueHomeContact); collection.insert(doc); logger.debug("Object insertion succeeded"); } catch (Exception exception) { logger.error("Object insertion failed:\n" + exception.getMessage()); } finally { logger.debug("Closing database connection"); DatabaseUtils.closeDBConnection(); } JOptionPane.showMessageDialog(new JFrame(), "Data has been saved!", "Success Message", JOptionPane.INFORMATION_MESSAGE); } } }); panel.add(saveButton); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { txtFirstName.setText(""); txtMiddleName.setText(""); txtLastName.setText(""); bg.clearSelection(); txtCity.setText(""); txtStreet.setText(""); txtBlockNumber.setText(""); txtCountry.setText(""); txtEmailAddress.setText(""); txtMobileNumber.setText(""); txtHomeContact.setText(""); } }); panel.add(clearButton); frame.add(panel); frame.setSize(LayoutUtils.FORM_WINDOW_WIDTH, LayoutUtils.FORM_WINDOW_HEIGHT); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java
License:Apache License
public void addDocuments(AbstractDocument... documents) { DBCollection collection = getDBCollection(documents[0]); List<DBObject> dbObjects = new LinkedList<DBObject>(); for (AbstractDocument document : documents) { dbObjects.add(document.resolveToBasicDBObject()); }/* w w w.j ava2s .c om*/ WriteResult wr = collection.insert(dbObjects); processWriteResult(wr); }
From source file:com.jjorgemoura.hangmanz.model.ZDHangmanGame.java
public void persist() { if (this.dbEngine == null) { return;/*from w w w . j ava2 s .c o m*/ } //Prepare JSON Document from Entity DBObject docX = this.createDBObject(); //Prepare Collection DBCollection dbCollection = this.dbEngine.getCollection(ZDHangmanGame.DB_COLLECTION_ID); //Save - Write WriteResult dbInsertResult = dbCollection.insert(docX); //Finalise int n = dbInsertResult.getN(); Object upsertedId = dbInsertResult.getUpsertedId(); WriteConcern lastConcernObject = dbInsertResult.getLastConcern(); String s = "ok"; }
From source file:com.jopss.apostas.db.migration.CadastroPerfil.java
@ChangeSet(order = "001", id = "002.001", author = "apostas") public void criarPerfil(DB db) { DBCollection perfil = db.getCollection("perfil"); DBCollection permissao = db.getCollection("permissao"); BasicDBObject filtro = new BasicDBObject(); filtro.put("papel", new BasicDBObject("$in", Arrays.asList("ROLE_GERAL", "ROLE_ADMIN"))); List listaPermissoes = new ArrayList(); DBObject ob1 = permissao.findOne(new BasicDBObject("papel", "ROLE_ADMIN")); listaPermissoes.add(new DBRef("permissao", ob1.get("_id"))); DBObject ob2 = permissao.findOne(new BasicDBObject("papel", "ROLE_GERAL")); listaPermissoes.add(new DBRef("permissao", ob2.get("_id"))); BasicDBObject admin = new BasicDBObject(); admin.append("dataatualizacao", null); admin.append("datacriacao", new Date()); admin.append("nome", "Perfil Administrador"); admin.append("descricao", "Perfil com permisso geral."); admin.append("permissoes", listaPermissoes); perfil.insert(admin); BasicDBObject visitante = new BasicDBObject(); visitante.append("dataatualizacao", null); visitante.append("datacriacao", new Date()); visitante.append("nome", "Perfil Visitante"); visitante.append("descricao", "Perfil sem permisso para gerenciar uma aposta."); perfil.insert(visitante);/* w w w .j a va2s . co m*/ }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public ExpandoValue addValue(long classNameId, long tableId, long columnId, long classPK, String data) throws PortalException { ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId); ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(columnId); ExpandoValue expandoValue = ExpandoValueUtil.create(0); expandoValue.setCompanyId(expandoTable.getCompanyId()); expandoValue.setTableId(tableId);/*from ww w . ja v a 2s . co m*/ expandoValue.setColumnId(columnId); expandoValue.setRowId(classPK); expandoValue.setClassNameId(classNameId); expandoValue.setClassPK(classPK); expandoValue.setData(data); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", tableId); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", classNameId); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject != null) { expandoValue.setValueId(expandoValueDBObject.getLong("valueId")); DBObject operatorDBObject = new BasicDBObject(); DBObject updateExpandoValueDBObject = new BasicDBObject(expandoColumn.getName(), getData(expandoColumn, expandoValue)); operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject); dbCollection.update(expandoValueDBObject, operatorDBObject); } else { long valueId = CounterLocalServiceUtil.increment(); expandoValue.setValueId(valueId); queryDBObject.put("valueId", valueId); queryDBObject.put(expandoColumn.getName(), getData(expandoColumn, expandoValue)); dbCollection.insert(queryDBObject); } return expandoValue; }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public void addValues(long classNameId, long tableId, List<ExpandoColumn> expandoColumns, long classPK, Map<String, String> data) throws PortalException { ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId); ExpandoValue expandoValue = ExpandoValueUtil.create(0); expandoValue.setCompanyId(expandoTable.getCompanyId()); expandoValue.setTableId(tableId);//ww w . j av a2 s .com expandoValue.setRowId(classPK); expandoValue.setClassNameId(classNameId); expandoValue.setClassPK(classPK); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", tableId); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", classNameId); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject != null) { expandoValue.setValueId(expandoValueDBObject.getLong("valueId")); DBObject operatorDBObject = new BasicDBObject(); DBObject updateExpandoValueDBObject = new BasicDBObject(); updateExpandoValueDBObject(updateExpandoValueDBObject, expandoColumns, data, expandoValue); operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject); dbCollection.update(expandoValueDBObject, operatorDBObject); } else { long valueId = CounterLocalServiceUtil.increment(); expandoValue.setValueId(valueId); queryDBObject.put("valueId", valueId); updateExpandoValueDBObject(queryDBObject, expandoColumns, data, expandoValue); dbCollection.insert(queryDBObject); } }
From source file:com.malsolo.mongodb.humongous.driver.Main.java
public static void main(String... args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("news"); Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s);//ww w .java2 s .c o m } DBCollection coll = db.getCollection("article"); BasicDBObject doc = new BasicDBObject("authorId", UUID.randomUUID()).append("author", "Driver") .append("date", new Date()).append("title", "Title"); coll.insert(doc); DBObject myDoc = coll.findOne(); System.out.println(myDoc); System.out.println(coll.getCount()); try (DBCursor cursor = coll.find()) { while (cursor.hasNext()) { System.out.println(cursor.next()); } } }
From source file:com.maoyan.pf.webcollector.spider.AttendrateCrawler.java
License:Open Source License
public static void main(String[] args) throws Exception { Executor executor = new Executor() { @Override//w ww .j ava2 s. c om public void execute(CrawlDatum datum, CrawlDatums next) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); // ? // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); DB db = mongoClient.getDB("maoyan_crawler"); // ????? Set<String> colls = db.getCollectionNames(); for (String s : colls) { // Collection(?"") if (s.equals("attend_rate")) { db.getCollection(s).drop(); } } DBCollection dbCollection = db.getCollection("attend_rate"); HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(false); driver.get(datum.getUrl()); // System.out.println(driver.getPageSource()); WebElement element = driver.findElementByCssSelector("div#seat_table"); List<WebElement> movie_name = element.findElements(By.className("c1 lineDot")); List<WebElement> boxoffice_rate = element.findElements(By.className("c2 red")); List<WebElement> visit_pershow = element.findElements(By.className("c3 gray")); WebElement cityarea = driver.findElementByCssSelector("span[class='today']"); System.out.println(cityarea.getText()); for (int i = 0; i < movie_name.size(); i++) { System.out.println(movie_name.get(i).getText()); System.out.println(boxoffice_rate.get(i).getText()); System.out.println(visit_pershow.get(i).getText()); BasicDBObject dbObject = new BasicDBObject(); dbObject.append("title", cityarea.getText()).append("movie_name", movie_name.get(i).getText()) .append("boxoffice_rate", boxoffice_rate.get(i).getText()) .append("visit_pershow", visit_pershow.get(i).getText()); dbCollection.insert(dbObject); } mongoClient.close(); } }; //DBDBManager DBManager manager = new BerkeleyDBManager("maoyan"); //Crawler?DBManagerExecutor Crawler crawler = new Crawler(manager, executor); crawler.addSeed("http://pf.maoyan.com/attend/rate"); crawler.start(1); }
From source file:com.maoyan.pf.webcollector.spider.MoviestoreCrawler.java
License:Open Source License
public static void main(String[] args) throws Exception { Executor executor = new Executor() { @Override/* w w w . j ava 2 s . com*/ public void execute(CrawlDatum datum, CrawlDatums next) throws Exception { MongoClient mongoClient = new MongoClient("127.0.0.1", 27017); Logger mongoLogger = Logger.getLogger("org.mongodb.driver"); mongoLogger.setLevel(Level.ERROR); // ? // DBCollection dbCollection = // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); DB db = mongoClient.getDB("maoyan_crawler"); // ????? DBCollection dbCollection = db.getCollection("movie_store"); HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(false); driver.get(datum.getUrl()); // System.out.println(driver.getPageSource()); System.out.println(driver.findElement(By.xpath("//html/body/header/h1")).getText()); // ProfilesIni pi = new ProfilesIni(); // FirefoxProfile profile = pi.getProfile("default"); // WebClient webClient = new // WebClient(BrowserVersion.FIREFOX_38); // driver.setJavascriptEnabled(false); // webClient.getOptions().setCssEnabled(true); // HtmlPage page = webClient.getPage(datum.getUrl()); // System.out.println(page.asXml()); // List<?> title = // page.getByXPath("//html/body/header/h1/text()"); // List<?> score = // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/i/text()"); // List<?> score_num = // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/span/text()"); // List<?> want_num = // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[2]/i/text()"); // List<?> type = // page.getByXPath("//html/body/section[1]/article[1]/aside/p[1]/text()"); // List<?> timing = // page.getByXPath("//html/body/section[1]/article[1]/aside/p[2]/text()"); // List<?> system = // page.getByXPath("//html/body/section[1]/article[1]/aside/p[3]/text()"); // List<?> onscreendate = // page.getByXPath("//html/body/section[1]/article[1]/aside/p[4]/text()"); // List<?> total_rev = // page.getByXPath("//html/body/section[1]/article[2]/span[1]/text()"); // List<?> firstweek_rev = // page.getByXPath("//html/body/section[1]/article[2]/span[2]/text()"); // List<?> ahead_rev = // page.getByXPath("//html/body/section[1]/article[2]/span[3]/text()"); // List<?> director = // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[1]/div[2]/text()"); // List<?> actor = // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[2]/div[2]/text()"); // List<?> production = // page.getByXPath("//*[@id='infoContent']/article[2]/div/div[1]/div/div[2]/text()"); // List<?> distribution = // page.getByXPath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]/text()"); // List<?> technique = // page.getByXPath("//*[@id='infoContent']/article[5]/div/div[1]/div/div[2]/text()"); // List<?> desc = // page.getByXPath("//*[@id='infoContent']/article[6]/div/div[2]/text()"); // List<?> want_by_city = // page.getByXPath("//*[@id='wantCity']/ul/li[2]/b/text()"); // List<?> want_val= // page.getByXPath("//*[@id='wantCity']/ul/li[3]/div/@style"); // System.out.println(title); //// System.out.println(page.getByXPath("//span[@class='today']/em/text()")); //// System.out.println(page.getByXPath("//span[@class='today']/text()")); //// List<?> movie_name = // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c1 // lineDot']/text()"); //// List<?> boxoffice_rate = // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c2 // red']/text()"); //// List<?> visit_pershow = // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c3 // gray']/text()"); //// for(int i = 0;i<movie_name.size();i++){ //// System.out.println(movie_name.get(i)); //// System.out.println(boxoffice_rate.get(i)); //// System.out.println(visit_pershow.get(i)); //// } // List<?> hbList = page.getByXPath("//div"); // HtmlDivision hb = (HtmlDivision)hbList.get(0); // System.out.println(hb.toString()); // System.out.println(ReturnString(driver,By.xpath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]"))); if (ReturnString(driver, (By.xpath("//html/body/header/h1"))).length() != 0) { BasicDBObject dbObject = new BasicDBObject(); dbObject.append("title", ReturnString(driver, (By.xpath("//html/body/header/h1")))) .append("url", driver.getCurrentUrl()) .append("score", ReturnString(driver, By.xpath( "//html/body/section[1]/article[1]/aside/hgroup/article[1]/i"))) .append("score_num", ReturnString(driver, By.xpath( "//html/body/section[1]/article[1]/aside/hgroup/article[1]/span"))) .append("want_num", ReturnString(driver, By.xpath( "//html/body/section[1]/article[1]/aside/hgroup/article[2]/i"))) .append("type", ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[1]"))) .append("timing", ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[2]"))) .append("system", ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[3]"))) .append("onscreendate", ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[4]"))) .append("total_rev", ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[1]"))) .append("firstweek_rev", ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[2]"))) .append("ahead_rev", ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[3]"))) .append("director", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[1]/div[2]"))) .append("actor", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[2]/div[2]"))) .append("production", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[@class='production-companies m-info-section']/div/div[1]/div/div[2]"))) .append("jointproduction", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[@class='joint-production-companies m-info-section']/div/div[1]/div/div[2]"))) .append("distribution", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[@class='distribution-firm m-info-section']/div/div[1]/div/div[2]"))) .append("techpara", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[tech-params m-info-section]/div/div[1]/div/div[2]"))) .append("infodrama", ReturnString(driver, By.xpath( "//*[@id='infoContent']/article[m-info-drama m-info-section]/div/div[2]"))) .append("want_by_city", ReturnString(driver, By.xpath("//*[@id='wantCity']"))); dbCollection.insert(dbObject); driver.close(); driver.quit(); mongoClient.close(); } } }; MongoClient mongoClient = new MongoClient("127.0.0.1", 27017); // ? // DBCollection dbCollection = // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); DB db = mongoClient.getDB("maoyan_crawler"); // ????? Set<String> colls = db.getCollectionNames(); for (String s : colls) { // Collection(?"") if (s.equals("movie_store")) { db.getCollection(s).drop(); } } // DBDBManager DBManager manager = new BerkeleyDBManager("maoyan"); // Crawler?DBManagerExecutor for (int round = 200; round < 300; round++) { System.out.println("Round " + round + " crawling.../n"); Crawler crawler = new Crawler(manager, executor); for (int i = 1 + 100 * round; i <= 100 + 100 * round; i++) { crawler.addSeed("http://pf.maoyan.com/movie/" + i + "?_v_=yes"); } /* ??visit */ crawler.setVisitInterval(50); /* ?http? */ crawler.setRetryInterval(100); crawler.setThreads(10); crawler.start(1); } }