简体   繁体   English

从存储在 .avsc 文件中的表架构创建配置单元表

[英]Create hive table from table schema stored in .avsc file

I have a hive table schema stored in one hdfs file schema.avsc.我在一个 hdfs 文件 schema.avsc 中存储了一个配置单元表架构。 I want to create a hive table of the same schema and want to dump a data from another hdfs path where data is stored in HDFS file system.我想创建一个相同架构的 hive 表,并想从另一个 hdfs 路径转储数据,其中数据存储在 HDFS 文件系统中。

1 : How can i create a table ? 1:如何创建表格? 2 : How can i dump a data stored in hdfs file into created table ? 2:如何将存储在 hdfs 文件中的数据转储到创建的表中?

How can i create a table ?如何创建表?

The Apache Hive documentation on the AvroSerDe shows the syntax for creating a table based on an Avro schema stored in a file. AvroSerDe上的 Apache Hive 文档显示了基于存储在文件中的 Avro 模式创建表的语法。 For convenience, I'll repeat one of the examples here:为方便起见,我将在此处重复其中一个示例:

CREATE TABLE kst
  PARTITIONED BY (ds string)
  ROW FORMAT SERDE
  'org.apache.hadoop.hive.serde2.avro.AvroSerDe'
  STORED AS INPUTFORMAT
  'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat'
  OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat'
  TBLPROPERTIES (
    'avro.schema.url'='http://schema_provider/kst.avsc');

This example pulls the schema file from a web server.此示例从 Web 服务器中提取架构文件。 The documentation also shows other options, such as pulling from a local file, depending on your specific needs.该文档还显示了其他选项,例如根据您的特定需求从本地文件中提取。

I recommend reading the entire AvroSerDe documentation page.我建议阅读整个 AvroSerDe 文档页面。 There is a lot of useful information there about getting the most out of using Hive with Avro.那里有很多关于充分利用 Hive 和 Avro 的有用信息。

How can i dump a data stored in hdfs file into created table ?如何将存储在 hdfs 文件中的数据转储到创建的表中?

You can define an external table that references the existing HDFS files.您可以定义引用现有 HDFS 文件的外部表。 The documentation page for External Tables shows the syntax. 外部表的文档页面显示了语法。 Repeating an example:重复一个例子:

CREATE EXTERNAL TABLE page_view(viewTime INT, userid BIGINT,
     page_url STRING, referrer_url STRING,
     ip STRING COMMENT 'IP Address of the User',
     country STRING COMMENT 'country of origination')
 COMMENT 'This is the staging page view table'
 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054'
 STORED AS TEXTFILE
 LOCATION '<hdfs_location>';

After defining the external table, you can then use an INSERT-SELECT query that reads from the external table and writes to the Avro table.定义外部表后,您可以使用 INSERT-SELECT 查询从外部表读取并写入 Avro 表。 The documentation on Inserting data into Hive Tables from queries describes the INSERT-SELECT syntax. 将数据从查询插入 Hive 表的文档描述了 INSERT-SELECT 语法。 For example:例如:

FROM page_view_stg pvs
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country)
       SELECT pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, null, null, pvs.ip, pvs.cnt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM