基于Hadoop平台使用MapReduce统计某银行信用卡违约用户数量

统计某银行信用卡违约用户数量

csv下载地址

违约规则:AY_1~PAY_6:PAY_1为2005年9月的还款情况;PAY_2为2005年8月的还款情况;…;PAY_6为2005年4月的还款情况。BILL_AMT1~BILL_AMT6和PAY_AMT1~PAY_AMT6中数字标识的含义也是如此。
PAY_1~PAY_6的取值含义为:0 = 及时还;1 = 还款延迟一个月;2 = 还款延迟两个月;3 = 还款延迟三个月;…;9 = 还款延迟九个月及以上。
每月的支付金额PAY_AMT不能低于银行规定的当月最低还款额,否则就是违约。如果支付金额PAY_AMT大于上月账单金额BILL_AMT则视为及时还,剩余金额存入信用卡留做下次消费;如果支付金额小于上月账单金额但高于最低还款额则视为延迟还款。

要求:

在Hadoop平台编程实现统计银行违约用户数量

实现:

porn.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Hadoop</groupId>
    <artifactId>BankDefaulter_MapReduce</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>au.com.bytecode</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.4</version>
        </dependency>



    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>bankfinddefaulter.FindDefaulter</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

FindDefaulter.java

package bankfinddefaulter;


import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;

public class FindDefaulter {

    public static void main(String[] args) throws Throwable {
        // TODO Auto-generated method stub
        Job job = new Job();
        job.setJarByClass(FindDefaulter.class);

        FileInputFormat.addInputPath(job, new Path("hdfs://172.18.74.236:9000/input/UCI_Credit_Card.csv"));  // csv文件所在目录
        FileOutputFormat.setOutputPath(job, new Path("hdfs://172.18.74.236:9000/out"));                   // 设置输出文件目录

        // 关联自定义的mapper和reducer
        job.setMapperClass(BankMapper.class);
        job.setReducerClass(BankReducer.class);

        // 设置map输入数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 设置最终输出数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        System.exit(job.waitForCompletion(true)?0:1);
    }

}

BankReducer.java

package bankfinddefaulter;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class BankReducer extends
        Reducer<Text, IntWritable, Text, 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(key, new IntWritable(count));

    }

}

BankMapper.java

package bankfinddefaulter;

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;

import au.com.bytecode.opencsv.CSVParser;

public class BankMapper extends
        Mapper<LongWritable, Text, Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        if (key.get() > 0) {

            String[] lines = new CSVParser().parseLine(value.toString());

            //选取csv文件中第25行数据进行统计
            context.write(new Text(lines[24]), new IntWritable(1));

        }
    }
}

方法1

在IDEA中编写好业务代码,是用mvn将程序打成jar包,上传到hdoop平台之后运行

在IDEA下的Terminal控制台中输入

mvn clean package

此命令基于pom.xml中

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>bankfinddefaulter.FindDefaulter</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Hadoop平台运行jar包

将项目代码打成jar包后,上传到hadoop平台,输入

hadoop jar Hadoop_API-1.0-SNAPSHOT-jar-with-dependencies.jar

其中1为违约用户数量, 共有6636位用户违约

方法2

在IDEA中本地运行

在Windows设置好Hadoop开发环境后, 运行FindDefaulter.java

172.18.74.236:50070/output 目录中下载查看输出

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【资源说明】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载食用体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【项目介绍】 基于HadoopMapReduce统计银行信用卡违约用户数量项目源码+项目说明+数据.zip 统计银行信用卡违约用户数量 数据集见文件UCI_Credit_Card.csv 违约规则:AY_1~PAY_6:PAY_1为2005年9月的还款情况;PAY_2为2005年8月的还款情况;…;PAY_6为2005年4月的还款情况。BILL_AMT1~BILL_AMT6和PAY_AMT1~PAY_AMT6中数字标识的含义也是如此。 PAY_1~PAY_6的取值含义为:0 = 及时还;1 = 还款延迟一个月;2 = 还款延迟两个月;3 = 还款延迟三个月;…;9 = 还款延迟九个月及以上。 每月的支付金额PAY_AMT不能低于银行规定的当月最低还款额,否则就是违约。如果支付金额PAY_AMT大于上月账单金额BILL_AMT则视为及时还,剩余金额存入信用卡留做下次消费;如果支付金额小于上月账单金额但高于最低还款额则视为延迟还款。 要求: 在Hadoop平台编程实现统计银行违约用户数量 实现: 在IDEA中编写好业务代码,使用mvn将程序打成jar包,上传到hdoop平台之后运行 其中1为违约用户数量, 共有6636位用户违约
基于Hadoop Streaming进行MapReduce实践需要遵循以下步骤: 1. 编写Mapper和Reducer代码,将其保存为可执行文件或脚本。Mapper和Reducer可以使用任何编程语言,只要它们可以在命令行上运行即可。 2. 在Hadoop集群上启动Hadoop Streaming作业,并指定Mapper和Reducer的可执行文件或脚本路径。 3. 将输入数据上传到HDFS中,并指定输入路径作为Hadoop Streaming作业的输入。 4. 指定输出路径作为Hadoop Streaming作业的输出。 5. 启动Hadoop Streaming作业,并等待作业完成。 下面是一个简单的示例,展示如何使用Hadoop Streaming进行单词计数: 1. 编写Mapper代码,将其保存为Python脚本: ```python #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words = line.split() for word in words: print(word + '\t' + '1') ``` 2. 编写Reducer代码,将其保存为Python脚本: ```python #!/usr/bin/env python import sys current_word = None current_count = 0 word = None for line in sys.stdin: line = line.strip() word, count = line.split('\t', 1) count = int(count) if current_word == word: current_count += count else: if current_word: print(current_word + '\t' + str(current_count)) current_count = count current_word = word if current_word == word: print(current_word + '\t' + str(current_count)) ``` 3. 将Mapper和Reducer保存为可执行文件,并上传到HDFS中。 4. 将输入数据上传到HDFS中,例如: ```bash $ hadoop fs -put input.txt /input ``` 5. 启动Hadoop Streaming作业: ```bash $ hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.7.3.jar \ -file mapper.py -mapper mapper.py \ -file reducer.py -reducer reducer.py \ -input /input -output /output ``` 在这个命令中,我们指定了Mapper和Reducer的Python脚本路径,以及输入和输出路径。Hadoop Streaming会自动将输入数据分割成小块,并将它们分配给Mapper进行处理。Mapper将每个单词映射到一个计数器,并将其发送到Reducer进行聚合。最终结果被写入到输出路径中。 6. 查看输出结果: ```bash $ hadoop fs -cat /output/* ``` 这将显示单词和它们的计数器,例如: ```text hello 2 world 1 ``` 这就是基于Hadoop Streaming进行MapReduce实践的基本步骤。你可以使用不同的编程语言和算法来解决不同的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值