Search in sources :

Example 1 with Iterable

use of java.lang.Iterable in project atlasdb by palantir.

the class SchemaApiTestV2Table method getColumn2.

/**
 * Returns a mapping from the specified row keys to their value at column Column2.
 * As the Column2 values are all loaded in memory, do not use for large amounts of data.
 * If the column does not exist for a key, the entry will be omitted from the map.
 */
public Map<String, StringValue> getColumn2(Iterable<String> rowKeys) {
    ColumnSelection colSelection = ColumnSelection.create(ImmutableList.of(PtBytes.toCachedBytes("d")));
    List<SchemaApiTestTable.SchemaApiTestRow> rows = Lists.newArrayList(rowKeys).stream().map(SchemaApiTestTable.SchemaApiTestRow::of).collect(Collectors.toList());
    SortedMap<byte[], RowResult<byte[]>> results = t.getRows(tableRef, Persistables.persistAll(rows), colSelection);
    return results.values().stream().map(entry -> SchemaApiTestTable.SchemaApiTestRowResult.of(entry)).collect(Collectors.toMap(entry -> entry.getRowName().getComponent1(), SchemaApiTestTable.SchemaApiTestRowResult::getColumn2));
}
Also used : StringValue(com.palantir.atlasdb.table.description.test.StringValue) Persistables(com.palantir.common.persist.Persistables) Function(java.util.function.Function) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Iterable(java.lang.Iterable) LinkedHashMap(java.util.LinkedHashMap) Generated(javax.annotation.Generated) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ColumnValues(com.palantir.atlasdb.table.generation.ColumnValues) Long(java.lang.Long) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) String(java.lang.String) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) Namespace(com.palantir.atlasdb.keyvalue.api.Namespace) SuppressWarnings(java.lang.SuppressWarnings) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Objects(java.util.Objects) List(java.util.List) Transaction(com.palantir.atlasdb.transaction.api.Transaction) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SortedMap(java.util.SortedMap) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult)

Example 2 with Iterable

use of java.lang.Iterable in project atlasdb by palantir.

the class SchemaApiTestV2Table method getColumn1.

/**
 * Returns a mapping from the specified row keys to their value at column Column1.
 * As the Column1 values are all loaded in memory, do not use for large amounts of data.
 * If the column does not exist for a key, the entry will be omitted from the map.
 */
public Map<String, Long> getColumn1(Iterable<String> rowKeys) {
    ColumnSelection colSelection = ColumnSelection.create(ImmutableList.of(PtBytes.toCachedBytes("c")));
    List<SchemaApiTestTable.SchemaApiTestRow> rows = Lists.newArrayList(rowKeys).stream().map(SchemaApiTestTable.SchemaApiTestRow::of).collect(Collectors.toList());
    SortedMap<byte[], RowResult<byte[]>> results = t.getRows(tableRef, Persistables.persistAll(rows), colSelection);
    return results.values().stream().map(entry -> SchemaApiTestTable.SchemaApiTestRowResult.of(entry)).collect(Collectors.toMap(entry -> entry.getRowName().getComponent1(), SchemaApiTestTable.SchemaApiTestRowResult::getColumn1));
}
Also used : StringValue(com.palantir.atlasdb.table.description.test.StringValue) Persistables(com.palantir.common.persist.Persistables) Function(java.util.function.Function) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Iterable(java.lang.Iterable) LinkedHashMap(java.util.LinkedHashMap) Generated(javax.annotation.Generated) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ColumnValues(com.palantir.atlasdb.table.generation.ColumnValues) Long(java.lang.Long) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) String(java.lang.String) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) Namespace(com.palantir.atlasdb.keyvalue.api.Namespace) SuppressWarnings(java.lang.SuppressWarnings) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Objects(java.util.Objects) List(java.util.List) Transaction(com.palantir.atlasdb.transaction.api.Transaction) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SortedMap(java.util.SortedMap) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult)

Example 3 with Iterable

use of java.lang.Iterable in project selenium_java by sergueik.

the class Field method toJson.

/**
 * Converts the given value to a JSON object.
 *
 * @param name Field name
 * @param value New field value
 * @param editmeta Edit metadata JSON object
 *
 * @return a JSON-encoded field value
 *
 * @throws JiraException when a value is bad or field has invalid metadata
 * @throws UnsupportedOperationException when a field type isn't supported
 */
public static Object toJson(String name, Object value, JSONObject editmeta) throws JiraException, UnsupportedOperationException {
    Meta m = getFieldMetadata(name, editmeta);
    if (m.type == null)
        throw new JiraException("Field '" + name + "' is missing metadata type");
    if (m.type.equals("array")) {
        if (value == null)
            value = new ArrayList();
        else if (!(value instanceof Iterable))
            throw new JiraException("Field '" + name + "' expects an Iterable value");
        return toArray((Iterable) value, m.items, m.custom);
    } else if (m.type.equals("date")) {
        if (value == null)
            return JSONNull.getInstance();
        Date d = toDate(value);
        if (d == null)
            throw new JiraException("Field '" + name + "' expects a date value or format is invalid");
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(d);
    } else if (m.type.equals("datetime")) {
        if (value == null)
            return JSONNull.getInstance();
        else if (!(value instanceof Timestamp))
            throw new JiraException("Field '" + name + "' expects a Timestamp value");
        SimpleDateFormat df = new SimpleDateFormat(DATETIME_FORMAT);
        return df.format(value);
    } else if (m.type.equals("issuetype") || m.type.equals("priority") || m.type.equals("user") || m.type.equals("resolution")) {
        JSONObject json = new JSONObject();
        if (value == null)
            return JSONNull.getInstance();
        else if (value instanceof ValueTuple) {
            ValueTuple tuple = (ValueTuple) value;
            json.put(tuple.type, tuple.value.toString());
        } else
            json.put(ValueType.NAME.toString(), value.toString());
        return json.toString();
    } else if (m.type.equals("project") || m.type.equals("issuelink")) {
        JSONObject json = new JSONObject();
        if (value == null)
            return JSONNull.getInstance();
        else if (value instanceof ValueTuple) {
            ValueTuple tuple = (ValueTuple) value;
            json.put(tuple.type, tuple.value.toString());
        } else
            json.put(ValueType.KEY.toString(), value.toString());
        return json.toString();
    } else if (m.type.equals("string") || (m.type.equals("securitylevel"))) {
        if (value == null)
            return "";
        else if (value instanceof List)
            return toJsonMap((List) value);
        else if (value instanceof ValueTuple) {
            JSONObject json = new JSONObject();
            ValueTuple tuple = (ValueTuple) value;
            json.put(tuple.type, tuple.value.toString());
            return json.toString();
        }
        return value.toString();
    } else if (m.type.equals("timetracking")) {
        if (value == null)
            return JSONNull.getInstance();
        else if (value instanceof TimeTracking)
            return ((TimeTracking) value).toJsonObject();
    } else if (m.type.equals("number")) {
        if (!(value instanceof java.lang.Integer) && !(value instanceof java.lang.Double) && !(value instanceof java.lang.Float) && !(value instanceof java.lang.Long)) {
            throw new JiraException("Field '" + name + "' expects a Numeric value");
        }
        return value;
    }
    throw new UnsupportedOperationException(m.type + " is not a supported field type");
}
Also used : Iterable(java.lang.Iterable) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) Date(java.util.Date) JSONObject(net.sf.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) UnsupportedOperationException(java.lang.UnsupportedOperationException)

Example 4 with Iterable

use of java.lang.Iterable in project learning-spark by databricks.

the class WordCount method main.

public static void main(String[] args) throws Exception {
    String master = args[0];
    JavaSparkContext sc = new JavaSparkContext(master, "wordcount", System.getenv("SPARK_HOME"), System.getenv("JARS"));
    JavaRDD<String> rdd = sc.textFile(args[1]);
    JavaPairRDD<String, Integer> counts = rdd.flatMap(new FlatMapFunction<String, String>() {

        public Iterable<String> call(String x) {
            return Arrays.asList(x.split(" "));
        }
    }).mapToPair(new PairFunction<String, String, Integer>() {

        public Tuple2<String, Integer> call(String x) {
            return new Tuple2(x, 1);
        }
    }).reduceByKey(new Function2<Integer, Integer, Integer>() {

        public Integer call(Integer x, Integer y) {
            return x + y;
        }
    });
    counts.saveAsTextFile(args[2]);
}
Also used : Iterable(java.lang.Iterable) Tuple2(scala.Tuple2) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) PairFunction(org.apache.spark.api.java.function.PairFunction)

Example 5 with Iterable

use of java.lang.Iterable in project learning-spark by databricks.

the class WordCount method main.

public static void main(String[] args) throws Exception {
    String inputFile = args[0];
    String outputFile = args[1];
    // Create a Java Spark Context.
    SparkConf conf = new SparkConf().setAppName("wordCount");
    JavaSparkContext sc = new JavaSparkContext(conf);
    // Load our input data.
    JavaRDD<String> input = sc.textFile(inputFile);
    // Split up into words.
    JavaRDD<String> words = input.flatMap(new FlatMapFunction<String, String>() {

        public Iterable<String> call(String x) {
            return Arrays.asList(x.split(" "));
        }
    });
    // Transform into word and count.
    JavaPairRDD<String, Integer> counts = words.mapToPair(new PairFunction<String, String, Integer>() {

        public Tuple2<String, Integer> call(String x) {
            return new Tuple2(x, 1);
        }
    }).reduceByKey(new Function2<Integer, Integer, Integer>() {

        public Integer call(Integer x, Integer y) {
            return x + y;
        }
    });
    // Save the word count back out to a text file, causing evaluation.
    counts.saveAsTextFile(outputFile);
}
Also used : Iterable(java.lang.Iterable) Tuple2(scala.Tuple2) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) PairFunction(org.apache.spark.api.java.function.PairFunction) SparkConf(org.apache.spark.SparkConf)

Aggregations

Iterable (java.lang.Iterable)5 List (java.util.List)3 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 PtBytes (com.palantir.atlasdb.encoding.PtBytes)2 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 ColumnSelection (com.palantir.atlasdb.keyvalue.api.ColumnSelection)2 Namespace (com.palantir.atlasdb.keyvalue.api.Namespace)2 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)2 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)2 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)2 StringValue (com.palantir.atlasdb.table.description.test.StringValue)2 ColumnValues (com.palantir.atlasdb.table.generation.ColumnValues)2 Transaction (com.palantir.atlasdb.transaction.api.Transaction)2 BatchingVisitableView (com.palantir.common.base.BatchingVisitableView)2 Persistables (com.palantir.common.persist.Persistables)2