头歌:旅游网站之数据分析

第1关 统计每个城市的宾馆平均价格

package com.processdata;

import java.io.IOException;
import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import com.util.HBaseUtil;

/**
 * 使用MapReduce程序处理HBase中的数据并将最终结果存入到另一张表 1中
 */
public class HBaseMapReduce extends Configured implements Tool {

    public static class MyMapper extends TableMapper<Text, DoubleWritable> {
        public static final byte[] column = "price".getBytes();
        public static final byte[] family = "hotel_info".getBytes();

        @Override
        protected void map(ImmutableBytesWritable rowKey, Result result, Context context)
                throws IOException, InterruptedException {
            /********** Begin *********/
		//获取酒店价格
        String cityId = Bytes.toString(result.getValue("cityIdInfo".getBytes(),"cityId".getBytes()));
        byte[] value = result.getValue(family,column);
        //将价格转换为double 
		Double hotel = Double.parseDouble(Bytes.toString(value));
        //将价格转化成()类型
        DoubleWritable i = new DoubleWritable(hotel);
		String key = cityId; 
		//写出城市(id,酒店价格)
        context.write(new Text(key),i);

            // String cityId = Bytes.toString(result.getValue("cityInfo".getBytes(), "cityId".getBytes())); 
            // byte[] value = result.getValue(family, column);
            
            // //获取酒店价格
            // //String cityId1 = Bytes.toString(result.getValue("hotel_info".getBytes(), "price".getBytes())); 
            
            // //将价格转换为double
            // Double ho =Double.parseDouble(Bytes.toString(value));
            // //将价格转换成()类型
            // DoubleWritable i = new DoubleWritable(ho);
            // String key = cityId;
            // //写出(城市id,酒店价格)
            // context.write(new  Text(key),i);
		  	/********** End *********/
        }
    }

    public static class MyTableReducer extends TableReducer<Text, DoubleWritable, ImmutableBytesWritable> {
        @Override
        public void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
            /********** Begin *********/
		 
		 
		double sum = 0;
        int count = 0;  
        for (DoubleWritable num:values){ 
            count++; 
            sum += num.get();  
        }
        double avePrice = sum / count;  
        Put put = new Put(Bytes.toBytes(key.toString()));  
        put.addColumn("average_infos".getBytes(),"price".getBytes(),Bytes.toBytes(String.valueOf(avePrice)));  
        context.write(null,put);//initTableReducerJob 设置了表名所以在这里无需设置了

            // double sum=0;
            // int count=0;
            // for(DoubleWritable value:values){
            //     count++;
            //     sum+=value.get();
            // }
            // double avePrice=sum/count;
            // //创建pit对象
            // Put put =new Put(Bytes.toBytes(key.toString()));
            // put.addColumn("average_infos".getBytes(),"price".getBytes(),Bytes.toBytes(String.valueOf(avePrice)));
            
            // context.write(null,put);        
		 
			/********** End *********/
        }

    }

    
    
    
    
    
    public int run(String[] args) throws Exception {
        //配置Job
        Configuration conf = HBaseConfiguration.create(getConf());
        conf.set("hbase.zookeeper.quorum", "127.0.0.1");  //hbase 服务地址
        conf.set("hbase.zookeeper.property.clientPort", "2181"); //端口号
        Scanner sc = new Scanner(System.in);
        String arg1 = sc.next();
        String arg2 = sc.next();
        //String arg1 = "t_city_hotels_info";
        //String arg2 = "average_table";
        try {
			HBaseUtil.createTable("average_table", new String[] {"average_infos"});
		} catch (Exception e) {
			// 创建表失败
			e.printStackTrace();
		}
        Job job = configureJob(conf,new String[]{arg1,arg2});
        return job.waitForCompletion(true) ? 0 : 1;
    }

    private Job configureJob(Configuration conf, String[] args) throws IOException {
        String tablename = args[0];
        String targetTable = args[1];
        Job job = new Job(conf,tablename);
        Scan scan = new Scan();
        scan.setCaching(300);
        scan.setCacheBlocks(false);//在mapreduce程序中千万不要设置允许缓存
        //初始化Mapreduce程序
        TableMapReduceUtil.initTableMapperJob(tablename,scan,MyMapper.class, Text.class, DoubleWritable.class,job);
        //初始化Reduce
        TableMapReduceUtil.initTableReducerJob(
                targetTable,        // output table
                MyTableReducer.class,    // reducer class
                job);
        job.setNumReduceTasks(1);
        return job;
    }
}

第2关 统计酒店评论中词频较高的词

package com.processdata;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.Word;
import com.util.HBaseUtil;
import com.vdurmont.emoji.EmojiParser;

/**
 * 词频统计
 *
 */
public class WorldCountMapReduce extends Configured implements Tool {
    

    public static class MyMapper extends TableMapper<Text, IntWritable> {
        private static byte[] family = "comment_info".getBytes();
    	private static byte[] column = "content".getBytes();
        
        @Override
        protected void map(ImmutableBytesWritable rowKey, Result result, Context context)
                throws IOException, InterruptedException {
            /********** Begin *********/
		// String content = Bytes.toString(result.getValue("comment_info".getBytes(),"content".getBytes()));
        // byte[] content = result.getValue(family,column);
		// if(content != null && content.isEmpty()){
        //     content=EmojiParser.removeAllEmojis(content);
        //     List<Word> words = WordSegmenter.seg(content);
        //     IntWritable intWritable = new IntWritable(1);
        //     for (Word word : words) {
        //         Text text = new Text(word.getText());
        //         //5、写入到context
        //         context.write(text,intWritable);
        //     } 
        // }
		 
		    byte[] value = result.getValue(family, column);
            String word = new String(value,"utf-8");
            if(!word.isEmpty()){
                String filter = EmojiParser.removeAllEmojis(word);
                List<Word> segs = WordSegmenter.seg(filter);
                for(Word cont : segs) {
                    Text text = new Text(cont.getText());
                    IntWritable v = new IntWritable(1);
                    context.write(text,v);
                }
            }
		 
			/********** End *********/
    	}
    }

    public static class MyReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {
        private static byte[] family =  "word_info".getBytes();
        private static byte[] column = "count".getBytes();
        
        @Override
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            /********** Begin *********/
		        //1.统计每一个分词的次数
                // int count = 0;
                // for (IntWritable value:values){
                //     count +=value.get();
                // }
                // //2.构建一个v3 是put  RowKey: 分词
                // Put put = new Put(Bytes.toBytes(key.toString()));
                // //添加列
                // put.addColumn(family,column,Bytes.toBytes(coumt));
                // //3.把k3,v3 写入到context
                // context.write(null,put);

            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            Put put = new Put(Bytes.toBytes(key.toString()));
            put.addColumn(family,column,Bytes.toBytes(sum));
            context.write(null,put);
		  	/********** End *********/
        }

    }

    
    
    
    
    
    public int run(String[] args) throws Exception {
        //配置Job
        Configuration conf = HBaseConfiguration.create(getConf());
        conf.set("hbase.zookeeper.quorum", "127.0.0.1");  //hbase 服务地址
        conf.set("hbase.zookeeper.property.clientPort", "2181"); //端口号
        Scanner sc = new Scanner(System.in);
        String arg1 = sc.next();
        String arg2 = sc.next();
        try {
			HBaseUtil.createTable("comment_word_count", new String[] {"word_info"});
		} catch (Exception e) {
			// 创建表失败
			e.printStackTrace();
		}
        Job job = configureJob(conf,new String[]{arg1,arg2});
        return job.waitForCompletion(true) ? 0 : 1;
    }

    private Job configureJob(Configuration conf, String[] args) throws IOException {
        String tablename = args[0];
        String targetTable = args[1];
        Job job = new Job(conf,tablename);
        Scan scan = new Scan();
        scan.setCaching(300);
        scan.setCacheBlocks(false);//在mapreduce程序中千万不要设置允许缓存
        //初始化Mapper Reduce程序
        TableMapReduceUtil.initTableMapperJob(tablename,scan,MyMapper.class, Text.class, IntWritable.class,job);
        TableMapReduceUtil.initTableReducerJob(targetTable,MyReducer.class,job);
        job.setNumReduceTasks(1);
        return job;
    }

}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个基于Python的爬虫程序,可以爬取去哪儿网的旅游数据,并进行可视化分析。具体代码如下: ```python import requests from bs4 import BeautifulSoup import pandas as pd import matplotlib.pyplot as plt # 设置请求头部信息 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } # 发送请求获取HTML页面 def get_html(url): r = requests.get(url, headers=headers) r.encoding = r.apparent_encoding return r.text # 解析HTML页面获取数据 def parse_html(html): soup = BeautifulSoup(html, 'html.parser') data_list = [] for item in soup.find_all('div', class_='gl_list'): data = { 'title': item.find('a', class_='list_title').get_text(), 'price': item.find('span', class_='price').get_text(), 'comment': item.find('div', class_='comment').get_text() } data_list.append(data) return data_list # 将数据保存到CSV文件中 def save_to_csv(data_list): df = pd.DataFrame(data_list) df.to_csv('travel_data.csv', index=False) # 可视化分析 def analyze_data(): df = pd.read_csv('travel_data.csv') # 统计价格区间占比 price_list = df['price'].tolist() bins = [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] labels = ['0-100', '100-200', '200-300', '300-400', '400-500', '500-600', '600-700', '700-800', '800-900', '900-1000+'] price_cut = pd.cut(price_list, bins=bins, labels=labels) price_cut.value_counts().plot(kind='bar', rot=0) plt.title('Price Distribution') plt.xlabel('Price Range') plt.ylabel('Count') plt.show() # 统计评论数量占比 comment_list = df['comment'].tolist() comment_count = {} for comment in comment_list: count = comment_count.get(comment, 0) count += 1 comment_count[comment] = count labels = list(comment_count.keys()) values = list(comment_count.values()) plt.pie(values, labels=labels, autopct='%1.2f%%') plt.title('Comment Distribution') plt.show() if __name__ == '__main__': url = 'https://travel.qunar.com/p-cs299895-huangshan-jingdian' html = get_html(url) data_list = parse_html(html) save_to_csv(data_list) analyze_data() ``` 运行以上代码后,程序会自动爬取去哪儿网的旅游数据,并将数据保存到CSV文件中。接着,程序会进行可视化分析,展示价格区间和评论数量的分布情况。可以根据需要对代码进行适当修改,以适应不同的爬取和分析需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值