MongoDB实训:电子商务日志存储任务
一、实验目的
1. 理解如何通过Java API连接MongoDB数据库。
2. 学习在Java中使用MongoDB进行数据库操作,包括插入数据、查询数据以及数据统计等。
3. 掌握电子商务日志数据在MongoDB中的存储和操作方法。
二、实验环境准备
1. JAVA环境准备:确保Java Development Kit (JDK) 已安装并配置好环境变量。
2. Hadoop环境准备:安装并配置Hadoop环境,确保Hadoop的各个组件可以在伪分布式模式下运行。
三、实验教材参考
《大数据存储》,谭旭,人民邮电出版社,2022,ISBN 978-7-115-59414-3。
四、实验内容与步骤
1、连接MongoDB
使用Java API连接到MongoDB数据库,确保连接过程正确,可以通过主机名、端口号等方式连接到MongoDB服务器。
1. 首先在pom.xml中添加MongoDB的Maven依赖
2. 使用MongoDB Java驱动连接到MongoDB数据库。
(1)代码实现
private static final String DATABASE_NAME = "ECommerceDB";
private static final String COLLECTION_NAME = "Logs";
private static final String CONNECTION_STRING = "mongodb://192.168.10.200:27017";
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create(CONNECTION_STRING)) {
System.out.println("Connected to MongoDB!");
}
}
(2)运行结果
2、创建数据库和集合
在MongoDB中创建一个新的数据库,然后在该数据库中创建一个用于存储电子商务日志数据的集合。
1. 代码实现
private static MongoCollection<Document> setupDatabaseAndCollection(MongoDatabase database) {
// 删除并重新创建集合(仅用于实验目的)
MongoCollection<Document> collection = database.getCollection(COLLECTION_NAME);
collection.drop();
database.createCollection(COLLECTION_NAME);
System.out.println("Database and Collection created!");
return database.getCollection(COLLECTION_NAME);
}
2. 运行结果
3、插入日志数据
编写 Java 代码,将模拟的电子商务日志数据插入到 MongoDB 的集合中,确保插入操作成功。
1. 代码实现
private static void insertLogData(MongoCollection<Document> collection) {
List<Document> logs = Arrays.asList(
new Document("timestamp", "2025-01-08T10:00:00")
.append("product", "Laptop")
.append("category", "Electronics")
.append("price", 1200)
.append("quantity", 1),
new Document("timestamp", "2025-01-08T11:30:00")
.append("product", "Phone")
.append("category", "Electronics")
.append("price", 800)
.append("quantity", 2),
new Document("timestamp", "2025-01-08T12:00:00")
.append("product", "Headphones")
.append("category", "Accessories")
.append("price", 200)
.append("quantity", 5),
new Document("timestamp", "2025-01-08T13:15:00")
.append("product", "Keyboard")
.append("category", "Accessories")
.append("price", 100)
.append("quantity", 3)
);
collection.insertMany(logs);
System.out.println("Log data inserted successfully!");
}
2. 运行结果
4、 查询日志数据
编写Java代码实现对MongoDB中日志数据的查询操作,可以包括基本的查询(如按时间范围查询、按商品名称查询)和复杂的查询(如多条件组合查询)。
1. 按时间范围查询
(1)代码实现
private static void queryLogsByTimeRange(MongoCollection<Document> collection, String startTime, String endTime) {
System.out.println("\nQuery: Logs from " + startTime + " to " + endTime);
Bson filter = Filters.and(
Filters.gte("timestamp", startTime),
Filters.lte("timestamp", endTime)
);
FindIterable<Document> results = collection.find(filter);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
(2)运行结果
2. 按商品名称查询
(1)代码实现
private static void queryLogsByProductName(MongoCollection<Document> collection, String productName) {
System.out.println("\nQuery: Logs for product '" + productName + "'");
Bson filter = Filters.eq("product", productName);
FindIterable<Document> results = collection.find(filter);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
(2)运行结果
5、分类统计
实现对日志数据的分类统计,例如统计某个时间段内的日志数量、按商品类别统计销售额等,确保统计结果准确。
1. 统计某时间段内日志数量
(1)代码实现
private static void countLogsByTimeRange(MongoCollection<Document> collection, String startTime, String endTime) {
System.out.println("\nCount: Logs from " + startTime + " to " + endTime);
Bson filter = Filters.and(
Filters.gte("timestamp", startTime),
Filters.lte("timestamp", endTime)
);
long count = collection.countDocuments(filter);
System.out.println("Number of logs in the specified time range: " + count);
}
(2)运行结果
2. 按商品类别统计销售额
(1)代码实现
private static void calculateSalesByCategory(MongoCollection<Document> collection) {
System.out.println("\nSales by Category:");
List<Bson> pipeline = Arrays.asList(
Aggregates.group("$category",
Accumulators.sum("total_sales", new Document("$multiply", Arrays.asList("$price", "$quantity"))))
);
AggregateIterable<Document> results = collection.aggregate(pipeline);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
(2)运行结果
6、完整代码
package com.example;
import com.mongodb.client.*;
import com.mongodb.client.model.*;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MongoDBConnection {
private static final String DATABASE_NAME = "ECommerceDB";
private static final String COLLECTION_NAME = "Logs";
private static final String CONNECTION_STRING = "mongodb://192.168.10.200:27017";
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create(CONNECTION_STRING)) {
System.out.println("Connected to MongoDB!");
MongoDatabase database = mongoClient.getDatabase(DATABASE_NAME);
MongoCollection<Document> collection = setupDatabaseAndCollection(database);
insertLogData(collection);
queryLogsByTimeRange(collection, "2025-01-08T10:00:00", "2025-01-08T12:00:00");
queryLogsByProductName(collection, "Laptop");
countLogsByTimeRange(collection, "2025-01-08T10:00:00", "2025-01-08T13:00:00");
calculateSalesByCategory(collection);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化数据库和集合
*/
private static MongoCollection<Document> setupDatabaseAndCollection(MongoDatabase database) {
// 删除并重新创建集合(仅用于实验目的)
MongoCollection<Document> collection = database.getCollection(COLLECTION_NAME);
collection.drop();
database.createCollection(COLLECTION_NAME);
System.out.println("Database and Collection created!");
return database.getCollection(COLLECTION_NAME);
}
/**
* 插入日志数据
*/
private static void insertLogData(MongoCollection<Document> collection) {
List<Document> logs = Arrays.asList(
new Document("timestamp", "2025-01-08T10:00:00")
.append("product", "Laptop")
.append("category", "Electronics")
.append("price", 1200)
.append("quantity", 1),
new Document("timestamp", "2025-01-08T11:30:00")
.append("product", "Phone")
.append("category", "Electronics")
.append("price", 800)
.append("quantity", 2),
new Document("timestamp", "2025-01-08T12:00:00")
.append("product", "Headphones")
.append("category", "Accessories")
.append("price", 200)
.append("quantity", 5),
new Document("timestamp", "2025-01-08T13:15:00")
.append("product", "Keyboard")
.append("category", "Accessories")
.append("price", 100)
.append("quantity", 3)
);
collection.insertMany(logs);
System.out.println("Log data inserted successfully!");
}
/**
* 按时间范围查询日志数据
*/
private static void queryLogsByTimeRange(MongoCollection<Document> collection, String startTime, String endTime) {
System.out.println("\nQuery: Logs from " + startTime + " to " + endTime);
Bson filter = Filters.and(
Filters.gte("timestamp", startTime),
Filters.lte("timestamp", endTime)
);
FindIterable<Document> results = collection.find(filter);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
/**
* 按商品名称查询日志数据
*/
private static void queryLogsByProductName(MongoCollection<Document> collection, String productName) {
System.out.println("\nQuery: Logs for product '" + productName + "'");
Bson filter = Filters.eq("product", productName);
FindIterable<Document> results = collection.find(filter);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
/**
* 统计某时间段内的日志数量
*/
private static void countLogsByTimeRange(MongoCollection<Document> collection, String startTime, String endTime) {
System.out.println("\nCount: Logs from " + startTime + " to " + endTime);
Bson filter = Filters.and(
Filters.gte("timestamp", startTime),
Filters.lte("timestamp", endTime)
);
long count = collection.countDocuments(filter);
System.out.println("Number of logs in the specified time range: " + count);
}
/**
* 按商品类别统计销售额
*/
private static void calculateSalesByCategory(MongoCollection<Document> collection) {
System.out.println("\nSales by Category:");
List<Bson> pipeline = Arrays.asList(
Aggregates.group("$category",
Accumulators.sum("total_sales", new Document("$multiply", Arrays.asList("$price", "$quantity"))))
);
AggregateIterable<Document> results = collection.aggregate(pipeline);
for (Document doc : results) {
System.out.println(doc.toJson());
}
}
}