我正在尝试通过解析csv并计算每个人去过一个国家或州的次数(如果他们在美国境内)来逐步更新我的数据库。
public static void main(String[] args) throws UnknownHostException {
Scanner dir = new Scanner(System.in);
String dir_loc;
System.out.println("Enter File Directory:");
dir_loc=dir.nextLine();
Scanner dbloc = new Scanner(System.in);
String db_loc;
System.out.println("Enter Database location:");
db_loc=dbloc.nextLine();
Scanner port = new Scanner(System.in);
int port_loc;
System.out.println("Enter Port:");
port_loc=port.nextInt();
dir.close();
dbloc.close();
port.close();
System.out.println("uploading");
String directoryName = dir_loc;
File directory = new File(directoryName);
File[] fileList = directory.listFiles();
DB dB = (new MongoClient(db_loc, port_loc)).getDB("visits");
DBCollection dBCollection = dB.getCollection("Channel");
BasicDBObject doc = null;
for(File file: fileList) {
String dataFileName = file.toString();
BufferedReader bReader = null;
try {
bReader = new BufferedReader(new FileReader(dataFileName));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
System.exit(-1);
}
String line = null;
int count = 0;
int statecount = 0;
try {
while((line = bReader.readLine()) !=null) {
String dataValue[] = line.split("\t");
String user_id = (dataValue[2]);
String country = (dataValue[1]);
boolean user_idExists = dBCollection.equals(user_id);
if(user_idExists){
if(dataValue[1].equals("US")) {
String state = (dataValue[5]);
System.out.println(state);
boolean stateExists = dBCollection.equals(state);
if(stateExists){
statecount ++;
doc = new BasicDBObject("user_id", user_id).append("country", country).append("state", state)
.append("count", statecount);
}
else{
statecount = 1;
doc = new BasicDBObject("user_id", user_id).append("country", country).append("state", state)
.append("count", statecount);
}
}
else{
boolean countryExists = dBCollection.equals(country);
if(countryExists){
count ++;
doc = new BasicDBObject("user_id", user_id).append("country", country).append("count", count);
}
else{
count = 1;
doc = new BasicDBObject("user_id", user_id).append("country", country).append("count", count);
}
}
}
else {
count = 1;
doc = new BasicDBObject("user_id", user_id).append("country", country).append("count", count);
}
}
} catch (IOException e1) {
e1.printStackTrace();
System.exit(-1);
}
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
dBCollection.insert(doc);
System.out.println("upload complete");
}
} 我得到的输出看起来像这样:
{ "_id" : { "$oid" : "53861247d121c16b38b3421d"} , "user_id" : "5406" , "country" : "US" , "state" : "NY" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b3421e"} , "user_id" : "5406" , "country" : "US" , "state" : "NY" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b3421f"} , "user_id" : "5406" , "country" : "US" , "state" : "NY" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34220"} , "user_id" : "5406" , "country" : "JA" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34220"} , "user_id" : "5406" , "country" : "JA" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34221"} , "user_id" : "5406" , "country" : "SA" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34221"} , "user_id" : "5406" , "country" : "SA" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34221"} , "user_id" : "5406" , "country" : "SA" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34222"} , "user_id" : "5406" , "country" : "US" , "state" : "TX" , "count" : 1}
{ "_id" : { "$oid" : "53861247d121c16b38b34223"} , "user_id" : "5406" , "country" : "US" , "state" : "TX" , "count" : 1}
但它看起来应该是这样的:
{ "_id" : { "$oid" : "53861247d121c16b38b3421d"} , "user_id" : "5406" , "country" : "VN" , "state" : "NY" , "count" : 3}
{ "_id" : { "$oid" : "53861247d121c16b38b34220"} , "user_id" : "5406" , "country" : "JA", "count" : 2}
{ "_id" : { "$oid" : "53861247d121c16b38b34221"} , "user_id" : "5406" , "country" : "SA", "count" : 3}
{ "_id" : { "$oid" : "53861247d121c16b38b34222"} , "user_id" : "5406" , "country" : "US" , "state" : "TX" , "count" : 2}
我不确定为什么我不能让州/国家计数增加?