<project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion > 4.0.0</modelVersion >
<groupId > com.laodixiao.connectToMongoDB</groupId >
<artifactId > connectToMongoDB</artifactId >
<version > 0.0.1-SNAPSHOT</version >
<packaging > jar</packaging >
<name > connectToMongoDB</name >
<url > http://maven.apache.org</url >
<properties >
<project.build.sourceEncoding > UTF-8</project.build.sourceEncoding >
</properties >
<dependencies >
<dependency >
<groupId > junit</groupId >
<artifactId > junit</artifactId >
<version > 3.8.1</version >
<scope > test</scope >
</dependency >
<dependency >
<groupId > org.mongodb</groupId >
<artifactId > mongo-java-driver</artifactId >
<version > 3.2.2</version >
</dependency >
<dependency >
<groupId > com.alibaba</groupId >
<artifactId > fastjson</artifactId >
<version > 1.2.7</version >
</dependency >
<dependency >
<groupId > org.slf4j</groupId >
<artifactId > slf4j-api</artifactId >
<version > 1.7.6</version >
</dependency >
<dependency >
<groupId > org.slf4j</groupId >
<artifactId > slf4j-log4j12</artifactId >
<version > 1.7.6</version >
</dependency >
</dependencies >
</project >
package com .laodixiao .connectToMongoDB .connectToMongoDB
import java.io .File
import java.io .FileOutputStream
import java.io .IOException
import java.io .OutputStreamWriter
import java.text .SimpleDateFormat
import java.util .ArrayList
import java.util .Date
import java.util .List
import java.util .Locale
import java.util .Properties
import org.bson .Document
import org.slf 4j.Logger
import org.slf 4j.LoggerFactory
import com .alibaba .fastjson .JSONObject
import com .mongodb .MongoClient
import com .mongodb .MongoCredential
import com .mongodb .ServerAddress
import com .mongodb .client .FindIterable
import com .mongodb .client .MongoCollection
import com .mongodb .client .MongoCursor
import com .mongodb .client .MongoDatabase
public class CopyOfConnectToMongoDB {
private static MongoClient mongoClient
private static Logger log = LoggerFactory.getLogger (CopyOfConnectToMongoDB.class )
public static void main(String[] args) {
String outputPathPRE = "/data/xinting.xiao/zzzHotCal/getEPG/data/"
if (args.length ==1 ){
outputPathPRE = args[0 ]
}else {
System.err .println ("Usage: $JAVA_HOME/java -jar connectToMongoDB.jar outputPathDir" )
return
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss" , Locale.CHINA )
System.out .println (df.format (new Date()))
//获取新的EPG信息
System.out .println ("Start to get EPG infomation ..." )
String outputTxtFilePathString = outputPathPRE + "EPGInfoNew.txt"
getEPGInfoFromMongoDB(outputTxtFilePathString)
//替换旧的EPG信息
System.out .println ("Start to replace EPG infomation ..." )
String ePGInfoOldTxtFilePathString = outputPathPRE + "EPGInfo.txt"
replaceOldEPGInfoTxt(ePGInfoOldTxtFilePathString, outputTxtFilePathString)
System.out .println ("OK !" )
System.out .println (df.format (new Date()))
}
public static void getEPGInfoFromMongoDB(String outputTxtFilePath){
Properties props = new Properties()
try {
props.load (CopyOfConnectToMongoDB.class .getClassLoader ().getResourceAsStream ("mongoProperties.properties" ))
} catch (IOException e1) {
e1.printStackTrace ()
}
try {
//连接MongoDB
log.info ("Class::CopyOfConnectToMongoDB --> Method::getEPGInfoFromMongoDB ==> 连接MongoDB ......" )
ServerAddress serverAddress = new ServerAddress(props.getProperty ("IP" ).toString (), Integer.parseInt (props.getProperty ("PORT" )))
List<ServerAddress> addrs = new ArrayList<ServerAddress>()
addrs.add (serverAddress)
MongoCredential credential = MongoCredential.createCredential (props.getProperty ("USERNAME" ), props.getProperty ("DATABASE" ), props.getProperty ("PASSWORD" ).toCharArray ())
List<MongoCredential> credentials = new ArrayList<MongoCredential>()
credentials.add (credential)
mongoClient = new MongoClient(addrs, credentials)
MongoDatabase database = mongoClient.getDatabase (props.getProperty ("DATABASE" ))
MongoCollection<Document> collection = database.getCollection (props.getProperty ("COLLECTION" ))
//保存符合条件的结果: one row = [cid, player, tags, model, vip]
File file = new File(outputTxtFilePath)
if(!file.exists ()){
file.createNewFile ()
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "utf-8" )
//获取所有点播EPG信息
log.info ("Class::CopyOfConnectToMongoDB --> Method::getEPGInfoFromMongoDB ==> 从MongoDB获取所有记录 ......" )
FindIterable<Document> find = collection.find ()
find.noCursorTimeout (true)
//示例:处理全部记录
MongoCursor<Document> cursor = find.iterator ()
int cnt = 0
while (cursor.hasNext ()) {
cnt = cnt + 1
//读取一条完整记录
Document nextTmp = cursor.next ()
//解析一条记录
JSONObject json = JSONObject.parseObject (nextTmp.toJson ())
try {
//Step 1 -1 : 获取节目id和播放器种类
String cid = ""
String player = ""
int tencent = Integer.parseInt (json.get ("tencent" ).toString ())
int youpeng = Integer.parseInt (json.get ("youpeng" ).toString ())
int iqiyi = Integer.parseInt (json.get ("iqiyi" ).toString ())
if (tencent==1 ) {
cid = JSONObject.parseObject (json.get ("pp_tencent" ).toString ()).get ("tencentId" ).toString ()
player = "tencent"
}else if (tencent==0 && youpeng==1 ) {
cid = JSONObject.parseObject (json.get ("pp_youpeng" ).toString ()).get ("yp_id" ).toString ()
player = "youpeng"
}else if (tencent==0 && youpeng==0 && iqiyi==1 ) {
cid = JSONObject.parseObject (json.get ("pp_iqiyi" ).toString ()).get ("iqiyiId" ).toString ()
player = "iqiyi"
}
//Step 1 -2 : 获取节目标签
StringBuilder tags = new StringBuilder()
String[] cat = json.get ("categorys" ).toString ().replace ("[" , "" ).replace ("]" , "" ).trim ().split ("}," )
for (int j = 0
String catName
if (j==cat.length -1 ){
catName = JSONObject.parseObject (cat[j].toString ()).get ("name" ).toString ()
}else {
catName = JSONObject.parseObject (cat[j].toString ()+"}" ).get ("name" ).toString ()
}
//去除不必要的标签
if (catName.contains ("内地" )
|| catName.contains ("国语" ) || catName.contains ("剧情" )
|| catName.contains ("1080P" )
|| catName.contains ("其它" ) || catName.contains ("华语" )
|| catName.matches ("^\\d{4}$" ))
{
continue
}else {
tags.append (catName+"$" )
}
}
String categorys_name = tags.toString ().substring (0 , tags.toString ().length ()-1 )
//Step 1 -3 :获取节目父类
String model = ""
if ( tags.toString ().contains ("动画" ) || tags.toString ().contains ("儿童" ) || tags.toString ().contains ("动漫" ) || tags.toString ().contains ("少儿" ) ) {
model = "cartoon"
}else {
model = json.get ("model" ).toString ()
}
//Step 1 -4 : 判断是否为VIP影片 vip=1 为vip, vip=0 为非vip, vip=-1 为未知
int vip = 0
if ("tencent" .equals (player)) {
String pay_status = JSONObject.parseObject (json.get ("pp_tencent" ).toString ()).get ("pay_status" ).toString ()
if ("免费" .equals (pay_status)) {
vip = 0
}else {
vip = 1
}
}else if ("youpeng" .equals (player)) {
vip = 1
}else {
vip = -1
}
//获取一条记录的有用信息 oneRecord = [cid, player, tag, model, vip]
if("" .equals (cid) || "" .equals (player) || "" .equals (model) || null==cid || null==player || null==model){
continue
}else {
if(categorys_name.equals ("" )){
categorys_name = "其他"
}
osw.write (cid + "," + player + "," + categorys_name + "," + model + "," + vip + "\n" )
}
} catch (Exception e) {
continue
}
}
System.out .println ("Totally " + cnt + " items have been record!" )
osw.close ()
} catch (Exception e) {
System.err .println (e.getClass ().getName () + ": " + e.getMessage ())
} finally {
if (mongoClient != null) {
//关闭MongoDB连接
mongoClient.close ()
}
}
}
private static void replaceOldEPGInfoTxt(String ePGInfoOldTxtFilePathString, String outputTxtFilePathString) {
File oldFile = new File(ePGInfoOldTxtFilePathString)
File newFile = new File(outputTxtFilePathString)
if ( !oldFile.exists () ) { //如果旧文件不存在,则直接将新文件命名为旧文件名称
newFile.renameTo (oldFile)
} else { //若旧文件存在,则先删除旧文件,然后将新文件命名为旧文件名称, 最后删除新文件
oldFile.delete ()
newFile.renameTo (oldFile)
newFile.delete ()
}
}
}