使用 FileFormat v Serde 读取自定义文本文件

发布于 2024-12-09 08:46:59 字数 157 浏览 0 评论 0原文

Hadoop/Hive 新手在这里。我正在尝试在 Hive 中使用以自定义文本格式存储的数据。我的理解是,您可以编写自定义 FileFormat 或自定义 SerDe 类来执行此操作。是这样还是我理解错了?关于何时选择哪个选项有哪些一般准则?谢谢!

Hadoop/Hive newbie here. I am trying to use data stored in a custom text-based format with Hive. My understanding is you can either write a custom FileFormat or a custom SerDe class to do that. Is that the case or am I misunderstanding it? And what are some general guidelines on which option to choose when? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゝ杯具 2024-12-16 08:47:00

如果您使用 Hive,请编写一个 serde。请参阅这些示例:
https://github .com/apache/hive/tree/trunk/contrib/src/java/org/apache/hadoop/hive/contrib/serde2

请注意,此接口是 Hive 特定的。如果您想将自定义文件格式用于常规 hadoop 作业,则必须实现一个单独的接口(我不太确定是哪一个)。

如果您已经知道如何用另一种语言反序列化数据,则只需编写一个流作业(使用任何语言)并使用现有的库即可。

希望有帮助

If you're using Hive, write a serde. See these examples:
https://github.com/apache/hive/tree/trunk/contrib/src/java/org/apache/hadoop/hive/contrib/serde2

Note that this interface is Hive specific. If you want to use your custom file format for regular hadoop jobs, you'll have to implement a separate interface (I'm not totally sure which one).

If you already know how to deserialize data in another language, you could just write a streaming job (using any language) and use your existing libraries.

Hope that helps

九厘米的零° 2024-12-16 08:46:59

我想通了。毕竟我不必编写 Serde,编写了一个自定义的 InputFormat (扩展 org.apache.hadoop.mapred.TextInputFormat ),它返回一个自定义的 RecordReader (实现 org.apache.hadoop.txt )。 mapred.RecordReader)。 RecordReader 实现逻辑来读取和解析我的文件并返回制表符分隔的行。

我将我的表声明为

create table t2 ( 
field1 string, 
..
fieldNN float)        
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'    
STORED AS INPUTFORMAT 'namespace.CustomFileInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';

This using a native SerDe。另外,使用自定义输入格式时需要指定输出格式,因此我选择其中一种内置输出格式。

I figured it out. I did not have to write a serde after all, wrote a custom InputFormat (extends org.apache.hadoop.mapred.TextInputFormat) which returns a custom RecordReader (implements org.apache.hadoop.mapred.RecordReader<K, V>). The RecordReader implements logic to read and parse my files and returns tab delimited rows.

With that I declared my table as

create table t2 ( 
field1 string, 
..
fieldNN float)        
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'    
STORED AS INPUTFORMAT 'namespace.CustomFileInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';

This uses a native SerDe. Also, it is required to specify an output format when using a custom input format, so I choose one of the built-in output formats.

弄潮 2024-12-16 08:46:59

基本上您需要了解何时修改 SerDe 以及何时修改文件格式的区别。

来自官方文档: Hive SerDe

什么是 SerDe?
1.SerDe是“串行器和解串器”的简称。
2.Hive使用SerDe(和FileFormat)来读写表行。
3.HDFS文件-->InputFileFormat--> -->解串器 -->行对象
4.Row对象-->序列化器--> -->输出文件格式 --> HDFS 文件

所以,第 3 点和第 4 点显然可以推断出差异。
当您想要以与平常不同的方式读取记录时(其中记录由“\n”分隔),您需要具有自定义文件格式(输入/输出)。
当您想要以自定义方式解释读取记录时,您需要自定义 SerDe。

我们以常用的 JSON 格式为例。

场景一:
假设您有一个输入 json 文件,其中一行包含一条 json 记录。
所以,现在您只需要 Custom Serde 以您想要的方式解释读取记录。
不需要自定义输入输出格式,因为 1 行就是 1 条记录。

场景2:
现在,如果您有一个输入文件,其中一个 json 记录跨越多行,并且您想按原样读取它
您应该首先编写一个自定义输入格式来读取 1 个 json 记录,然后该读取的 json 记录将转到 Custom SerDe。

Basically you need to understand the difference that when to modify SerDe and and when to modify fileformat.

From official documentation: Hive SerDe

What is a SerDe?
1.SerDe is a short name for "Serializer and Deserializer."
2.Hive uses SerDe (and FileFormat) to read and write table rows.
3.HDFS files-->InputFileFormat--> --> Deserializer --> Row object
4.Row object -->Serializer --> --> OutputFileFormat --> HDFS files

So,3rd and 4th points are clearly inferring the difference.
You need to have custom fileformat(input/output) when you want to read a record in a different way than usual(where records are separated by '\n').
And you need to have customize SerDe when you want to interpret the read records in a custom way.

Let's take an example of commonly used format JSON.

Scenario 1:
Let's say you have an input json file where one line contains one json record.
So,now you just needs Custom Serde to interpret the read record in a way you want.
No need of custom inout format as 1 line will be 1 record.

Scenario 2:
Now if you have an input file where your one json record spans across multiple lines and you want to read it as it is then
you should first write a custom input format to read in 1 json record and then this read json record will go to Custom SerDe.

煮茶煮酒煮时光 2024-12-16 08:46:59

取决于您从文本文件中获得的内容。

您可以编写自定义记录读取器来解析文本日志文件并以您想要的方式返回,输入格式类会为您完成这项工作。您将使用此 jar 创建 Hive 表并加载该表中的数据。

谈到 SerDe,我的使用方式略有不同。我同时使用 InputFormat 和 SerDe,前者用于解析实际数据,后者用于稳定代表实际数据的元数据。我这样做的原因是什么?我想在 hive 表中为我的日志文件的每一行创建适当的列(不多或更少),我认为 SerDe 对我来说是完美的解决方案。

最终,如果我想要或保留这些表,我将映射这两个表以创建最终表,以便我可以进行联接以从这些表进行查询。

我喜欢Cloudera博客的解释。

http://blog. cloudera.com/blog/2012/12/how-to-use-a-serde-in-apache-hive/

Depends on what you're getting from your text file.

You can write a custom record reader to parse the text log file and return the way you want, Input format class does that job for you. You will use this jar to create the Hive table and load the data in that table.

Talking about SerDe, I use it a little differently. I use both InputFormat and SerDe, former to parse the actual data and latter to get stabilization to my metadata which represents actual data. Reason why I do that? I want to create appropriate columns(not more or less) in hive table for each row of my log file I have and I think SerDe is the perfect solution for me.

Eventually I map those two to create a final table if I want or keep those tables as it is so that I can do joins to query from those.

I like the explanation of Cloudera blog.

http://blog.cloudera.com/blog/2012/12/how-to-use-a-serde-in-apache-hive/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文