Hadoop Partition使用实例<转>

1、为何使用Partitioner,主要是想reduce的结果能够根据key再次分类输出到不同的文件夹中。

2、结果能够直观,同时做到对数据结果的简单的统计分析。


1、输入的数据文件内容如下(1条数据内容少,1条数据内容超长,3条数据内容正常):

kaka    1       28
hua     0       26
chao    1
tao     1       22
mao     0       29      22

2、目的是为了分别输出结果,正确的结果输出到一个文本,太短的数据输出到一个文本,太长的输出到一个文本,共三个文本输出。


代码


    package com.partition;

    import java.io.IOException;
    import java.util.*;

    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.conf.*;
    import org.apache.hadoop.io.*;
    import org.apache.hadoop.mapred.*;
    import org.apache.hadoop.util.*;

    import com.hadoop.mapred.WordCount.Map;
import com.hadoop.mapred.WordCount.Reduce;

    //Partitioner函数的使用

    public class MyPartitioner {
        //Map函数
        public static class MyMap extends MapReduceBase implements Mapper<LongWritable,Text,Text,Text>{
            
            
            public void map(LongWritable key, Text value,
                    OutputCollector<Text, Text> output, Reporter reporter)
                    throws IOException {
                
                String [] arr_value = value.toString().split("\t");
                Text word1 = new Text();
                Text word2 = new Text();
                
                if(arr_value.length > 3){
                    word1.set("long");
                    word2.set(value);
                }else if(arr_value.length < 3){
                    word1.set("short");
                    word2.set(value);
                }else {
                    word1.set("right");
                    word2.set(value);
                }
                
                output.collect(word1, word2);
            }        
        }        
        
        public static class MyPartitionerPar implements Partitioner<Text, Text> {

            @Override
            public int getPartition(Text key, Text value, int numPartitions) {
                int result = 0;
                System.out.println("numPartitions--"+numPartitions);
                if (key.toString().equals("long")) {
                    result = 0 % numPartitions;
                } else if (key.toString().equals("short")) {
                    result = 1 % numPartitions;
                } else if (key.toString().equals("right")) {
                    result = 2 % numPartitions;
                }
                return result;
            }

            @Override
            public void configure(JobConf arg0) {
                // TODO Auto-generated method stub
                
            }

        }
        
        public static class MyReduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> {
             public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
               int sum = 0;
               while (values.hasNext()) {
                   output.collect(key, new Text(values.next().getBytes()));
               }               
             }
           }
        
        public static void main(String[] args) throws Exception {
             JobConf conf = new JobConf(MyPartitioner.class);
             conf.setJobName("MyPartitioner");
             conf.setNumReduceTasks(3);
             
             conf.setMapOutputKeyClass(Text.class);
             conf.setMapOutputValueClass(Text.class);
             
             conf.setPartitionerClass(MyPartitionerPar.class);

             conf.setOutputKeyClass(Text.class);
             conf.setOutputValueClass(Text.class);
        
             conf.setMapperClass(MyMap.class);
             conf.setReducerClass(MyReduce.class);
        
             conf.setInputFormat(TextInputFormat.class);
             conf.setOutputFormat(TextOutputFormat.class);
        
             FileInputFormat.setInputPaths(conf, new Path(args[0]));
              FileOutputFormat.setOutputPath(conf, new Path(args[1]));
        
             JobClient.runJob(conf);
        }    
    }

### Flink 1.14 连接到 Hadoop 的配置方法及示例 Flink 可以通过多种方式与 Hadoop 生态系统集成,包括读取和写入 HDFS 文件以及访问 Hive 表。以下是关于如何在 Flink 1.14 中配置并连接到 Hadoop 的详细说明。 #### 1. 添加依赖项 要使 Flink 能够与 Hadoop 集成,需确保项目中包含了必要的依赖库。对于 Maven 构建工具,可以添加以下依赖: ```xml <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-hdfs_${scala.binary.version}</artifactId> <version>1.14.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> ``` 这些依赖允许 Flink 访问 HDFS 并支持其他 Hadoop 功能[^1]。 --- #### 2. 配置 `core-site.xml` 和 `hdfs-site.xml` 将 Hadoop 的核心配置文件 (`core-site.xml`, `hdfs-site.xml`) 放置于 Flink 的类路径下(通常位于 `$FLINK_HOME/conf/` 或打包的应用程序资源目录)。这使得 Flink 能识别 Hadoop 的集群设置。 如果需要动态加载配置,可以通过 Java 系统属性传递参数,例如: ```bash export HADOOP_CONF_DIR=/path/to/hadoop/conf/ ``` 或者,在启动 Flink JobManager 和 TaskManager 时显式指定配置路径: ```bash ./bin/start-cluster.sh -Dfs.defaultFS=hdfs://namenode:8020/ ``` --- #### 3. 使用 Table API/SQl 连接 Hive 当需要操作存储于 Hive 中的数据时,可利用 `HiveCatalog` 将 Hive 数据源注册至 Flink 的表环境中。具体步骤如下: ##### (a) 创建 Hive Catalog 定义一个名为 `hive_catalog` 的 HiveCatalog 对象,并将其作为默认的元数据管理器。 ```sql CREATE CATALOG hive_catalog WITH ( 'type'='hive', 'hive-conf-dir'='/path/to/hive/conf/', 'default-database'='default' ); ``` 此命令会初始化一个指向现有 Hive 实例的接口。 ##### (b) 切换至目标数据库 假设已创建了一个新的数据库用于测试目的,则可通过以下语句切换上下文: ```sql USE hadoop_catalog.iceberg_db; ``` 验证执行成功的信息应类似于 `[INFO] Execute statement succeed.`[^2]。 --- #### 4. 执行窗口聚合查询 针对实时流处理场景下的需求,比如统计最近一小时内某产品的销售额总和,可以采用基于时间范围的滑动窗口函数完成分析任务。下面给出一段示范代码片段: ```sql SELECT order_id, order_time, amount, SUM(amount) OVER( PARTITION BY product ORDER BY order_time RANGE BETWEEN INTERVAL '1' HOUR PRECEDING AND CURRENT ROW ) AS one_hour_prod_amount_sum FROM Orders; ``` 上述脚本展示了如何应用带有时间间隔约束条件的累积求和逻辑[^4]。 --- #### 总结 综上所述,从基础环境搭建到高级功能实现,本文介绍了完整的流程来帮助开发者快速掌握如何让 Apache Flink 版本号为 1.14 的应用程序无缝对接 Apache Hadoop 平台及其附属组件服务。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值