hadoop,处理一个delimeter但长度不同的文件

kgsdhlau  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(316)

我是hadoop的初学者。刚开始在Ubuntu14.04上使用hive,cdh5的 Impala 。
我的数据格式如下

A¦aa¦bb¦cc
A¦bb¦cc¦ac¦dd¦ff¦fg
B¦aa¦ac
...

第一列可以是键,但其他部分没有固定长度或固定列数。完全不同。我不知道每行有多少列。所以我把这个改成

Col1¦col2
A¦aa
A¦bb
A¦cc
A¦bb
A¦cc
A¦ac
A¦dd
....

但我花了很长时间,因为我有数百个文件,每个都有超过10万行和30 GB的大小。在我将第一种格式转换为第二种格式之后,文件大小增加了两到三倍。所以我想跳过这个过程。
问题1。如果我将原始文件导入到hdfs中,我可以在hive或impala上使用一些类似“selectdistinct col1,col2 from…”的sql吗?会有结果的

Col1¦col2
A¦aa
A¦bb
A¦cc
A¦ac
A¦dd
....

问题2。如果q1是可能的,那么sql呢?还是一样吗?”选择不同的列1,列2从…“

mqkwyuun

mqkwyuun1#

问题1是的,可以通过hive或impala读取已处理的(完成所有初始工作后)文件。我提供给你的解决方案是在Hive下面。
登录到配置单元并创建一个外部表。

CREATE EXTERNAL TABLE Test_table(column1 string, column2 string)
  ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '|'
  STORED AS TEXTFILE
  LOCATION '/location/of/your/file/on/hdfs';

根据问题中提供的输入,我假设您的分隔符为“|”,格式为文本。这将在您的数据上创建一个表。
同样的代码可以用于在impala中创建表。
问题2。
登录到配置单元并运行以下命令。

select column1,column2 from test_table;
uurv41yg

uurv41yg2#

简单明了的mapreduce程序最适合您的场景,以下代码供您参考:
以下程序一次将执行2个操作:
1) 收集行数据并转换为键值对
2) 消除重复项并只存储不同的值以输出,因为键是初始标记+值标记的组合,因此将消除重复项。示例(a | bb)将是您的键,null作为值。
代码:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class MRTranspose extends Configured implements Tool {

    public static void main(String args[]) throws Exception {
        int res = ToolRunner.run(new MRTranspose(), args);
        System.exit(res);
    }

    public int run(String[] args) throws Exception {
        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);

        Configuration conf = getConf();
        Job job = new Job(conf, this.getClass().toString());

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        job.setJobName("transpose");
        job.setJarByClass(MRTranspose.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);

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

    public static class Map extends
            Mapper<LongWritable, Text, Text, NullWritable> {

        @Override
        public void map(LongWritable key, Text value, Mapper.Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            String words[] = line.split("¦");
            for (int i = 1; i < words.length; i++) {
                context.write(new Text(words[0] + "¦" + words[i]),
                        NullWritable.get());
            }
        }
    }

    public static class Reduce extends
            Reducer<Text, NullWritable, Text, NullWritable> {

        @Override
        public void reduce(Text key, Iterable<NullWritable> values,
                Context context) throws IOException, InterruptedException {

            context.write(key, NullWritable.get());
        }
    }

}

提供原始数据文件夹作为第一个参数,第二个参数作为输出文件夹
输入文件:input.txt

A¦aa¦bb¦cc
 A¦bb¦cc¦ac¦dd¦ff¦fg
 B¦aa¦ac

输出文件:part-r-00000文件

A¦aa
A¦ac
A¦bb
A¦cc
A¦dd
A¦ff
A¦fg
B¦aa
B¦ac

最后,将mapreduce输出路径绑定为配置单元表的输入路径,以便进一步查询
希望这是有用的。

相关问题