Search in sources :

Example 1 with ConstantField

use of org.apache.hop.pipeline.transforms.constant.ConstantField in project hop by apache.

the class BeamPipelineMetaUtil method generateFilterRowsPipelineMeta.

public static final PipelineMeta generateFilterRowsPipelineMeta(String transname, String inputTransformName, String outputTransformName, IHopMetadataProvider metadataProvider) throws Exception {
    IHopMetadataSerializer<FileDefinition> serializer = metadataProvider.getSerializer(FileDefinition.class);
    FileDefinition customerFileDefinition = createCustomersInputFileDefinition();
    serializer.save(customerFileDefinition);
    PipelineMeta pipelineMeta = new PipelineMeta();
    pipelineMeta.setName(transname);
    pipelineMeta.setMetadataProvider(metadataProvider);
    // Add the io transform
    // 
    BeamInputMeta beamInputMeta = new BeamInputMeta();
    beamInputMeta.setInputLocation(PipelineTestBase.INPUT_CUSTOMERS_FILE);
    beamInputMeta.setFileDefinitionName(customerFileDefinition.getName());
    TransformMeta beamInputTransformMeta = new TransformMeta(inputTransformName, beamInputMeta);
    beamInputTransformMeta.setTransformPluginId(BeamConst.STRING_BEAM_INPUT_PLUGIN_ID);
    pipelineMeta.addTransform(beamInputTransformMeta);
    // Add 2 add constants transforms A and B
    // 
    ConstantMeta constantA = new ConstantMeta();
    ConstantField cf1 = new ConstantField("label", "String", "< 'k'");
    constantA.getFields().add(cf1);
    TransformMeta constantAMeta = new TransformMeta("A", constantA);
    pipelineMeta.addTransform(constantAMeta);
    ConstantMeta constantB = new ConstantMeta();
    ConstantField cf2 = new ConstantField("label", "String", ">= 'k'");
    constantB.getFields().add(cf2);
    TransformMeta constantBMeta = new TransformMeta("B", constantB);
    pipelineMeta.addTransform(constantBMeta);
    // Add Filter rows transform looking for customers name > "k"
    // Send rows to A (true) and B (false)
    // 
    FilterRowsMeta filter = new FilterRowsMeta();
    filter.getCondition().setLeftValuename("name");
    filter.getCondition().setFunction(Condition.FUNC_SMALLER);
    filter.getCondition().setRightExact(new ValueMetaAndData("value", "k"));
    filter.setTrueTransformName("A");
    filter.setFalseTransformName("B");
    TransformMeta filterMeta = new TransformMeta("Filter", filter);
    pipelineMeta.addTransform(filterMeta);
    pipelineMeta.addPipelineHop(new PipelineHopMeta(beamInputTransformMeta, filterMeta));
    pipelineMeta.addPipelineHop(new PipelineHopMeta(filterMeta, constantAMeta));
    pipelineMeta.addPipelineHop(new PipelineHopMeta(filterMeta, constantBMeta));
    // Add a dummy behind it all to flatten/merge the data again...
    // 
    DummyMeta dummyPipelineMeta = new DummyMeta();
    TransformMeta dummyTransformMeta = new TransformMeta("Flatten", dummyPipelineMeta);
    pipelineMeta.addTransform(dummyTransformMeta);
    pipelineMeta.addPipelineHop(new PipelineHopMeta(constantAMeta, dummyTransformMeta));
    pipelineMeta.addPipelineHop(new PipelineHopMeta(constantBMeta, dummyTransformMeta));
    // Add the output transform
    // 
    BeamOutputMeta beamOutputMeta = new BeamOutputMeta();
    beamOutputMeta.setOutputLocation("/tmp/customers/output/");
    beamOutputMeta.setFileDefinitionName(null);
    beamOutputMeta.setFilePrefix("filter-test");
    beamOutputMeta.setFileSuffix(".csv");
    // Not yet supported
    beamOutputMeta.setWindowed(false);
    TransformMeta beamOutputTransformMeta = new TransformMeta(outputTransformName, beamOutputMeta);
    beamOutputTransformMeta.setTransformPluginId("BeamOutput");
    pipelineMeta.addTransform(beamOutputTransformMeta);
    pipelineMeta.addPipelineHop(new PipelineHopMeta(dummyTransformMeta, beamOutputTransformMeta));
    return pipelineMeta;
}
Also used : FilterRowsMeta(org.apache.hop.pipeline.transforms.filterrows.FilterRowsMeta) DummyMeta(org.apache.hop.pipeline.transforms.dummy.DummyMeta) FileDefinition(org.apache.hop.beam.metadata.FileDefinition) ConstantField(org.apache.hop.pipeline.transforms.constant.ConstantField) BeamOutputMeta(org.apache.hop.beam.transforms.io.BeamOutputMeta) BeamInputMeta(org.apache.hop.beam.transforms.io.BeamInputMeta) TransformMeta(org.apache.hop.pipeline.transform.TransformMeta) PipelineHopMeta(org.apache.hop.pipeline.PipelineHopMeta) ValueMetaAndData(org.apache.hop.core.row.ValueMetaAndData) ConstantMeta(org.apache.hop.pipeline.transforms.constant.ConstantMeta) PipelineMeta(org.apache.hop.pipeline.PipelineMeta)

Example 2 with ConstantField

use of org.apache.hop.pipeline.transforms.constant.ConstantField in project hop by apache.

the class BeamPipelineMetaUtil method generateSwitchCasePipelineMeta.

public static final PipelineMeta generateSwitchCasePipelineMeta(String transname, String inputTransformName, String outputTransformName, IHopMetadataProvider metadataProvider) throws Exception {
    IHopMetadataSerializer<FileDefinition> serializer = metadataProvider.getSerializer(FileDefinition.class);
    FileDefinition customerFileDefinition = createCustomersInputFileDefinition();
    serializer.save(customerFileDefinition);
    PipelineMeta pipelineMeta = new PipelineMeta();
    pipelineMeta.setName(transname);
    pipelineMeta.setMetadataProvider(metadataProvider);
    // Add the io transform
    // 
    BeamInputMeta beamInputMeta = new BeamInputMeta();
    beamInputMeta.setInputLocation(PipelineTestBase.INPUT_CUSTOMERS_FILE);
    beamInputMeta.setFileDefinitionName(customerFileDefinition.getName());
    TransformMeta beamInputTransformMeta = new TransformMeta(inputTransformName, beamInputMeta);
    beamInputTransformMeta.setTransformPluginId(BeamConst.STRING_BEAM_INPUT_PLUGIN_ID);
    pipelineMeta.addTransform(beamInputTransformMeta);
    // Add 4 add constants transforms CA and FL, NY, Default
    // 
    String[] stateCodes = new String[] { "CA", "FL", "NY", "AR", "Default" };
    for (String stateCode : stateCodes) {
        ConstantMeta constant = new ConstantMeta();
        ConstantField cf = new ConstantField("Comment", "String", stateCode + " : some comment");
        constant.getFields().add(cf);
        TransformMeta constantMeta = new TransformMeta(stateCode, constant);
        pipelineMeta.addTransform(constantMeta);
    }
    // Add Switch / Case transform looking switching on stateCode field
    // Send rows to A (true) and B (false)
    // 
    SwitchCaseMeta switchCaseMeta = new SwitchCaseMeta();
    switchCaseMeta.setFieldName("stateCode");
    switchCaseMeta.setCaseValueType("String");
    // 
    for (int i = 0; i < stateCodes.length - 1; i++) {
        String stateCode = stateCodes[i];
        List<SwitchCaseTarget> caseTargets = switchCaseMeta.getCaseTargets();
        SwitchCaseTarget target = new SwitchCaseTarget();
        target.setCaseValue(stateCode);
        target.setCaseTargetTransformName(stateCode);
        caseTargets.add(target);
    }
    switchCaseMeta.setDefaultTargetTransformName(stateCodes[stateCodes.length - 1]);
    switchCaseMeta.searchInfoAndTargetTransforms(pipelineMeta.getTransforms());
    TransformMeta switchCaseTransformMeta = new TransformMeta("Switch/Case", switchCaseMeta);
    pipelineMeta.addTransform(switchCaseTransformMeta);
    pipelineMeta.addPipelineHop(new PipelineHopMeta(beamInputTransformMeta, switchCaseTransformMeta));
    for (String stateCode : stateCodes) {
        pipelineMeta.addPipelineHop(new PipelineHopMeta(switchCaseTransformMeta, pipelineMeta.findTransform(stateCode)));
    }
    // Add a dummy behind it all to flatten/merge the data again...
    // 
    DummyMeta dummyPipelineMeta = new DummyMeta();
    TransformMeta dummyTransformMeta = new TransformMeta("Flatten", dummyPipelineMeta);
    pipelineMeta.addTransform(dummyTransformMeta);
    for (String stateCode : stateCodes) {
        pipelineMeta.addPipelineHop(new PipelineHopMeta(pipelineMeta.findTransform(stateCode), dummyTransformMeta));
    }
    // Add the output transform
    // 
    BeamOutputMeta beamOutputMeta = new BeamOutputMeta();
    beamOutputMeta.setOutputLocation("/tmp/customers/output/");
    beamOutputMeta.setFileDefinitionName(null);
    beamOutputMeta.setFilePrefix("switch-case-test");
    beamOutputMeta.setFileSuffix(".csv");
    // Not yet supported
    beamOutputMeta.setWindowed(false);
    TransformMeta beamOutputTransformMeta = new TransformMeta(outputTransformName, beamOutputMeta);
    beamOutputTransformMeta.setTransformPluginId("BeamOutput");
    pipelineMeta.addTransform(beamOutputTransformMeta);
    pipelineMeta.addPipelineHop(new PipelineHopMeta(dummyTransformMeta, beamOutputTransformMeta));
    return pipelineMeta;
}
Also used : DummyMeta(org.apache.hop.pipeline.transforms.dummy.DummyMeta) FileDefinition(org.apache.hop.beam.metadata.FileDefinition) ConstantField(org.apache.hop.pipeline.transforms.constant.ConstantField) BeamOutputMeta(org.apache.hop.beam.transforms.io.BeamOutputMeta) PipelineHopMeta(org.apache.hop.pipeline.PipelineHopMeta) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) BeamInputMeta(org.apache.hop.beam.transforms.io.BeamInputMeta) TransformMeta(org.apache.hop.pipeline.transform.TransformMeta) SwitchCaseMeta(org.apache.hop.pipeline.transforms.switchcase.SwitchCaseMeta) SwitchCaseTarget(org.apache.hop.pipeline.transforms.switchcase.SwitchCaseTarget) ConstantMeta(org.apache.hop.pipeline.transforms.constant.ConstantMeta)

Aggregations

FileDefinition (org.apache.hop.beam.metadata.FileDefinition)2 BeamInputMeta (org.apache.hop.beam.transforms.io.BeamInputMeta)2 BeamOutputMeta (org.apache.hop.beam.transforms.io.BeamOutputMeta)2 PipelineHopMeta (org.apache.hop.pipeline.PipelineHopMeta)2 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)2 TransformMeta (org.apache.hop.pipeline.transform.TransformMeta)2 ConstantField (org.apache.hop.pipeline.transforms.constant.ConstantField)2 ConstantMeta (org.apache.hop.pipeline.transforms.constant.ConstantMeta)2 DummyMeta (org.apache.hop.pipeline.transforms.dummy.DummyMeta)2 ValueMetaAndData (org.apache.hop.core.row.ValueMetaAndData)1 FilterRowsMeta (org.apache.hop.pipeline.transforms.filterrows.FilterRowsMeta)1 SwitchCaseMeta (org.apache.hop.pipeline.transforms.switchcase.SwitchCaseMeta)1 SwitchCaseTarget (org.apache.hop.pipeline.transforms.switchcase.SwitchCaseTarget)1