Day7实习日记

今天做Hadoop实现的电商实战

项目要求

根据电商日志文件,分析:

  1. 统计页面浏览量(每行记录就是一次浏览)

  2. 统计各个省份的浏览量 (需要解析IP)

  3. 日志的ETL操作(ETL:数据从来源端经过抽取(Extract)、转换(Transform)、加载(Load)至目的端的过程)

为什么要ETL:没有必要解析出所有数据,只需要解析出有价值的字段即可。本项目中需要解析出:ip、url、pageId(topicId对应的页面Id)、country、province、city


1.统计页面浏览量(每行记录就是一次浏览)
这跟WordCount差不多一个一次,用计数器读一遍加个1就行

  • Mapper
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MyMapper1 extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
            context.write(new Text("key"), new IntWritable(1));
    }
}
  • Reducer
public class MyReducer1 extends Reducer<Text, IntWritable, NullWritable, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        int count = 0;
        for (IntWritable value : values) {
            count ++;
        }
        context.write(NullWritable.get(), new IntWritable(count));
    }

}
  • Main
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration configuration = new Configuration();

        Job job = Job.getInstance(configuration);
        job.setJarByClass(Main.class);

        job.setMapperClass(MyMapper1.class);
        job.setReducerClass(MyReducer1.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path("input/trackinfo_20130721.txt"));
        FileOutputFormat.setOutputPath(job, new Path("output/ot1"));

        boolean result = job.waitForCompletion(true);
        System.out.println(result?"ok":"0");
    }
}
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了升级案例7-3的日记本,我们可以加入一些新的功能,如日期选择器、关键词搜索等。 首先,我们可以使用PyQt5中的QCalendarWidget类来添加日期选择器。我们还可以添加一个搜索框和按钮,以便用户可以按关键词搜索他们的日记。这需要使用QLineEdit和QPushButton类。 在主窗口中,我们可以创建一个垂直布局,并将QCalendarWidget、QLineEdit、QPushButton和QTextEdit添加到其中。这将使用户能够选择日期、输入关键词并搜索日记,然后在QTextEdit中查看结果。 我们还可以添加一个菜单栏,以便用户可以保存和打开他们的日记。这需要使用QMenuBar、QMenu和QAction类。 最后,我们可以将日记保存为文本文件,并使用QFileDialog类选择文件的保存位置。当用户打开日记时,我们将使用QFileDialog类选择文件的位置,并将其加载到QTextEdit中。 以下是一个简单的代码示例: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget, QVBoxLayout, QLineEdit, QPushButton, QTextEdit, QMenuBar, QMenu, QAction, QFileDialog from datetime import datetime class Diary(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('日记本') self.setGeometry(100, 100, 800, 600) # 创建菜单栏 menubar = self.menuBar() fileMenu = menubar.addMenu('文件') # 创建菜单项 saveAction = QAction('保存日记', self) saveAction.triggered.connect(self.saveDiary) fileMenu.addAction(saveAction) openAction = QAction('打开日记', self) openAction.triggered.connect(self.openDiary) fileMenu.addAction(openAction) # 创建布局 layout = QVBoxLayout() # 添加日历控件 self.calendar = QCalendarWidget() self.calendar.clicked.connect(self.showDiary) layout.addWidget(self.calendar) # 添加搜索框和按钮 searchLayout = QHBoxLayout() self.searchBox = QLineEdit() self.searchButton = QPushButton('搜索') self.searchButton.clicked.connect(self.searchDiary) searchLayout.addWidget(self.searchBox) searchLayout.addWidget(self.searchButton) layout.addLayout(searchLayout) # 添加文本框 self.textEdit = QTextEdit() layout.addWidget(self.textEdit) # 设置主窗口的中心部分 centralWidget = QWidget() centralWidget.setLayout(layout) self.setCentralWidget(centralWidget) def saveDiary(self): fileName, _ = QFileDialog.getSaveFileName(self, '保存日记', '', 'Text Files (*.txt)') if fileName: with open(fileName, 'w') as f: f.write(self.textEdit.toPlainText()) def openDiary(self): fileName, _ = QFileDialog.getOpenFileName(self, '打开日记', '', 'Text Files (*.txt)') if fileName: with open(fileName, 'r') as f: self.textEdit.setPlainText(f.read()) def showDiary(self): date = self.calendar.selectedDate() fileName = date.toString('yyyy-MM-dd') + '.txt' try: with open(fileName, 'r') as f: self.textEdit.setPlainText(f.read()) except FileNotFoundError: self.textEdit.setPlainText('') def searchDiary(self): keyword = self.searchBox.text() if keyword: result = '' for year in range(2000, datetime.now().year+1): for month in range(1, 13): for day in range(1, 32): try: with open(f'{year}-{month:02d}-{day:02d}.txt', 'r') as f: content = f.read() if keyword in content: result += f'{year}-{month:02d}-{day:02d}\n{content}\n\n' except FileNotFoundError: pass if result: self.textEdit.setPlainText(result) else: self.textEdit.setPlainText('未找到相关日记。') else: self.showDiary() if __name__ == '__main__': app = QApplication([]) diary = Diary() diary.show() app.exec_() ``` 在这个例子中,我们添加了一个菜单栏,其中包含保存和打开日记的选项。我们还使用了QCalendarWidget、QLineEdit和QPushButton类添加了一些新的控件。最后,我们将日记保存为文本文件,并使用QFileDialog类选择文件的位置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值