List of usage examples for org.springframework.batch.item.file.transform FieldSet getValues
String[] getValues();
From source file:de.langmi.spring.batch.examples.writers.jdbc.generic.FieldSetItemPreparedStatementSetter.java
/** {@inheritDoc} */ @Override/*from www. j av a 2 s .c o m*/ public void setValues(FieldSet item, PreparedStatement ps) throws SQLException { for (int i = 0; i < item.getValues().length; i++) { // PreparedStatements start with 1 ps.setObject(i + 1, item.getValues()[i]); } }
From source file:io.spring.batch.SimilaritiesFieldSetMapper.java
@Override public Object mapFieldSet(FieldSet fieldSet) throws BindException { int tagId = fieldSet.readInt(0); String[] values = fieldSet.getValues(); Map<Integer, Double> similarites = new HashMap<Integer, Double>(); for (int i = 1; i < values.length; i++) { String[] curValues = values[i].split(":"); similarites.put(Integer.valueOf(curValues[0]), Double.valueOf(curValues[1])); }/* www. j a va 2 s . c o m*/ return new Similarities(tagId, similarites); }
From source file:de.langmi.spring.batch.examples.readers.file.fieldcount.FieldCountItemReader.java
/** {@inheritDoc} */ @Override//from w ww. ja va 2 s. c o m public List<String> read() throws Exception { List<String> fields = new ArrayList<String>(); FieldSet field = null; // read until end of file while ((field = delegate.read()) != null) { String[] fieldSetValues = field.getValues(); for (int i = 0; i < fieldSetValues.length; i++) { fields.add(field.getValues()[i]); } // field count reached ? if (fields.size() != count) { continue; } else { return fields; } } // reader returned nothing return null; }
From source file:fr.acxio.tools.agia.item.database.AbstractFieldSetSqlParameterSourceProvider.java
protected MapSqlParameterSource mapFieldSet(MapSqlParameterSource sMapSqlParameterSource, FieldSet sFieldSet, int sRecIdx) { if (sFieldSet != null) { boolean aHasNames = sFieldSet.hasNames(); int aFieldCount = sFieldSet.getFieldCount(); String[] aNames = aHasNames ? sFieldSet.getNames() : null; String[] aValues = sFieldSet.getValues(); for (int i = 0; i < aFieldCount; i++) { sMapSqlParameterSource.addValue(String.format(fieldsetNameFormat, sRecIdx, (aHasNames && (aNames[i] != null) && !aNames[i].isEmpty()) ? aNames[i] : String.format(unnamedColumnFormat, i)), aValues[i]);// w w w . j av a2 s .co m } } return sMapSqlParameterSource; }
From source file:de.langmi.spring.batch.examples.readers.file.csv.CsvFlatFileItemReaderTest.java
/** * Test should read succesfully./*from w w w . j ava2s . c om*/ * * @throws Exception */ @Test public void testSuccessfulReading() throws Exception { // init linetokenizer DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames(new String[] { "id", "value" }); // init linemapper DefaultLineMapper<FieldSet> lineMapper = new DefaultLineMapper<FieldSet>(); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(new PassThroughFieldSetMapper()); // init reader reader.setLineMapper(lineMapper); reader.setResource(new FileSystemResource(INPUT_FILE)); // open, provide "mock" ExecutionContext reader.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext()); // read try { int count = 0; FieldSet line; while ((line = reader.read()) != null) { // really test for the fieldSet names and values assertEquals("id", line.getNames()[0]); assertEquals(String.valueOf(count), line.getValues()[0]); assertEquals("value", line.getNames()[1]); // csv contains entry like '0,foo0' assertEquals("foo" + String.valueOf(count), line.getValues()[1]); count++; } assertEquals(EXPECTED_COUNT, count); } catch (Exception e) { throw e; } finally { reader.close(); } }
From source file:fr.acxio.tools.agia.item.MultiLineItemReader.java
@Override public synchronized T read() { List<FieldSet> aTmpResult = new ArrayList<FieldSet>(); boolean aConditionResult = false; FieldSet line = readNextFieldSet(); while (!aConditionResult && (line != null)) { aTmpResult.add(line);//from ww w . j a v a 2s . c om if (nextItem != null) { updateContext(currentVariableName, (line.hasNames()) ? line.getProperties() : line.getValues(), getEvaluationContext()); updateContext(nextVariableName, (nextItem.hasNames()) ? nextItem.getProperties() : nextItem.getValues(), getEvaluationContext()); aConditionResult = getExpressionResolver().evaluate(newRecordCondition, getEvaluationContext(), Boolean.class); } if (!aConditionResult) { line = readNextFieldSet(); } } return (aTmpResult.isEmpty() ? null : mapFieldSets(aTmpResult)); }
From source file:fr.acxio.tools.agia.transform.ListFieldSetToMapProcessor.java
protected Map<String, Object> mapFieldSet(FieldSet sFieldSet, int sRecIdx) { Map<String, Object> aResult = new HashMap<String, Object>(); if (sFieldSet != null) { boolean aHasNames = sFieldSet.hasNames(); int aFieldCount = sFieldSet.getFieldCount(); String[] aNames = aHasNames ? sFieldSet.getNames() : null; String[] aValues = sFieldSet.getValues(); for (int i = 0; i < aFieldCount; i++) { aResult.put(String.format(fieldsetNameFormat, sRecIdx, (aHasNames && (aNames[i] != null) && !aNames[i].isEmpty()) ? aNames[i] : String.format(unnamedColumnFormat, i)), aValues[i]);//w w w.j a v a2 s.c om } } return aResult; }
From source file:org.springframework.batch.admin.sample.LeadRandomizer.java
@Test public void testLeads() throws Exception { FlatFileItemReader<FieldSet> reader = new FlatFileItemReader<FieldSet>(); reader.setResource(new ClassPathResource("/data/test.txt")); DefaultLineMapper<FieldSet> lineMapper = new DefaultLineMapper<FieldSet>(); lineMapper.setLineTokenizer(new DelimitedLineTokenizer()); lineMapper.setFieldSetMapper(new PassThroughFieldSetMapper()); reader.setLinesToSkip(1);//from w w w .java2 s . co m final List<String> headers = new ArrayList<String>(); reader.setSkippedLinesCallback(new LineCallbackHandler() { public void handleLine(String line) { headers.add(line); } }); reader.setLineMapper(lineMapper); reader.open(new ExecutionContext()); List<FieldSet> list = new ArrayList<FieldSet>(); FieldSet item = reader.read(); while (item != null) { list.add(item); item = reader.read(); } assertEquals(7, list.size()); FlatFileItemWriter<FieldSet> writer = new FlatFileItemWriter<FieldSet>(); FileSystemResource resource = new FileSystemResource("target/output/output.txt"); FileUtils.deleteQuietly(resource.getFile()); writer.setResource(resource); writer.setHeaderCallback(new FlatFileHeaderCallback() { public void writeHeader(Writer writer) throws IOException { for (String header : headers) { writer.write(header); } } }); writer.setLineAggregator(new DelimitedLineAggregator<FieldSet>()); writer.open(new ExecutionContext()); String[] names = getFields(list, 1); String[] country = getFields(list, 2); String[] products = getFields(list, 3); double[] amounts = getMinMax(list, 4); NumberFormat formatter = new DecimalFormat("#.##"); int count = 20; for (int i = 0; i < 100; i++) { List<FieldSet> items = new ArrayList<FieldSet>(); for (FieldSet fieldSet : list) { String[] values = fieldSet.getValues(); values[0] = "" + (count++); values[1] = choose(names); values[2] = choose(country); values[3] = choose(products); values[4] = formatter.format(random(amounts)); items.add(new DefaultFieldSet(values)); } writer.write(items); } writer.close(); }