主要是为了给spark读,符合线上环境。(注意,spark可以直接读parquet文件的)。环境搭建参考docker部署的pyspark-hive章节。

查看parquet数据:

  • parquet目录结构:

|-_SUCCESS
-part-00072-91435140-304d-4c32-921d-47c4b8c0d43e-c000.snappy.parquet
-part-00000-91435140-304d-4c32-921d-47c4b8c0d43e-c000.snappy.parquet

  • 查看数据结构,其中需要下载 parquet-tools-1.6.0rc3-SNAPSHOT.jar 工具

    1. java -jar parquet-tools-1.6.0rc3-SNAPSHOT.jar schema -d test.parquet
    1. message spark_schema {
    2. optional binary __key__ (UTF8);
    3. optional binary __error__ (UTF8);
    4. optional binary source (UTF8);
    5. optional binary host (UTF8);
    6. optional binary inserttime (UTF8);
    7. optional int32 match;
    8. optional int32 tags;
    9. .....
  • 查看内容

    1. java -jar parquet-tools-1.6.0rc3-SNAPSHOT.jar head -n 2 test.parquet
  • parquet 和 hive 的 字段数据类型映射关系

    1. BINARY -> STRING
    2. BOOLEAN -> BOOLEAN
    3. DOUBLE -> DOUBLE
    4. FLOAT -> FLOAT
    5. INT32 -> INT
    6. INT64 -> BIGINT
    7. INT96 -> TIMESTAMP
    8. BINARY + OriginalType UTF8 -> STRING
    9. BINARY + OriginalType DECIMAL -> DECIMAL

    hive 命令

    创建表

    ```sql

    创建表时可选择数据存储格式

    hive> use default; hive> create table t_order(id string,create_time string,amount float,uid string);

    表建好后,会在所属的库目录中生成一个表目录

    hive> /user/hive/warehouse/default.db/t_order

  1. **Tips**:分区 partitioned by (date string)
  2. <a name="1d0Il"></a>
  3. ### 删除表
  4. ```sql
  5. 1. 仅删除表中数据,保留表结构
  6. hive> truncate table test;
  7. 2. 删除表
  8. hive> drop table if exists test;
  9. 3. 删除库
  10. hive> drop database if exists testDB;

本地导入数据

  1. hive> load data local inpath '/path/test.parquet' into table test_database.test_table_name;

导入HDFS中的数据

  1. hive> load data inpath '/path/test.parquet' into table test_database.test_table_name;

Tips:区别是没有 local

pandas 数据导成 parquet 文件

先安装 pyarrowfastparquet

  1. import pandas as pd
  2. >>> df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  3. >>> df.to_parquet('df.parquet.gzip', compression='gzip')
  4. >>> pd.read_parquet('df.parquet.gzip')
  5. col1 col2
  6. 0 1 3
  7. 1 2 4