(記)org.apache.hadoop.hive.serde

1、Hive官方建表語句

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)]
INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

2、Hive序列化以及反序列化的過程

1、序列化
Row object –> Serializer –> <key, value> –> OutputFileFormat –> HDFS files
2、反序列化
HDFS files –> InputFileFormat –> <key, value> –> Deserializer –> Row object

3、Hive-SerDe

SerDe 類型 應用
LazySimpleSerDe: SerDe(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe) ,用來處理文本文件格式:TEXTFILE  jdbc:hive2://> CREATE TABLE test_serde_lz
. . . . . . .> STORED AS TEXTFILE AS
. . . . . . .> SELECT name from employee;
ColumnarSerDe: 用來處理 RCFile 的內置 SerDe jdbc:hive2://> CREATE TABLE test_serde_cs
. . . . . . .> ROW FORMAT SERDE
. . . . . . .> 'org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe'
. . . . . . .> STORED ASRCFile AS
. . . . . . .> SELECT name from employee
RegexSerDe: 用來處理文本文件的內置 JAVA 正則表達式 SerDe jdbc:hive2://> CREATE TABLE test_serde_rex(
. . . . . . .> name string,
. . . . . . .> sex string,
. . . . . . .> age string
. . . . . . .> )
. . . . . . .> ROW FORMAT SERDE
. . . . . . .> 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
. . . . . . .> WITH SERDEPROPERTIES(
. . . . . . .> 'input.regex' = '([^,]*),([^,]*),([^,]*)',
. . . . . . .> 'output.format.string' = '%1$s %2$s %3$s'
. . . . . . .> )
. . . . . . .> STORED AS TEXTFILE;
HBaseSerDe: 內置的 SerDe,可以讓 Hive 跟 HBase 進行集成。我們可以利用 HBaseSerDe 來將 Hive 表存儲到 HBase 中。 jdbc:hive2://> CREATE TABLE test_serde_hb(
. . . . . . .> id string,
. . . . . . .> name string,
. . . . . . .> sex string,
. . . . . . .> age string
. . . . . . .> )
. . . . . . .> ROW FORMAT SERDE
. . . . . . .> 'org.apache.hadoop.hive.hbase.HBaseSerDe'
. . . . . . .> STORED BY
. . . . . . .> 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
. . . . . . .> WITH SERDEPROPERTIES (
. . . . . . .> "hbase.columns.mapping"=
. . . . . . .> ":key,info:name,info:sex,info:age"
. . . . . . .> )
. . . . . . .> TBLPROPERTIES("hbase.table.name" = "test_serde");
AvroSerDe: 用來在 Hive 表中讀寫 Avro 數據格式的內置 SerDe(參考:http://avro.apache.org/
Avro 是一個 RPC 和序列化框架,從 Hive 0.14.0 版本才本地支持 Avro :CREATE TABLE ... STORED AS AVRO 

jdbc:hive2://> CREATE TABLE test_serde_avro(
. . . . . . .> name string,
. . . . . . .> sex string,
. . . . . . .> age 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'
. . . . . . .>;

詳細內容請參考:https://cwiki.apache.org/confluence/display/Hive/AvroSerDe

ParquetHiveSerDe: 用來在 Hive 中讀寫 Parquet 數據格式的內置 SerDe。從 Hive 0.13.0 版本開始本地支持。 jdbc:hive2://> CREATE TABLE test_serde_parquet
. . . . . . .> STORED AS PARQUET AS
. . . . . . .> SELECT name from employee;
OpenCSVSerDe: 用來讀寫 CSV 數據的 SerDe. 從 Hive 0.14.0 版本才發佈的。
我們可以通過從 Github 中下載源碼進行安裝(https://github.com/ogrodnek/csv-serde )
jdbc:hive2://> CREATE TABLE test_serde_csv(
. . . . . . .> name string,
. . . . . . .> sex string,
. . . . . . .> age string
. . . . . . .>)
. . . . . . .> ROW FORMAT SERDE
. . . . . . .> 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
. . . . . . .> STORED AS TEXTFILE;
JSONSerDe: 這是一個第三方的 SerDe,用來利用 Hive 讀取 JSON 數據記錄。
你可以通知下載源碼進行安裝(https://github.com/rcongiu/Hive-JSON-Serde
jdbc:hive2://> CREATE TABLE test_serde_js(
. . . . . . .> name string,
. . . . . . .> sex string,
. . . . . . .> age string
. . . . . . .> )
. . . . . . .> ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
. . . . . . .> STORED AS TEXTFILE;

4、官方案例

RegEx

ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES 
(
"input.regex" = "<regex>"
)
STORED AS TEXTFILE;

Json

ROW FORMAT SERDE 
'org.apache.hive.hcatalog.data.JsonSerDe' 
STORED AS TEXTFILE


 ADD JAR /usr/lib/hive-hcatalog/lib/hive-hcatalog-core.jar;


CREATE TABLE my_table(a string, b bigint, ...)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
STORED AS TEXTFILE;

CSV/TSV CREATE TABLE my_table(a string, b string, ...)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = "\t",
   "quoteChar"     = "'",
   "escapeChar"    = "\\"
)  
STORED AS TEXTFILE;

5、序列化反序列話方法

1、序列化
public abstract Writable serialize(Object obj, ObjectInspector objInspector)
      throws SerDeException;
2、反序列化
public abstract Object deserialize(Writable blob) throws SerDeException;

感謝:https://blog.csdn.net/sinat_29581293/article/details/82106703

參考Hive講解網站:

https://programtalk.com/java-api-usage-examples/org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe/

https://data-flair.training/blogs/hive-serde/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章