@Override public RecordReader<Text, Text> getRecordReader(InputSplit split, JobConf job, Reporter reporter) throws IOException { if (schemas == null) { loadSchemas(job); } FileSplit fs = (FileSplit) split; Schema schema = null; if (schemas.size() == 1) { schema = schemas.get(0); } else { // Need to figure out which schema we're loading String current = fs.getPath().toString(); int index = -1; int bestMatchLength = -1; for (int i = 0; i < inputPaths.length; i++) { int match = Strings.commonPrefix(current, inputPaths[i]).length(); if (match > bestMatchLength) { bestMatchLength = match; index = i; } } schema = schemas.get(index); } return new AvroAsJSONRecordReader(schema, job, fs); }
@Nonnull public static String commonPrefix(@Nonnull final String... strs) { Preconditions.checkArgument(strs.length > 0, "Must have at least 1 string %s", strs); String common = strs[0]; for (int i = 1; i < strs.length; i++) { common = com.google.common.base.Strings.commonPrefix(common, strs[i]); } return common; }
@Test public void testConstructSelfUri() { String id = UUID.randomUUID().toString(); URI self = InstanceResource.constructSelfUri(INSTANCE_URI_INFO, id); assertEquals(id, Strings.commonSuffix(id, self.getPath())); assertEquals( INSTANCE_URI_INFO.getAbsolutePath().toString(), Strings.commonPrefix(INSTANCE_URI_INFO.getAbsolutePath().toString(), self.toString())); }
/** * Returns the longest string {@code prefix} such that {@code a.toString().startsWith(prefix) && * b.toString().startsWith(prefix)}, taking care not to split surrogate pairs. If {@code a} and * {@code b} have no common prefix, returns the empty string. * * @since 11.0 * @see Strings#commonPrefix(CharSequence, CharSequence) */ @Beta public static String commonPrefix(final CharSequence a, final CharSequence b) { return Strings.commonPrefix(a, b); }