1. Java语言连接mongo库需要mongo提供的驱动包,类似于传统数据库使用的jdbc包。
一般将连接的参数写在配置文件中,这些参数有:
#是否重试
autoConnectRetry=true
#连接池大小
poolSize=50
#主机地址
hosts=ip:port
#数据库名称
dbName=dbname
#数据库连接用户名
userName= username
#连接密码
password=password
#表名称
tableName=emailSent
所以,连接mongo的Java代码如下:
import java.util.ArrayList;
import java.util.Properties;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.ServerAddress;
public class MongoDBUtil {
private static final String AUTO_CONNECT_RETRY = "autoConnectRetry";
private static final String POOL_SIZE = "poolSize";
private static final String HOSTS = "hosts";
private static final String DB_NAME = "dbName";
private static final String USERNAME = "userName";
private static final String PASSWORD = "password";
private static final String CONFIG = "ubd_esp_conf.properties";
private static final Properties prop = new Properties();
private static MongoClient client = null;
private static ArrayList<ServerAddress> address = new ArrayList<ServerAddress>();
private static DB db = null;
static {
try{
prop.load(MongoDBUtil.class.getClassLoader().getResourceAsStream(CONFIG));
//对配置文件中的多个ip地址进行遍历
for(String hostIP : prop.getProperty(HOSTS).split(";")){
//虑空
hostIP= hostIP.trim();
//获得ip
Stringip = hostIP.split(":")[0];
//获得端口
int port= Integer.parseInt(hostIP.split(":")[1]);
//添加地址
address.add(new ServerAddress(ip,port));
}
mongoDBInit(address);
}catch(Exception e){
e.getStackTrace();
}
}
/**
* 初始化MongoDB连接
* @param address
* @throws Exception
*/
private static void mongoDBInit(ArrayList<ServerAddress> address) throws Exception{
Builderbuilder = new MongoClientOptions.Builder();
builder.autoConnectRetry(Boolean.parseBoolean(prop.getProperty(AUTO_CONNECT_RETRY)));
builder.connectionsPerHost(Integer.parseInt(prop.getProperty(POOL_SIZE)));
client = new MongoClient(address,builder.build());
db = client.getDB(prop.getProperty(DB_NAME));
if(!db.isAuthenticated()){
if(prop.getProperty(USERNAME)==null||prop.getProperty(PASSWORD)==null){
throw new Exception("the username or password is null in"+CONFIG);
}
db.authenticate(prop.getProperty(USERNAME), prop.getProperty(PASSWORD).toCharArray());
}
}
/**
* 返回collection
* @param tableName
* @return
*/
public static DBCollection getCollection(String tableName){
return db.getCollection(tableName);
}
}
2. 给出一个简单的mongo查询示例,将mongo查询的结果写入一个文件,并且按\001分隔,以备接下来使用Hadoop处理。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
importcom.dangdang.ubd.esp.help.MongoDBUtil;
import com.dangdang.ubd.esp.help.TimeTool;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryOperators;
public class GetEmailData {
privatestatic final String TABLE_NAME ="tableName";
privatestatic final String FILE_DIR = "fileDir";
privatestatic Properties prop = new Properties();
privatestatic final String CONFIG = "ubd_esp_conf.properties";
privatestatic int before = 0;
static{
try{
prop.load(GetEmailData.class.getClassLoader().getResourceAsStream(CONFIG));
before= Integer.parseInt(prop.getProperty("before"));
}catch (IOException e) {
e.printStackTrace();
}
}
publicstatic void getDataToFile(String sDate,String eDate,String tableName,StringfileName){
//文件
Filefile = new File(fileName);
if(file.exists()){
file.delete();
}
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
BufferedWriterbw = null;
try{
bw= new BufferedWriter(new FileWriter(file));
//获取到collection
DBCollectiondbCollection = MongoDBUtil.getCollection(tableName);
//查询条件为lastChangedTime距离现在为90天以内的并且打开的
DBObjectquery = new BasicDBObject();
DBObjectcondition = new BasicDBObject();
condition.put(QueryOperators.GTE,TimeTool.strToDate(sDate));
condition.put(QueryOperators.LT,TimeTool.strToDate(eDate));
query.put("lastChangedTime",condition);
query.put("open",true);
//执行查询
DBCursorcursor = dbCollection.find(query);
intcount = 0;
while(cursor.hasNext()){
DBObjectdbObject = cursor.next();
Stringinfo = getLineStr(dbObject);
bw.write(info);
bw.newLine();
count++;
}
bw.flush();
System.out.println(count);
}catch(Exceptione){
e.printStackTrace();
}finally{
if(bw!=null){
try{
bw.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 将mongo查询结果转换成hdfs存放的串
* @param jsonStr
* @return
*/
publicstatic String getLineStr (DBObject jsonStr){
StringBuffersb = new StringBuffer();
if(jsonStr.containsField("open")){
sb.append("\001"+ jsonStr.get("open"));
}else{
sb.append("\001"+ "false");
}
if(jsonStr.containsField("openTime")) {
sb.append("\001"+ TimeTool.dateToStr((Date)jsonStr.get("openTime")));
}else{
sb.append("\001"+ "1000-00-00 00:00:00");
}
returnsb.toString();
}
publicstatic void main(String[] args) {
try{
Stringedate = args[0];
Stringsdate = TimeTool.getBeforDate(edate, before);
StringtableName = prop.getProperty(TABLE_NAME);
StringfileDir = prop.getProperty(FILE_DIR);
StringfileName = fileDir+"ubd_esp_email_chg_log";
getDataToFile(sdate,edate,tableName,fileName);
}catch(Exceptione){
e.printStackTrace();
System.exit(-1);
}
}
}