gphdfs Support for Avro Files (Deprecated)

Note: The gphdfs external table protocol is deprecated and will be removed in the next major release of Greenplum Database. You can use the Greenplum Platform Extension Framework (PXF) to access Avro-format data.

You can use the Greenplum Database gphdfs protocol to access Avro files on a Hadoop file system (HDFS).

Parent topic: Accessing HDFS Data with gphdfs (Deprecated)

About the Avro File Format

An Avro file stores both the data definition (schema) and the data together in one file making it easy for programs to dynamically understand the information stored in an Avro file. The Avro schema is in JSON format, the data is in a binary format making it compact and efficient.

The following example Avro schema defines an Avro record with 3 fields:

  • name
  • favorite_number
  • favorite_color
  1. {"namespace": "example.avro",
  2. "type": "record", "name": "User",
  3. "fields": [
  4. {"name": "name", "type": "string"},
  5. {"name": "favorite_number", "type": ["int", "null"]},
  6. {"name": "favorite_color", "type": ["string", "null"]}
  7. ]
  8. }

These are two rows of data based on the schema:

  1. { "name" : "miguno" , "favorite_number" : 6 , "favorite_color" : "red" }
  2. { "name" : "BlizzardCS" , "favorite_number" : 21 , "favorite_color" : "green" }

For information about the Avro file format, see http://avro.apache.org/docs/1.7.7/

Required Avro Jar Files

Support for the Avro file format requires these jar files:

  • avro-1.7.7.jar
  • avro-tools-1.7.7.jar
  • avro-mapred-1.7.5-hadoop2.jar (available with Apache Pig)

Note: Hadoop 2 distributions include the Avro jar file $HADOOP_HOME/share/hadoop/common/lib/avro-1.7.4.jar. To avoid conflicts, you can rename the file to another file such as avro-1.7.4.jar.bak.

For the Cloudera 5.4.x Hadoop distribution, only the jar file avro-mapred-1.7.5-hadoop2.jar needs to be downloaded and installed. The distribution contains the other required jar files. The other files are included in the classpath used by the gphdfs protocol.

For information about downloading the Avro jar files, see https://avro.apache.org/releases.html.

On all the Greenplum Database hosts, ensure that the jar files are installed and are on the classpath used by the gphdfs protocol. The classpath is specified by the shell script $GPHOME/lib/hadoop/hadoop_env.sh.

As an example, if the directory $HADOOP_HOME/share/hadoop/common/lib does not exist, create it on all Greenplum Database hosts as the gpadmin user. Then, add the add the jar files to the directory on all hosts.

The hadoop_env.sh script file adds the jar files to classpath for the gphdfs protocol. This fragment in the script file adds the jar files to the classpath.

  1. if [ -d "${HADOOP_HOME}/share/hadoop/common/lib" ]; then
  2. for f in ${HADOOP_HOME}/share/hadoop/common/lib/*.jar; do
  3. CLASSPATH=${CLASSPATH}:$f;
  4. done

Avro File Format Support

The Greenplum Database gphdfs protocol supports the Avro file type as an external table:

  • Avro file format - GPDB certified with Avro version 1.7.7
  • Reading and writing Avro files
  • Support for overriding the Avro schema when reading an Avro file
  • Compressing Avro files during writing
  • Automatic Avro schema generation when writing an Avro file

Greenplum Database returns an error if the Avro file contains unsupported features or if the specified schema does not match the data.

Reading from and Writing to Avro Files

To read from or write to an Avro file, you create an external table and specify the location of the Avro file in the LOCATION clause and 'AVRO' in the FORMAT clause. For example, this is the syntax for a readable external table.

  1. CREATE EXTERNAL TABLE <tablename> (<column_spec>) LOCATION ( 'gphdfs://<location>') FORMAT 'AVRO'

The location can be an individual Avro file or a directory containing a set of Avro files. If the location specifies multiple files (a directory name or a file name containing wildcard characters), Greenplum Database uses the schema in the first file of the directory as the schema of the whole directory. For the file name you can specify the wildcard character * to match any number of characters.

You can add parameters after the file specified in the location. You add parameters with the http query string syntax that starts with ? and & between field and value pairs.

For readable external tables, the only valid parameter is schema. The gphdfs uses this schema instead of the Avro file schema when reading Avro files. See Avro Schema Overrides for Readable External Tables.

For writable external tables, you can specify schema, namespace, and parameters for compression.

ParameterValueReadable/WritableDefault Value
schemaURL_to_schema_fileRead and WriteNone.

For a readable external table:

- The specified schema overrides the schema in the Avro file. See Avro Schema Overrides

- If not specified, Greenplum Database uses the Avro file schema.

For a writable external table:

- Uses the specified schema when creating the Avro file.

- If not specified, Greenplum Database creates a schema according to the external table definition.

namespaceavro_namespaceWrite onlypublic.avro

If specified, a valid Avro namespace.

compresstrue or falseWrite onlyfalse
compression_typeblockWrite onlyOptional.

For avro format, compression_type must be block if compress is true.
codecdeflate or snappyWrite onlydeflate
codec_level (deflate codec only)integer between 1 and 9Write only6

The level controls the trade-off between speed and compression. Valid values are 1 to 9, where 1 is the fastest and 9 is the most compressed.

This set of parameters specify snappy compression:

  1. 'compress=true&codec=snappy'

These two sets of parameters specify deflate compression and are equivalent:

  1. 'compress=true&codec=deflate&codec_level=1'
  2. 'compress=true&codec_level=1'

Data Conversion When Reading Avro Files

When you create a readable external table to Avro file data, Greenplum Database converts Avro data types to Greenplum Database data types.

Note: When reading an Avro, Greenplum Database converts the Avro field data at the top level of the Avro schema to a Greenplum Database table column. This is how the gphdfs protocol converts the Avro data types.

  • An Avro primitive data type, Greenplum Database converts the data to a Greenplum Database type.
  • An Avro complex data type that is not map or record, Greenplum Database converts the data to a Greenplum Database type.
  • An Avro record that is a sub-record (nested within the top level Avro schema record), Greenplum Database converts the data XML.

This table lists the Avro primitive data types and the Greenplum Database type it is converted to.

Avro Data TypeGreenplum Database Data Type
nullSupported only in a Avro union data type. See Data Conversion when Writing Avro Files.
booleanboolean
intint or smallint
longbigint
floatreal
doubledouble
bytesbytea
stringtext

Note: When reading the Avro int data type as Greenplum Database smallint data type, you must ensure that the Avro int values do not exceed the Greenplum Database maximum smallint value. If the Avro value is too large, the Greenplum Database value will be incorrect.

The gphdfs protocol converts performs this conversion for smallint: short result = (short)IntValue;.

This table lists the Avro complex data types and the and the Greenplum Database type it is converted to.

Avro Data TypeGreenplum Database Data Type
enumint

The integer represents the zero-based position of the symbol in the schema.
arrayarray

The Greenplum Database array dimensions match the Avro array dimensions. The element type is converted from the Avro data type to the Greenplum Database data type
mapsNot supported
unionThe first non-null data type.
fixedbytea
recordXML data

Example Avro Schema

This is an example Avro schema. When reading the data from the Avro file the gphdfs protocol performs these conversions:

  • name and color data are converted to Greenplum Database sting.
  • age data is converted to Greenplum Database int.
  • clist records are converted to XML.
  1. {"namespace": "example.avro",
  2. "type": "record",
  3. "name": "User",
  4. "fields": [
  5. {"name": "name", "type": "string"},
  6. {"name": "number", "type": ["int", "null"]},
  7. {"name": "color", "type": ["string", "null"]},
  8. {"name": "clist",
  9. "type": {
  10. "type":"record",
  11. "name":"clistRecord",
  12. "fields":[
  13. {"name": "class", "type": ["string", "null"]},
  14. {"name": "score", "type": ["double", "null"]},
  15. {"name": "grade",
  16. "type": {
  17. "type":"record",
  18. "name":"inner2",
  19. "fields":[
  20. {"name":"a", "type":["double" ,"null"]},
  21. {"name":"b", "type":["string","null"]}
  22. ]}
  23. },
  24. {"name": "grade2",
  25. "type": {
  26. "type":"record",
  27. "name":"inner",
  28. "fields":[
  29. {"name":"a", "type":["double","null"]},
  30. {"name":"b", "type":["string","null"]},
  31. {"name":"c", "type":{
  32. "type": "record",
  33. "name":"inner3",
  34. "fields":[
  35. {"name":"c1", "type":["string", "null"]},
  36. {"name":"c2", "type":["int", "null"]}
  37. ]}}
  38. ]}
  39. }
  40. ]}
  41. }
  42. ]
  43. }

This XML is an example of how the gpfist protocol converts Avro data from the clist field to XML data based on the previous schema. For records nested in the Avro top-level record, gpfist protocol converts the Avro element name to the XML element name and the name of the record is an attribute of the XML element. For example, the name of the top most element clist and the type attribute is the name of the Avro record element clistRecord.

  1. <clist type="clistRecord">
  2. <class type="string">math</class>
  3. <score type="double">99.5</score>
  4. <grade type="inner2">
  5. <a type="double">88.8</a>
  6. <b type="string">subb0</b>
  7. </grade>
  8. <grade2 type="inner">
  9. <a type="double">77.7</a>
  10. <b type="string">subb20</b>
  11. <c type="inner3">
  12. <c1 type="string">subc</c1>
  13. <c2 type="int& quot;>0</c2>
  14. </c>
  15. </grade2>
  16. </clist>

Avro Schema Overrides for Readable External Tables

When you specify schema for a readable external table that specifies an Avro file as a source, Greenplum Database uses the schema when reading data from the Avro file. The specified schema overrides the Avro file schema.

You can specify a file that contains an Avro schema as part of the location paramter CREATE EXTERNAL TABLE command, to override the Avro file schema. If a set of Avro files contain different, related schemas, you can specify an Avro schema to retrieve the data common to all the files.

Greenplum Database extracts the data from the Avro files based on the field name. If an Avro file contains a field with same name, Greenplum Database reads the data , otherwise a NULL is returned.

For example, if a set of Avro files contain one of the two different schemas. This is the original schema.

  1. {
  2. "type":"record",
  3. "name":"tav2",
  4. "namespace":"public.avro",
  5. "doc":"",
  6. "fields":[
  7. {"name":"id","type":["null","int"],"doc":""},
  8. {"name":"name","type":["null","string"],"doc":""},
  9. {"name":"age","type":["null","long"],"doc":""},
  10. {"name":"birth","type":["null","string"],"doc":""}
  11. ]
  12. }

This updated schema contains a comment field.

  1. {
  2. "type":"record",
  3. "name":"tav2",
  4. "namespace":"public.avro",
  5. "doc":"",
  6. "fields":[
  7. {"name":"id","type":["null","int"],"doc":""},
  8. {"name":"name","type":["null","string"],"doc":""},
  9. {"name":"birth","type":["null","string"],"doc":""},
  10. {"name":"age","type":["null","long"],"doc":""},
  11. {"name":"comment","type":["null","string"],"doc":""}
  12. ]
  13. }

You can specify an file containing this Avro schema in a CREATE EXTERNAL TABLE command, to read the id, name, birth, and comment fields from the Avro files.

  1. {
  2. "type":"record",
  3. "name":"tav2",
  4. "namespace":"public.avro",
  5. "doc":"",
  6. "fields":[
  7. {"name":"id","type":["null","int"],"doc":""},
  8. {"name":"name","type":["null","string"],"doc":""},
  9. {"name":"birth","type":["null","string"],"doc":""},
  10. {"name":"comment","type":["null","string"],"doc":""}
  11. ]
  12. }

In this example command, the customer data is in the Avro files tmp/cust*.avro. Each file uses one of the schemas listed previously. The file avro/cust.avsc is a text file that contains the Avro schema used to override the schemas in the customer files.

  1. CREATE WRITABLE EXTERNAL TABLE cust_avro(id int, name text, birth date)
  2. LOCATION ('gphdfs://my_hdfs:8020/tmp/cust*.avro
  3. ?schema=hdfs://my_hdfs:8020/avro/cust.avsc')
  4. FORMAT 'avro';

When reading the Avro data, if Greenplum Database reads a file that does not contain a comment field, a NULL is returned for the comment data.

Data Conversion when Writing Avro Files

When you create a writable external table to write data to an Avro file, each table row is an Avro record and each table column is an Avro field. When writing an Avro file, the default compression algorithm is deflate.

For a writable external table, if the schema option is not specified, Greenplum Database creates an Avro schema for the Avro file based on the Greenplum Database external table definition. The name of the table column is the Avro field name. The data type is a union data type. See the following table:

Greenplum Database Data TypeAvro Union Data Type Definition
boolean[“boolean”, “null”]
int[“int”, “null”]
bigint[“long”, “null”]
smallint[“int”, “null”]
real[“float”, “null”]
double[“double”, “null”]
bytea[“bytes”, “null”]
text[“string”, “null”]
array[{array}, “null”]

The Greenplum Database array is converted to an Avro array with same dimensions and same element type as the Greenplum Database array.
other data types[“string”, “null”]

Data are formatted strings. The gphdfs protocol casts the data to Greenplum Database text and writes the text to the Avro file as an Avro string. For example, date and time data are formatted as date and time strings and converted to Avro string type.

You can specify a schema with the schema option. When you specify a schema, the file can be on the segment hosts or a file on the HDFS that is accessible to Greenplum Database. For a local file, the file must exist in all segment hosts in the same location. For a file on the HDFS, the file must exist in the same cluster as the data file.

This example schema option specifies a schema on an HDFS.

  1. 'schema=hdfs://mytest:8000/avro/array_simple.avsc'

This example schema option specifies a schema on the host file system.

  1. 'schema=file:///mydata/avro_schema/array_simple.avsc'

gphdfs Limitations for Avro Files

For a Greenplum Database writable external table definition, columns cannot specify the NOT NULL clause.

Greenplum Database supports only a single top-level schema in Avro files or specified with the schema parameter in the CREATE EXTERNAL TABLE command. An error is returned if Greenplum Database detects multiple top-level schemas.

Greenplum Database does not support the Avro map data type and returns an error when encountered.

When Greenplum Database reads an array from an Avro file, the array is converted to the literal text value. For example, the array [1,3] is converted to '{1,3}'.

User defined types (UDT), including array UDT, are supported. For a writable external table, the type is converted to string.

Examples

Simple CREATE EXTERNAL TABLE command that reads data from the two Avro fields id and ba.

  1. CREATE EXTERNAL TABLE avro1 (id int, ba bytea[])
  2. LOCATION ('gphdfs://my_hdfs:8020/avro/singleAvro/array2.avro')
  3. FORMAT 'avro';

CREATE WRITABLE EXTERNAL TABLE command specifies the Avro schema that is the gphdfs protocol uses to create the Avro file.

  1. CREATE WRITABLE EXTERNAL TABLE at1w(id int, names text[], nums int[])
  2. LOCATION ('gphdfs://my_hdfs:8020/tmp/at1
  3. ?schema=hdfs://my_hdfs:8020/avro/array_simple.avsc')
  4. FORMAT 'avro';

CREATE WRITABLE EXTERNAL TABLE command that writes to an Avro file and specifies a namespace for the Avro schema.

  1. CREATE WRITABLE EXTERNAL TABLE atudt1 (id int, info myt, birth date, salary numeric )
  2. LOCATION ('gphdfs://my_hdfs:8020/tmp/emp01.avro
  3. ?namespace=public.example.avro')
  4. FORMAT 'avro';