Skip to content

Commit

Permalink
Merge pull request apache#1620 from metamx/longFriendlyQTL
Browse files Browse the repository at this point in the history
Allow long values in the key or value fields for URIExtractionNamespace
  • Loading branch information
xvrl committed Sep 2, 2015
2 parents ce3506b + b24a88b commit 82f9ecf
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ private DelegateParser(
public Map<String, String> parse(String input)
{
final Map<String, Object> inner = delegate.parse(input);
final String k = (String) Preconditions.checkNotNull(
final String k = Preconditions.checkNotNull(
inner.get(key),
"Key column [%s] missing data in line [%s]",
key,
input
);
final String val = (String) Preconditions.checkNotNull(
).toString(); // Just in case is long
final String val = Preconditions.checkNotNull(
inner.get(value),
"Value column [%s] missing data in line [%s]",
value,
input
);
).toString();
return ImmutableMap.<String, String>of(k, val);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,98 @@ public void testSimpleToString() throws IOException
public void testExplicitJson() throws IOException
{
final ObjectMapper mapper = registerTypes(new DefaultObjectMapper());
URIExtractionNamespace namespace = mapper.readValue("{\"type\":\"uri\", \"uri\":\"file:/foo\", \"namespaceParseSpec\":{\"format\":\"simpleJson\"}, \"pollPeriod\":\"PT5M\", \"versionRegex\":\"a.b.c\", \"namespace\":\"testNamespace\"}", URIExtractionNamespace.class);
URIExtractionNamespace namespace = mapper.readValue(
"{\"type\":\"uri\", \"uri\":\"file:/foo\", \"namespaceParseSpec\":{\"format\":\"simpleJson\"}, \"pollPeriod\":\"PT5M\", \"versionRegex\":\"a.b.c\", \"namespace\":\"testNamespace\"}",
URIExtractionNamespace.class
);

Assert.assertEquals(URIExtractionNamespace.ObjectMapperFlatDataParser.class.getCanonicalName(), namespace.getNamespaceParseSpec().getClass().getCanonicalName());
Assert.assertEquals(
URIExtractionNamespace.ObjectMapperFlatDataParser.class.getCanonicalName(),
namespace.getNamespaceParseSpec().getClass().getCanonicalName()
);
Assert.assertEquals("file:/foo", namespace.getUri().toString());
Assert.assertEquals("testNamespace", namespace.getNamespace());
Assert.assertEquals("a.b.c", namespace.getVersionRegex());
Assert.assertEquals(5L * 60_000L, namespace.getPollMs());
}

@Test
public void testFlatDataNumeric()
{
final String keyField = "keyField";
final String valueField = "valueField";
final int n = 341879;
final String nString = String.format("%d", n);
URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
new ObjectMapper(),
keyField,
valueField
);
Assert.assertEquals(
"num string value",
ImmutableMap.of("B", nString),
parser.getParser()
.parse(
String.format(
"{\"%s\":\"B\", \"%s\":\"%d\", \"FOO\":\"BAR\"}",
keyField,
valueField,
n
)
)
);
Assert.assertEquals(
"num string key",
ImmutableMap.of(nString, "C"),
parser.getParser()
.parse(
String.format(
"{\"%s\":\"%d\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
keyField,
n,
valueField
)
)
);
Assert.assertEquals(
"num value",
ImmutableMap.of("B", nString),
parser.getParser()
.parse(
String.format(
"{\"%s\":\"B\", \"%s\":%d, \"FOO\":\"BAR\"}",
keyField,
valueField,
n
)
)
);
Assert.assertEquals(
"num key",
ImmutableMap.of(nString, "C"),
parser.getParser()
.parse(
String.format(
"{\"%s\":%d, \"%s\":\"C\", \"FOO\":\"BAR\"}",
keyField,
n,
valueField
)
)
);
}

@Test
public void testSimpleJsonNumeric()
{
final URIExtractionNamespace.ObjectMapperFlatDataParser parser = new URIExtractionNamespace.ObjectMapperFlatDataParser(
registerTypes(new DefaultObjectMapper())
);
final int n = 341879;
final String nString = String.format("%d", n);
Assert.assertEquals(
ImmutableMap.of("key", nString),
parser.getParser().parse(String.format("{\"key\":%d}", n))
);
}
}

0 comments on commit 82f9ecf

Please sign in to comment.