头歌-旅游网站大数据分析 - 数据存储

该代码段展示了如何使用Java和ApacheHBase库来保存酒店和城市的数据,包括创建HBase表,读取JSON数据,转换为Java对象,并将数据存储到表中。另外,它还包含了保存酒店评论信息的函数结构,但未提供具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1关:保存酒店和城市数据

package com.savedata;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import com.alibaba.fastjson.JSONObject;
import com.entity.Hotel;
import com.entity.HotelComment;
import com.util.HBaseUtil;
public class SaveData {
    /**
     * 获取并保存酒店和城市数据
     */
    public static void saveCityAndHotelInfo() {
        /**********   Begin   **********/        
                try {
            HBaseUtil.createTable("t_city_hotels_info", new String[] { "cityInfo", "hotel_info" });
        } catch (Exception e) {
            // 创建表失败
            e.printStackTrace();
        }
        List<Put> puts = new ArrayList<>();
        // 添加数据
        try {
            InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("aomen.txt");
            String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
            List<Hotel> parseArray = JSONObject.parseArray(readFileToString, Hotel.class);
            String hongkong = IOUtils.toString(SaveData.class.getClassLoader().getResourceAsStream("hongkong.txt"),
                    "UTF-8");
            List<Hotel> hongkongHotel = JSONObject.parseArray(hongkong, Hotel.class);
            parseArray.addAll(hongkongHotel);
            for (Hotel hotel : parseArray) {
                String cityId = hotel.getCity_id();
                String hotelId = hotel.getId();
                Put put = new Put(Bytes.toBytes(cityId + "_" + hotelId));
                // 添加city数据
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityId"), Bytes.toBytes(cityId));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityName"),
                        Bytes.toBytes(hotel.getCity_name()));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("pinyin"), Bytes.toBytes(hotel.getPinyin()));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("collectionTime"),
                        Bytes.toBytes(hotel.getCollectionTime()));
                // 添加hotel数据
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("id"), Bytes.toBytes(hotel.getId()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("name"), Bytes.toBytes(hotel.getName()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("price"), Bytes.toBytes(String.valueOf(hotel.getPrice())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("lon"), Bytes.toBytes(String.valueOf(hotel.getLon())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("url"), Bytes.toBytes(hotel.getUrl()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("img"), Bytes.toBytes(hotel.getImg()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("address"), Bytes.toBytes(hotel.getAddress()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("score"), Bytes.toBytes(String.valueOf(hotel.getScore())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpscore"), Bytes.toBytes(String.valueOf(hotel.getDpscore())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpcount"), Bytes.toBytes(String.valueOf(hotel.getDpcount())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("star"), Bytes.toBytes(hotel.getStar()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("stardesc"),
                        Bytes.toBytes(hotel.getStardesc()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("shortName"),
                        Bytes.toBytes(hotel.getShortName()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("isSingleRec"),
                        Bytes.toBytes(hotel.getIsSingleRec()));
                puts.add(put);
            }
            // 批量保存数据
            HBaseUtil.putByTable("t_city_hotels_info", puts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
        
        /**********   End   **********/         
    }
    
    /**
     * 获取和保存酒店的评论数据
     */
    public static void saveCommentInfo() {
        /**********   Begin   **********/
         
         
         
         
        /**********   End   **********/
    }
}

第2关:保存酒店评论信息

package com.savedata;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import com.alibaba.fastjson.JSONObject;
import com.entity.Hotel;
import com.entity.HotelComment;
import com.util.HBaseUtil;
public class SaveData {
    /**
     * 获取并保存酒店和城市数据
     */
    public static void saveCityAndHotelInfo() {
        /**********   Begin   **********/        
                try {
            HBaseUtil.createTable("t_city_hotels_info", new String[] { "cityInfo", "hotel_info" });
        } catch (Exception e) {
            // 创建表失败
            e.printStackTrace();
        }
        List<Put> puts = new ArrayList<>();
        // 添加数据
        try {
            InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("aomen.txt");
            String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
            List<Hotel> parseArray = JSONObject.parseArray(readFileToString, Hotel.class);
            String hongkong = IOUtils.toString(SaveData.class.getClassLoader().getResourceAsStream("hongkong.txt"),
                    "UTF-8");
            List<Hotel> hongkongHotel = JSONObject.parseArray(hongkong, Hotel.class);
            parseArray.addAll(hongkongHotel);
            for (Hotel hotel : parseArray) {
                String cityId = hotel.getCity_id();
                String hotelId = hotel.getId();
                Put put = new Put(Bytes.toBytes(cityId + "_" + hotelId));
                // 添加city数据
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityId"), Bytes.toBytes(cityId));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityName"),
                        Bytes.toBytes(hotel.getCity_name()));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("pinyin"), Bytes.toBytes(hotel.getPinyin()));
                put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("collectionTime"),
                        Bytes.toBytes(hotel.getCollectionTime()));
                // 添加hotel数据
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("id"), Bytes.toBytes(hotel.getId()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("name"), Bytes.toBytes(hotel.getName()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("price"), Bytes.toBytes(String.valueOf(hotel.getPrice())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("lon"), Bytes.toBytes(String.valueOf(hotel.getLon())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("url"), Bytes.toBytes(hotel.getUrl()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("img"), Bytes.toBytes(hotel.getImg()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("address"), Bytes.toBytes(hotel.getAddress()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("score"), Bytes.toBytes(String.valueOf(hotel.getScore())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpscore"), Bytes.toBytes(String.valueOf(hotel.getDpscore())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpcount"), Bytes.toBytes(String.valueOf(hotel.getDpcount())));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("star"), Bytes.toBytes(hotel.getStar()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("stardesc"),
                        Bytes.toBytes(hotel.getStardesc()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("shortName"),
                        Bytes.toBytes(hotel.getShortName()));
                put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("isSingleRec"),
                        Bytes.toBytes(hotel.getIsSingleRec()));
                puts.add(put);
            }
            // 批量保存数据
            HBaseUtil.putByTable("t_city_hotels_info", puts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
        
        /**********   End   **********/         
    }
    
    /**
     * 获取和保存酒店的评论数据
     */
    public static void saveCommentInfo() {
        /**********   Begin   **********/
        // 创建评论表
        try {
            HBaseUtil.createTable("t_hotel_comment", new String[] { "hotel_info", "comment_info" });
        } catch (Exception e) {
            // 创建表失败
            e.printStackTrace();
        }
        InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("comment.txt");
        try {
        String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
        List<HotelComment> otherCommentListByPage = JSONObject.parseArray(readFileToString, HotelComment.class);
        // 获取数据
        List<Put> puts = new ArrayList<>();
        // 定义Put对象
        for (HotelComment comment : otherCommentListByPage) {
            Put put = new Put((comment.getHotel_id()  + "_" + comment.getId()).getBytes());
            put.addColumn("hotel_info".getBytes(), "hotel_name".getBytes(),
                    comment.getHotel_name().getBytes());
            put.addColumn("hotel_info".getBytes(), "hotel_id".getBytes(), comment.getHotel_id().getBytes());
            // 数据量很大在这里只保存用作分析的数据
            put.addColumn("comment_info".getBytes(), "id".getBytes(), Bytes.toBytes(String.valueOf(comment.getId())));
            put.addColumn("comment_info".getBytes(), "baseRoomId".getBytes(), Bytes.toBytes(String.valueOf(comment.getBaseRoomId())));
            if (comment.getBaseRoomId() != -1 && comment.getBaseRoomName() != null) {
                put.addColumn("comment_info".getBytes(), "baseRoomName".getBytes(),
                        Bytes.toBytes(comment.getBaseRoomName()));
            }
            put.addColumn("comment_info".getBytes(), "checkInDate".getBytes(), Bytes.toBytes(comment.getCheckInDate()));
            put.addColumn("comment_info".getBytes(), "postDate".getBytes(), Bytes.toBytes(comment.getPostDate()));
            put.addColumn("comment_info".getBytes(), "content".getBytes(), Bytes.toBytes(comment.getContent()));
            put.addColumn("comment_info".getBytes(), "highlightPosition".getBytes(),
                    Bytes.toBytes(comment.getHighlightPosition()));
            put.addColumn("comment_info".getBytes(), "hasHotelFeedback".getBytes(),
                    Bytes.toBytes(String.valueOf(comment.getHasHotelFeedback())));
            put.addColumn("comment_info".getBytes(), "userNickName".getBytes(),
                    Bytes.toBytes(comment.getUserNickName()));
            puts.add(put);
        }
            // 上传数据
            HBaseUtil.putByTable("t_hotel_comment", puts);
        } catch (Exception e) {
            e.printStackTrace();
        }
         
         
         
         
        /**********   End   **********/
    }
}

### 旅游网站大数据分析中的数据存储方案 在旅游网站大数据分析场景下,数据存储方案的选择至关重要。由于涉及大量的用户行为日志、预订记录以及地理空间数据等复杂多样的信息源,因此需要一种能够高效处理大规模数据并提供灵活查询能力的技术栈。 #### 使用Hadoop作为基础架构 Hadoop作为一个开源的分布式文件系统 (HDFS) 和分布式计算框架 (MapReduce),非常适合用于存储和管理海量的历史数据[^4]。对于旅游网站而言,用户的浏览历史、点击流数据以及其他非结构化或半结构化的信息都可以通过HDFS来持久化保存。这种设计不仅具备高可用性和容错机制,还允许随着业务增长轻松扩展集群规模以适应更大的数据量需求。 #### 利用Spark提升性能与灵活性 尽管Hadoop擅长于批量作业执行,但在某些情况下可能显得效率较低或者不够敏捷。这时引入Apache Spark就变得尤为重要了——相比传统的MapReduce模型来说,它可以显著加快迭代型算法的速度,并且支持内存内的快速运算[^2]。特别是当涉及到实时推荐引擎开发或者是复杂的交互式探索任务时,采用Spark无疑会带来更好的用户体验效果。 另外值得一提的是,在具体实现过程中还可以借助于Spark SQL这一强大功能组件来进行结构化数据集上的操作[^3]。例如针对已有的订单表单或是客户反馈意见等内容创建相应的DataFrames对象之后再运用标准SQL语句完成检索工作;这样一来既简化了编码流程又提高了可读性水平! ```python from pyspark.sql import SparkSession # 初始化Spark Session spark = SparkSession.builder \ .appName("Tourism Data Analysis") \ .getOrCreate() # 加载CSV格式的数据到DataFrame中 df_orders = spark.read.csv("/path/to/orders", header=True, inferSchema=True) # 执行简单的SQL查询 df_filtered = df_orders.filter(df_orders['status'] == 'completed') result = df_filtered.groupBy('destination').count().orderBy('count', ascending=False).limit(10) result.show() ``` 上述代码片段展示了如何利用PySpark加载一份关于旅行订单详情列表(假设为CSV形式),并通过编写简洁明快的SQL表达式找出最受欢迎的目的地排名前十位的结果展示出来。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值