hive - 使用 Hive 读取 Hadoop SequenceFiles

标签 hive sequencefile

我有一些来自 Common Crawl 的映射数据,它们以 SequenceFile 格式存储。我反复尝试将这些数据“按原样”与 Hive 一起使用,以便我可以在各个阶段对其进行查询和采样。但是我的作业输出中总是出现以下错误:

LazySimpleSerDe: expects either BytesWritable or Text object!

我什至构建了一个更简单(更小)的 [Text, LongWritable] 记录数据集,但也失败了。如果我将数据输出为文本格式,然后在其上创建一个表,它工作正常:
hive> create external table page_urls_1346823845675
    >     (pageurl string, xcount bigint) 
    >     location 's3://mybucket/text-parse/1346823845675/';
OK
Time taken: 0.434 seconds
hive> select * from page_urls_1346823845675 limit 10;
OK
http://0-italy.com/tag/package-deals    643    NULL
http://011.hebiichigo.com/d63e83abff92df5f5913827798251276/d1ca3aaf52b41acd68ebb3bf69079bd1.html    9    NULL
http://01fishing.com/fly-fishing-knots/    3437    NULL
http://01fishing.com/flyin-slab-creek/    1005    NULL
...

我尝试使用自定义输入格式:
// My custom input class--very simple
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.SequenceFileInputFormat;
public class UrlXCountDataInputFormat extends 
     SequenceFileInputFormat<Text, LongWritable> {  }

我创建表然后:
create external table page_urls_1346823845675_seq 
  (pageurl string, xcount bigint) 
  stored as inputformat 'my.package.io.UrlXCountDataInputFormat' 
  outputformat 'org.apache.hadoop.mapred.SequenceFileOutputFormat'  
  location 's3://mybucket/seq-parse/1346823845675/';

但我仍然遇到相同的 SerDer 错误。

我确定我在这里缺少一些非常基本的东西,但我似乎无法正确理解。此外,我必须能够就地解析 SequenceFiles(即我无法将数据转换为文本)。所以我需要为我的项目的 future 部分找出 SequenceFile 方法。

解决方案:
正如@mark-grover 在下面指出的那样,问题在于 Hive 默认会忽略 key 。只有一列(即只有值),serder 无法映射我的第二列。

解决方案是使用比我最初使用的更复杂的自定义 InputFormat。我在指向 Git 的链接中找到了一个关于使用键而不是值的答案,然后我修改了它以满足我的需要:从内部 SequenceFile.Reader 中获取键和值,然后将它们组合到最终的 BytesWritable 中。 IE。像这样(来自自定义阅读器,因为这是所有艰苦工作发生的地方):
// I used generics so I can use this all with 
// other output files with just a small amount
// of additional code ...
public abstract class HiveKeyValueSequenceFileReader<K,V> implements RecordReader<K, BytesWritable> {

    public synchronized boolean next(K key, BytesWritable value) throws IOException {
        if (!more) return false;

        long pos = in.getPosition();
        V trueValue = (V) ReflectionUtils.newInstance(in.getValueClass(), conf);
        boolean remaining = in.next((Writable)key, (Writable)trueValue);
        if (remaining) combineKeyValue(key, trueValue, value);
        if (pos >= end && in.syncSeen()) {
          more = false;
        } else {
          more = remaining;
        }
        return more;
    }

    protected abstract void combineKeyValue(K key, V trueValue, BytesWritable newValue);

}

// from my final implementation
public class UrlXCountDataReader extends HiveKeyValueSequenceFileReader<Text,LongWritable>
    @Override
    protected void combineKeyValue(Text key, LongWritable trueValue, BytesWritable newValue) {
        // TODO I think we need to use straight bytes--I'm not sure this works?
        StringBuilder builder = new StringBuilder();
        builder.append(key);
        builder.append('\001');
        builder.append(trueValue.get());
        newValue.set(new BytesWritable(builder.toString().getBytes()) );
    }
}

有了这个,我得到了我所有的专栏!
http://0-italy.com/tag/package-deals    643
http://011.hebiichigo.com/d63e83abff92df5f5913827798251276/d1ca3aaf52b41acd68ebb3bf69079bd1.html    9
http://01fishing.com/fly-fishing-knots/ 3437
http://01fishing.com/flyin-slab-creek/  1005
http://01fishing.com/pflueger-1195x-automatic-fly-reels/    1999

最佳答案

不确定这是否会影响您,但 Hive 在读取 SequenceFiles 时会忽略键。您可能需要创建一个自定义 InputFormat(除非您可以在网上找到一个:-))

引用:http://mail-archives.apache.org/mod_mbox/hive-user/200910.mbox/%3C5573211B-634D-4BB0-9123-E389D90A786C@metaweb.com%3E

关于hive - 使用 Hive 读取 Hadoop SequenceFiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203770/

相关文章:

json - nginx日志解析工具

java - 如何从Spark中的序列文件中提取行的范围?

sql - hive 中两条记录之间的差异

struct - 使用hive复杂的struct数据类型,如何使用where子句编写查询

hadoop - hive 表中 count(*) 的错误结果

java - Hadoop 序列文件大小

java - 附加到现有序列文件会覆盖内容

hive - 如何使用其他表的数据更新 Hive 中表的某些列

java - 从Hadoop的手动生成(硬编码)序列文件中读取时出现ChecksumException?

hadoop - 在Hadoop 2.0中读取sequencefile