Skip to content

Commit

Permalink
refactor!: Moved Redis client options from Main command to subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Apr 29, 2024
1 parent c1d974e commit fce39af
Show file tree
Hide file tree
Showing 88 changed files with 1,095 additions and 2,393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.JsonObjectMarshaller;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.redis.riot.core.AbstractExport;
import com.redis.riot.core.ExecutionException;
import com.redis.riot.file.resource.JsonResourceItemWriter;
import com.redis.riot.file.resource.JsonResourceItemWriterBuilder;
import com.redis.riot.file.resource.XmlResourceItemWriter;
import com.redis.riot.file.resource.XmlResourceItemWriterBuilder;
import com.redis.spring.batch.KeyValue;
import com.redis.riot.file.xml.XmlResourceItemWriter;
import com.redis.riot.file.xml.XmlResourceItemWriterBuilder;
import com.redis.spring.batch.RedisItemReader;
import com.redis.spring.batch.reader.MemKeyValue;

import io.lettuce.core.codec.StringCodec;

Expand All @@ -36,35 +36,7 @@ public class FileDumpExport extends AbstractExport {
private String lineSeparator = DEFAULT_LINE_SEPARATOR;
private FileDumpType type;

public void setFile(String file) {
this.file = file;
}

public void setFileOptions(FileOptions fileOptions) {
this.fileOptions = fileOptions;
}

public void setType(FileDumpType type) {
this.type = type;
}

public void setAppend(boolean append) {
this.append = append;
}

public void setRootName(String rootName) {
this.rootName = rootName;
}

public void setElementName(String elementName) {
this.elementName = elementName;
}

public void setLineSeparator(String lineSeparator) {
this.lineSeparator = lineSeparator;
}

private ItemWriter<KeyValue<String, Object>> writer() {
private ItemWriter<MemKeyValue<String, Object>> writer() {
WritableResource resource;
try {
resource = FileUtils.outputResource(file, fileOptions);
Expand All @@ -84,8 +56,8 @@ private FileDumpType dumpType(WritableResource resource) {
return type;
}

private JsonResourceItemWriter<KeyValue<String, Object>> jsonWriter(Resource resource) {
JsonResourceItemWriterBuilder<KeyValue<String, Object>> jsonWriterBuilder = new JsonResourceItemWriterBuilder<>();
private JsonFileItemWriter<MemKeyValue<String, Object>> jsonWriter(WritableResource resource) {
JsonFileItemWriterBuilder<MemKeyValue<String, Object>> jsonWriterBuilder = new JsonFileItemWriterBuilder<>();
jsonWriterBuilder.name("json-resource-item-writer");
jsonWriterBuilder.append(append);
jsonWriterBuilder.encoding(fileOptions.getEncoding());
Expand All @@ -96,8 +68,8 @@ private JsonResourceItemWriter<KeyValue<String, Object>> jsonWriter(Resource res
return jsonWriterBuilder.build();
}

private XmlResourceItemWriter<KeyValue<String, Object>> xmlWriter(Resource resource) {
XmlResourceItemWriterBuilder<KeyValue<String, Object>> xmlWriterBuilder = new XmlResourceItemWriterBuilder<>();
private XmlResourceItemWriter<MemKeyValue<String, Object>> xmlWriter(Resource resource) {
XmlResourceItemWriterBuilder<MemKeyValue<String, Object>> xmlWriterBuilder = new XmlResourceItemWriterBuilder<>();
xmlWriterBuilder.name("xml-resource-item-writer");
xmlWriterBuilder.append(append);
xmlWriterBuilder.encoding(fileOptions.getEncoding());
Expand All @@ -109,20 +81,49 @@ private XmlResourceItemWriter<KeyValue<String, Object>> xmlWriter(Resource resou
return xmlWriterBuilder.build();
}

private JsonObjectMarshaller<KeyValue<String, Object>> xmlMarshaller() {
private JsonObjectMarshaller<MemKeyValue<String, Object>> xmlMarshaller() {
XmlMapper mapper = FileUtils.xmlMapper();
mapper.setConfig(mapper.getSerializationConfig().withRootName(elementName));
JacksonJsonObjectMarshaller<KeyValue<String, Object>> marshaller = new JacksonJsonObjectMarshaller<>();
JacksonJsonObjectMarshaller<MemKeyValue<String, Object>> marshaller = new JacksonJsonObjectMarshaller<>();
marshaller.setObjectMapper(mapper);
return marshaller;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected Job job() {
RedisItemReader<String, String, KeyValue<String, Object>> reader = RedisItemReader.struct();
RedisItemReader<String, String, MemKeyValue<String, Object>> reader = RedisItemReader.struct();
configure(reader);
ItemProcessor<KeyValue<String, Object>, KeyValue<String, Object>> processor = processor(StringCodec.UTF8);
ItemProcessor processor = processor(StringCodec.UTF8);
return jobBuilder().start(step(getName(), reader, writer()).processor(processor).build()).build();
}

public void setFile(String file) {
this.file = file;
}

public void setFileOptions(FileOptions fileOptions) {
this.fileOptions = fileOptions;
}

public void setType(FileDumpType type) {
this.type = type;
}

public void setAppend(boolean append) {
this.append = append;
}

public void setRootName(String rootName) {
this.rootName = rootName;
}

public void setElementName(String elementName) {
this.elementName = elementName;
}

public void setLineSeparator(String lineSeparator) {
this.lineSeparator = lineSeparator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.redis.riot.core.AbstractImport;
import com.redis.spring.batch.KeyValue;
import com.redis.spring.batch.RedisItemWriter;
import com.redis.spring.batch.reader.MemKeyValue;

public class FileDumpImport extends AbstractImport {

Expand Down Expand Up @@ -59,11 +60,11 @@ protected Job job() {
return job.build();
}

private ItemReader<KeyValue<String, Object>> reader(Resource resource) {
private ItemReader<MemKeyValue<String, Object>> reader(Resource resource) {
if (type == FileDumpType.XML) {
return FileUtils.xmlReader(resource, KeyValue.class);
return FileUtils.xmlReader(resource, MemKeyValue.class);
}
return FileUtils.jsonReader(resource, KeyValue.class);
return FileUtils.jsonReader(resource, MemKeyValue.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
package com.redis.riot.file;

import com.redis.riot.file.resource.AbstractResourceItemWriter;
import java.nio.charset.StandardCharsets;

public class FileOptions {

public static final String DEFAULT_ENCODING = AbstractResourceItemWriter.DEFAULT_CHARSET;
public static final String DEFAULT_ENCODING = StandardCharsets.UTF_8.name();

private String encoding = DEFAULT_ENCODING;
private String encoding = DEFAULT_ENCODING;

private boolean gzipped;
private boolean gzipped;

private GoogleStorageOptions googleStorageOptions = new GoogleStorageOptions();
private GoogleStorageOptions googleStorageOptions = new GoogleStorageOptions();

private AmazonS3Options amazonS3Options = new AmazonS3Options();
private AmazonS3Options amazonS3Options = new AmazonS3Options();

public String getEncoding() {
return encoding;
}
public String getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}

public boolean isGzipped() {
return gzipped;
}
public boolean isGzipped() {
return gzipped;
}

public void setGzipped(boolean gzipped) {
this.gzipped = gzipped;
}
public void setGzipped(boolean gzipped) {
this.gzipped = gzipped;
}

public GoogleStorageOptions getGoogleStorageOptions() {
return googleStorageOptions;
}
public GoogleStorageOptions getGoogleStorageOptions() {
return googleStorageOptions;
}

public void setGoogleStorageOptions(GoogleStorageOptions googleStorageOptions) {
this.googleStorageOptions = googleStorageOptions;
}
public void setGoogleStorageOptions(GoogleStorageOptions googleStorageOptions) {
this.googleStorageOptions = googleStorageOptions;
}

public AmazonS3Options getAmazonS3Options() {
return amazonS3Options;
}
public AmazonS3Options getAmazonS3Options() {
return amazonS3Options;
}

public void setAmazonS3Options(AmazonS3Options amazonS3Options) {
this.amazonS3Options = amazonS3Options;
}
public void setAmazonS3Options(AmazonS3Options amazonS3Options) {
this.amazonS3Options = amazonS3Options;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.storage.StorageOptions;
import com.redis.riot.file.resource.FilenameInputStreamResource;
import com.redis.riot.file.resource.OutputStreamResource;
import com.redis.riot.file.resource.UncustomizedUrlResource;
import com.redis.riot.file.resource.XmlItemReader;
import com.redis.riot.file.resource.XmlItemReaderBuilder;
import com.redis.riot.file.resource.XmlObjectReader;
import com.redis.spring.batch.KeyValue;
import com.redis.riot.file.xml.XmlItemReader;
import com.redis.riot.file.xml.XmlItemReaderBuilder;
import com.redis.riot.file.xml.XmlObjectReader;
import com.redis.spring.batch.reader.MemKeyValue;

public abstract class FileUtils {

Expand Down Expand Up @@ -187,7 +184,7 @@ public static XmlMapper xmlMapper() {
private static void configureMapper(ObjectMapper mapper) {
mapper.configure(DeserializationFeature.USE_LONG_FOR_INTS, true);
SimpleModule module = new SimpleModule();
module.addDeserializer(KeyValue.class, new KeyValueDeserializer());
module.addDeserializer(MemKeyValue.class, new MemKeyValueDeserializer());
mapper.registerModule(module);
mapper.setSerializationInclusion(Include.NON_NULL);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.redis.riot.file.resource;
package com.redis.riot.file;

import java.io.InputStream;
import java.util.Objects;

import org.springframework.core.io.InputStreamResource;

import com.redis.riot.file.resource.FilenameInputStreamResource;
import com.redis.riot.file.FilenameInputStreamResource;

public class FilenameInputStreamResource extends InputStreamResource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import com.redis.lettucemod.timeseries.Sample;
import com.redis.spring.batch.KeyValue;
import com.redis.spring.batch.KeyValue.DataType;
import com.redis.spring.batch.reader.MemKeyValue;

import io.lettuce.core.ScoredValue;
import io.lettuce.core.StreamMessage;

public class KeyValueDeserializer extends StdDeserializer<KeyValue<String, Object>> {
public class MemKeyValueDeserializer extends StdDeserializer<MemKeyValue<String, Object>> {

private static final long serialVersionUID = 1L;

Expand All @@ -35,18 +36,18 @@ public class KeyValueDeserializer extends StdDeserializer<KeyValue<String, Objec
public static final String BODY = "body";
public static final String TIMESTAMP = "timestamp";

public KeyValueDeserializer() {
public MemKeyValueDeserializer() {
this(null);
}

public KeyValueDeserializer(Class<KeyValue<String, Object>> t) {
public MemKeyValueDeserializer(Class<MemKeyValue<String, Object>> t) {
super(t);
}

@Override
public KeyValue<String, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
public MemKeyValue<String, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);
KeyValue<String, Object> keyValue = new KeyValue<>();
MemKeyValue<String, Object> keyValue = new MemKeyValue<>();
JsonNode keyNode = node.get(KEY);
if (keyNode != null) {
keyValue.setKey(node.get(KEY).asText());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.riot.file.resource;
package com.redis.riot.file;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.riot.file.resource;
package com.redis.riot.file;

import org.springframework.core.io.UrlResource;

Expand Down
Loading

0 comments on commit fce39af

Please sign in to comment.