汕头网站推广教程,中国建设银行网站能查流水吗,深圳建设工程交易网app,网站优化试卷一、实验目的
1. 理解如何通过Java API连接MongoDB数据库。
2. 学习在Java中使用MongoDB进行数据库操作#xff0c;包括插入数据、查询数据以及数据统计等。
3. 掌握电子商务日志数据在MongoDB中的存储和操作方法。
二、实验环境准备
1. JAVA环境准备#xff1a;确保…一、实验目的
1. 理解如何通过Java API连接MongoDB数据库。
2. 学习在Java中使用MongoDB进行数据库操作包括插入数据、查询数据以及数据统计等。
3. 掌握电子商务日志数据在MongoDB中的存储和操作方法。
二、实验环境准备
1. JAVA环境准备确保Java Development Kit (JDK) 已安装并配置好环境变量。
2. Hadoop环境准备安装并配置Hadoop环境确保Hadoop的各个组件可以在伪分布式模式下运行。
三、实验教材参考
《大数据存储》谭旭人民邮电出版社2022ISBN 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 MongoCollectionDocument setupDatabaseAndCollection(MongoDatabase database) {// 删除并重新创建集合仅用于实验目的MongoCollectionDocument 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(MongoCollectionDocument collection) {ListDocument 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(MongoCollectionDocument 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));FindIterableDocument results collection.find(filter);for (Document doc : results) {System.out.println(doc.toJson());}
}2运行结果 2. 按商品名称查询
1代码实现
private static void queryLogsByProductName(MongoCollectionDocument collection, String productName) {System.out.println(\nQuery: Logs for product productName );Bson filter Filters.eq(product, productName);FindIterableDocument results collection.find(filter);for (Document doc : results) {System.out.println(doc.toJson());}
}2运行结果 5、分类统计
实现对日志数据的分类统计例如统计某个时间段内的日志数量、按商品类别统计销售额等确保统计结果准确。
1. 统计某时间段内日志数量
1代码实现
private static void countLogsByTimeRange(MongoCollectionDocument 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(MongoCollectionDocument collection) {System.out.println(\nSales by Category:);ListBson pipeline Arrays.asList(Aggregates.group($category,Accumulators.sum(total_sales, new Document($multiply, Arrays.asList($price, $quantity)))));AggregateIterableDocument 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);MongoCollectionDocument 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 MongoCollectionDocument setupDatabaseAndCollection(MongoDatabase database) {// 删除并重新创建集合仅用于实验目的MongoCollectionDocument 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(MongoCollectionDocument collection) {ListDocument 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(MongoCollectionDocument 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));FindIterableDocument results collection.find(filter);for (Document doc : results) {System.out.println(doc.toJson());}}/*** 按商品名称查询日志数据*/private static void queryLogsByProductName(MongoCollectionDocument collection, String productName) {System.out.println(\nQuery: Logs for product productName );Bson filter Filters.eq(product, productName);FindIterableDocument results collection.find(filter);for (Document doc : results) {System.out.println(doc.toJson());}}/*** 统计某时间段内的日志数量*/private static void countLogsByTimeRange(MongoCollectionDocument 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(MongoCollectionDocument collection) {System.out.println(\nSales by Category:);ListBson pipeline Arrays.asList(Aggregates.group($category,Accumulators.sum(total_sales, new Document($multiply, Arrays.asList($price, $quantity)))));AggregateIterableDocument results collection.aggregate(pipeline);for (Document doc : results) {System.out.println(doc.toJson());}}
}