/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Stores the array of items in the configuration with the given keyName. * * @param <K> the class of the item * @param conf the configuration to use * @param items the objects to be stored * @param keyName the name of the key to use * @throws IndexOutOfBoundsException if the items array is empty * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> void storeArray(Configuration conf, K[] items, String keyName) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, GenericsUtil.getClass(items[0])); try { StringBuilder builder = new StringBuilder(); for (K item : items) { builder.append(stringifier.toString(item)).append(SEPARATOR); } conf.set(keyName, builder.toString()); } finally { stringifier.close(); } }
/** * Stores the item in the configuration with the given keyName. * * @param <K> the class of the item * @param conf the configuration to store * @param item the object to be stored * @param keyName the name of the key to use * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> void store(Configuration conf, K item, String keyName) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, GenericsUtil.getClass(item)); conf.set(keyName, stringifier.toString(item)); stringifier.close(); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
public void testGetClass() { //test with Integer Integer x = new Integer(42); Class<Integer> c = GenericsUtil.getClass(x); assertEquals(Integer.class, c); //test with GenericClass<Integer> GenericClass<Integer> testSubject = new GenericClass<Integer>(); Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject); assertEquals(GenericClass.class, c2); }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<Map<String,String>>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<Map<String,String>>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
@Override public String toString() { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<>(conf, GenericsUtil.getClass(params)); try { return mapStringifier.toString(params); } catch (IOException e) { log.info("Encountered IOException while deserializing returning empty string", e); return ""; } }
@Override public String toString() { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<Map<String,String>>(conf, GenericsUtil.getClass(params)); try { return mapStringifier.toString(params); } catch (IOException e) { log.info("Encountered IOException while deserializing returning empty string", e); return ""; } }
@Override protected void setup(Context context) throws IOException, InterruptedException { super.setup(context); Configuration conf = context.getConfiguration(); DefaultStringifier<Map<String,Integer>> mapStringifier = new DefaultStringifier<Map<String,Integer>>( conf, GenericsUtil.getClass(dictionary)); dictionary = mapStringifier.fromString(conf.get("dictionary")); splitter = Pattern.compile("<sep>"); writer = new VectorWritable(); } }
/** * Stores the item in the configuration with the given keyName. * * @param <K> the class of the item * @param conf the configuration to store * @param item the object to be stored * @param keyName the name of the key to use * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> void store(Configuration conf, K item, String keyName) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, GenericsUtil.getClass(item)); conf.set(keyName, stringifier.toString(item)); stringifier.close(); }
public void testGetClass() { //test with Integer Integer x = new Integer(42); Class<Integer> c = GenericsUtil.getClass(x); assertEquals("Correct generic type is acquired from object", Integer.class, c); //test with GenericClass<Integer> GenericClass<Integer> testSubject = new GenericClass<Integer>(); Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject); assertEquals("Inner generics are acquired from object.", GenericClass.class, c2); }
public void testGetClass() { //test with Integer Integer x = new Integer(42); Class<Integer> c = GenericsUtil.getClass(x); assertEquals("Correct generic type is acquired from object", Integer.class, c); //test with GenericClass<Integer> GenericClass<Integer> testSubject = new GenericClass<Integer>(); Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject); assertEquals("Inner generics are acquired from object.", GenericClass.class, c2); }
private <K> K serDeser(K conf) throws Exception { SerializationFactory factory = new SerializationFactory(CONF); Serializer<K> serializer = factory.getSerializer(GenericsUtil.getClass(conf)); Deserializer<K> deserializer = factory.getDeserializer(GenericsUtil.getClass(conf)); DataOutputBuffer out = new DataOutputBuffer(); serializer.open(out); serializer.serialize(conf); serializer.close(); DataInputBuffer in = new DataInputBuffer(); in.reset(out.getData(), out.getLength()); deserializer.open(in); K after = deserializer.deserialize(null); deserializer.close(); return after; }