greenplum使用PXF访问外部数据

Greenplum平台扩展框架(PXF)通过内置连接器提供对外部数据的访问,
这些连接器可以将外部数据源映射到Greenplum数据库表,通过外部表的形式查询外部数据源。


PXF可支持访问的外部数据源有HDFS,Hive和HBase。其中可以对HDFS数据进行读和写操作。


下面介绍PXF访问Hive数据的方法
一)PXF访问Hive数据
PXF Hive连接器支持多种hive的数据格式,包括TextFile,SequenceFile,RCFile,ORC,Parquet.


1、首先,创建一个文本文件:


$ vi /tmp/pxf_hive_datafile.txt


2、将以下数据添加到pxf_hive_datafile.txt; 请注意使用逗号,分隔四个字段值:


Prague,Jan,101,4875.33
Rome,Mar,87,1557.39
Bangalore,May,317,8936.99
Beijing,Jul,411,11600.67
San Francisco,Sept,156,6846.34
Paris,Nov,159,7134.56
San Francisco,Jan,113,5397.89
Prague,Dec,333,9894.77
Bangalore,Jul,271,8320.55
Beijing,Dec,100,4248.41


3、在default数据库中创建一个命名为sales_info的Hive表(这里的hive表是textfile格式):


hive> CREATE TABLE sales_info (location string, month string,
        number_of_orders int, total_sales double)
        ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
        STORED AS textfile;

4、将pxf_hive_datafile.txt示例数据文件加载到sales_info表中:


hive> LOAD DATA LOCAL INPATH '/tmp/pxf_hive_datafile.txt'
        INTO TABLE sales_info;

5、执行查询sales_info以验证您是否成功加载了数据:


hive> SELECT * FROM sales_info;


6、确定Hive表的HDFS位置


hive> DESCRIBE EXTENDED sales_info;
Detailed Table Information
...
location:hdfs://:/apps/hive/warehouse/sales_info
...


7、gp访问TextFile格式的Hive表
可以使用Hive和HiveText配置文件访问以TextFile格式存储的Hive表格数据。


示例:使用Hive配置文件
gp=# CREATE EXTERNAL TABLE salesinfo_hiveprofile(location text, month text, num_orders int, total_sales float8)
            LOCATION ('pxf://default.sales_info?PROFILE=Hive')
          FORMAT 'custom' (formatter='pxfwritable_import');


示例:使用HiveText配置文件
gp=# CREATE EXTERNAL TABLE salesinfo_hivetextprofile(location text, month text, num_orders int, total_sales float8)
             LOCATION ('pxf://default.sales_info?PROFILE=HiveText&DELIMITER=\x2c')

           FORMAT 'TEXT' (delimiter=E',');

8、gp访问RCFile格式的Hive表
PXF HiveRC配置文件提供对RCFile数据的访问


示例:使用HiveRC配置文件查询Hive表中的RCFile格式的数据。
(1)启动hive命令行并创建一个以RCFile格式存储的Hive表:
hive> CREATE TABLE sales_info_rcfile (location string, month string,
        number_of_orders int, total_sales double)
      ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
      STORED AS rcfile;
  
(2)将sales_info表中的数据插入到sales_info_rcfile:
hive> INSERT INTO TABLE sales_info_rcfile SELECT * FROM sales_info;
样本数据集的副本现在以Hive sales_info_rcfile表格中的RCFile格式存储。


(3)查询sales_info_rcfileHive表以验证数据是否正确加载:
hive> SELECT * FROM sales_info_rcfile;


(4)使用PXF HiveRC配置文件创建一个可读的Greenplum数据库外部表,引用sales_info_rcfile您在前面的步骤中创建的Hive 表。
你必须在LOCATION和FORMAT子句中指定一个分隔符。
gp=# CREATE EXTERNAL TABLE salesinfo_hivercprofile(location text, month text, num_orders int, total_sales float8)
             LOCATION ('pxf://default.sales_info_rcfile?PROFILE=HiveRC&DELIMITER=\x2c')
           FORMAT 'TEXT' (delimiter=E',');
   
(5)查询外部表格:
gp=# SELECT location, total_sales FROM salesinfo_hivercprofile;


9、gp访问ORC格式的Hive表
优化的行列(ORC)文件格式是列式文件格式,它提供了一种高效的方式来存储和访问HDFS数据。
在压缩和性能方面,ORC格式改进了文本和RCFile格式。
PXF支持ORC版本1.2.1。


-----gp支持ORC文件格式的配置文件的一些限制条件------
选择支持ORC的配置文件时,请考虑以下几点:


1)HiveORC配置文件:
一次读取一行数据。
支持列投影。
支持复杂的类型。您可以访问由数组,地图,结构和联合数据类型组成的Hive表。PXF序列化每个这些复杂的类型text。


2)HiveVectorizedORC配置文件:
一次最多可读取1024行数据。
不支持列投影。
不支持复杂类型或时间戳数据类型。

注意:HiveORC和HiveVectorizedORC配置文件当前不支持谓词下推。


(1)示例:使用HiveORC配置文件查询Hive表中的ORC格式的数据
gp外部表如下:
gp=> CREATE EXTERNAL TABLE salesinfo_hiveORCprofile(location text, month text, num_orders int, total_sales float8)
             LOCATION ('pxf://default.sales_info_ORC?PROFILE=HiveORC')
             FORMAT 'CUSTOM' (formatter='pxfwritable_import');


(2)示例:使用HiveVectorizedORC配置文件查询Hive表中的ORC格式的数据
gp外部表如下:
gp=> CREATE EXTERNAL TABLE salesinfo_hiveVectORC(location text, month text, num_orders int, total_sales float8)
             LOCATION ('pxf://default.sales_info_ORC?PROFILE=HiveVectorizedORC')
             FORMAT 'CUSTOM' (formatter='pxfwritable_import');

 
10、gp访问Parquet格式的Hive表  
定义Greenplum数据库外部表格:
gp=# CREATE EXTERNAL TABLE pxf_parquet_table (location text, month text, number_of_orders int, total_sales double precision)
    LOCATION ('pxf://default.hive_parquet_table?profile=Hive')
    FORMAT 'CUSTOM' (formatter='pxfwritable_import');

你可能感兴趣的:(greenplum)