Search in sources :

Example 1 with Context

use of org.apache.inlong.sort.flink.hive.partition.PartitionCommitPolicy.Context in project incubator-inlong by apache.

the class JdbcHivePartitionTool method main.

public static void main(String[] args) throws Exception {
    final Configuration config = ParameterTool.fromArgs(args).getConfiguration();
    final List<HivePartitionInfo> partitions = new ArrayList<>();
    final List<String> partitionValues = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String partitionName = config.getString("partition_" + i + "_name", null);
        if (partitionName != null) {
            partitions.add(new HiveFieldPartitionInfo(partitionName));
            partitionValues.add(config.getString("partition_" + i + "_value", ""));
        }
    }
    final String database = config.getString("database", null);
    final String table = config.getString("table", null);
    HiveSinkInfo hiveSinkInfo = new HiveSinkInfo(new FieldInfo[0], config.getString("metastore_address", null), database, table, config.getString("username", null), config.getString("password", null), config.getString("root_path", null), partitions.toArray(new HivePartitionInfo[0]), new TextFileFormat("\t".charAt(0)));
    JdbcHivePartitionCommitPolicy committer = new JdbcHivePartitionCommitPolicy(config, hiveSinkInfo);
    try {
        committer.commit(new Context() {

            @Override
            public String databaseName() {
                return database;
            }

            @Override
            public String tableName() {
                return table;
            }

            @Override
            public HivePartition partition() {
                HivePartition hivePartition = new HivePartition();
                List<Tuple2<String, String>> partitionPairs = new ArrayList<>();
                for (int i = 0; i < partitions.size(); i++) {
                    partitionPairs.add(Tuple2.of(partitions.get(i).getFieldName(), partitionValues.get(i)));
                }
                // noinspection unchecked
                hivePartition.setPartitions(partitionPairs.toArray(new Tuple2[0]));
                return hivePartition;
            }
        });
    } finally {
        committer.close();
    }
}
Also used : Context(org.apache.inlong.sort.flink.hive.partition.PartitionCommitPolicy.Context) Configuration(org.apache.inlong.sort.configuration.Configuration) ArrayList(java.util.ArrayList) HivePartitionInfo(org.apache.inlong.sort.protocol.sink.HiveSinkInfo.HivePartitionInfo) HiveFieldPartitionInfo(org.apache.inlong.sort.protocol.sink.HiveSinkInfo.HiveFieldPartitionInfo) HiveSinkInfo(org.apache.inlong.sort.protocol.sink.HiveSinkInfo) List(java.util.List) ArrayList(java.util.ArrayList) TextFileFormat(org.apache.inlong.sort.protocol.sink.HiveSinkInfo.TextFileFormat)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Configuration (org.apache.inlong.sort.configuration.Configuration)1 Context (org.apache.inlong.sort.flink.hive.partition.PartitionCommitPolicy.Context)1 HiveSinkInfo (org.apache.inlong.sort.protocol.sink.HiveSinkInfo)1 HiveFieldPartitionInfo (org.apache.inlong.sort.protocol.sink.HiveSinkInfo.HiveFieldPartitionInfo)1 HivePartitionInfo (org.apache.inlong.sort.protocol.sink.HiveSinkInfo.HivePartitionInfo)1 TextFileFormat (org.apache.inlong.sort.protocol.sink.HiveSinkInfo.TextFileFormat)1