• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Samples | Apache Thrift
  1. The application structure
  2. The POM file (pom.xml)
  3. Sample Thrift file (sample.thrift)
  4. Sample Java Thrift class (SampleThrift.java)

  1. The application structure

    mtitek-thrift-sample-eclipse

    Notes:
    - You can find information about how to install Thrift in this page: https://thrift.apache.org
    - You can find information about the ".thrift" file in this page: https://wiki.apache.org/thrift/Tutorial
  2. The POM file (pom.xml)
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>mtitek.thrift.samples</groupId>
        <artifactId>mtitek-thrift-samples</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.thrift</groupId>
                <artifactId>libthrift</artifactId>
                <version>0.9.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.4</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.thrift.tools</groupId>
                    <artifactId>maven-thrift-plugin</artifactId>
                    <version>0.1.11</version>
    
                    <executions>
                        <execution>
                            <id>thrift-compile</id>
    
                            <phase>generate-sources</phase>
    
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.12</version>
    
                    <executions>
                        <execution>
                            <id>build-helper-add-source</id>
    
                            <phase>generate-sources</phase>
    
                            <goals>
                                <goal>add-source</goal>
                            </goals>
    
                            <configuration>
                                <sources>
                                    <source>${project.build.directory}/generated-sources/thrift/</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
  3. Sample Thrift file (sample.thrift)
    namespace java mtitek.thrift.samples
    
    /*
     * Constraint.
     */
    union Constraint {
        1: required string primaryKey;
        2: required string foreignKey;
        3: required string uniqueKey;
    }
    
    /*
     * Column.
     */
    struct Column {
        1: required string columnName;
        2: required string dataType;
        3: required bool nullable;
        4: optional map<string, Constraint> constraints;
    }
    
    /*
     * Table.
     */
    struct Table {
        1: required string tableName;
        2: optional list<Column> columns;
    }
    
    /*
     * Schema.
     */
    struct Schema {
        1: required string schemaName;
        2: optional list<Table> tables;
    }
  4. Sample Java Thrift class (SampleThrift.java)
    This sample class uses the generated thrift java classes.

    If you build your code using the command "mvn package", Thrift will compile the "sample.thrift" file and generate the java classes:
    ► target/generated-sources/thrift/mtitek.thrift.samples.Schema
    ► target/generated-sources/thrift/mtitek.thrift.samples.Table
    ► target/generated-sources/thrift/mtitek.thrift.samples.Column
    ► target/generated-sources/thrift/mtitek.thrift.samples.Constraint

    package mtitek.thrift.samples;
    
    import java.util.Map;
    
    public class SampleThrift {
        public static void main(String[] args) {
            final Schema schema = initSchema();
    
            System.out.println("Schema Name: " + schema.getSchemaName());
    
            if (schema.isSetTables()) {
                for (final Table table : schema.getTables()) {
                    System.out.println("Table Name: " + table.getTableName());
    
                    if (table.isSetTableName()) {
                        for (final Column column : table.getColumns()) {
                            System.out.println("Column Name: " + column.getColumnName());
                            System.out.println("Column Data Type: " + column.getDataType());
                            System.out.println("Column isNullable: " + column.isNullable());
    
                            if (column.isSetConstraints()) {
                                for (final Map.Entry<String, Constraint> entry : column.getConstraints().entrySet()) {
                                    final String constraintName = entry.getKey();
                                    final Constraint constraint = entry.getValue();
    
                                    if (constraint.isSetPrimaryKey()) {
                                        System.out.println(
                                                "Constraint: {" + constraintName + ":" + constraint.getPrimaryKey() + "}");
                                    }
    
                                    if (constraint.isSetForeignKey()) {
                                        System.out.println(
                                                "Constraint: {" + constraintName + ":" + constraint.getForeignKey() + "}");
                                    }
    
                                    if (constraint.isSetUniqueKey()) {
                                        System.out.println(
                                                "Constraint: {" + constraintName + ":" + constraint.getUniqueKey() + "}");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    
        private static Schema initSchema() {
            final Constraint constraint1 = new Constraint();
            constraint1.setPrimaryKey("primaryKey1");
    
            final Constraint constraint2 = new Constraint();
            constraint2.setForeignKey("foreignKey1");
    
            final Constraint constraint3 = new Constraint();
            constraint3.setUniqueKey("uniqueKey1");
    
            final Column column = new Column();
            column.setColumnName("column1");
            column.setDataType("dataType1");
            column.setNullable(Boolean.TRUE);
            column.putToConstraints("constraint1", constraint1);
            column.putToConstraints("constraint2", constraint2);
            column.putToConstraints("constraint3", constraint3);
    
            final Table table = new Table();
            table.setTableName("table1");
            table.addToColumns(column);
    
            final Schema schema = new Schema();
            schema.setSchemaName("schema1");
            schema.addToTables(table);
    
            return schema;
        }
    }
© 2025  mtitek