O exemplo a seguir lê um banco de dados do PostgreSQL e grava os dados
em arquivos de texto. Embora este exemplo use o PostgreSQL, a configuração de outros
bancos de dados compatíveis é semelhante.
importcom.google.common.collect.ImmutableMap;importorg.apache.beam.sdk.Pipeline;importorg.apache.beam.sdk.PipelineResult;importorg.apache.beam.sdk.io.TextIO;importorg.apache.beam.sdk.managed.Managed;importorg.apache.beam.sdk.options.Description;importorg.apache.beam.sdk.options.PipelineOptions;importorg.apache.beam.sdk.options.PipelineOptionsFactory;importorg.apache.beam.sdk.transforms.MapElements;importorg.apache.beam.sdk.values.TypeDescriptors;publicclassPostgresRead{publicinterfaceOptionsextendsPipelineOptions{@Description("The JDBC URL of the PostgreSQL database to read from.")StringgetJdbcUrl();voidsetJdbcUrl(Stringvalue);@Description("The PostgresSQL table to read from.")StringgetTable();voidsetTable(Stringvalue);@Description("The username for the PostgreSQL database.")StringgetUsername();voidsetUsername(Stringvalue);@Description("The password for the PostgreSQL database.")StringgetPassword();voidsetPassword(Stringvalue);@Description("The path to write the output file. Can be a local file path, "+"a GCS path, or a path to any other supported file systems.")StringgetOutputPath();voidsetOutputPath(Stringvalue);}publicstaticPipelineResult.Statemain(String[]args){// Parse the pipeline options passed into the application. Example:// --runner=DirectRunner --jdbcUrl=$JDBC_URL --table=$TABLE// --username=$USERNAME --password=$PASSWORD --outputPath=$OUTPUT_FILE// For more information, see// https://beam.apache.org/documentation/programming-guide/#configuring-pipeline-optionsvaroptions=PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);Pipelinepipeline=createPipeline(options);returnpipeline.run().waitUntilFinish();}publicstaticPipelinecreatePipeline(Optionsoptions){// Create configuration parameters for the Managed I/O transform.ImmutableMap<String,Object>config=ImmutableMap.<String,Object>builder().put("jdbc_url",options.getJdbcUrl()).put("location",options.getTable()).put("username",options.getUsername()).put("password",options.getPassword()).build();// Build the pipeline.varpipeline=Pipeline.create(options);pipeline// Read data from a Postgres database using Managed I/O..apply(Managed.read(Managed.POSTGRES).withConfig(config)).getSinglePCollection()// Convert each row to a string..apply(MapElements.into(TypeDescriptors.strings()).via((row->String.format("%d,%s",row.getInt32("id"),row.getString("name")))))// Write strings to a text file..apply(TextIO.write().to(options.getOutputPath()).withSuffix(".txt").withNumShards(1));returnpipeline;}}
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2026-05-21 UTC."],[],[]]