public final class TupleFactory extends Object
String input = "{ \"name\" : \"bob\" }";
Object parser = TupleFactory.getJsonParser();
Tuple tuple = TupleFactory.getTupleFromJson(input, parser);
Tuple tuple = TupleFactory.getNewTuple(fromJson);
// Assign a leaf value, i,e, the tuple contains a Json element
tuple.set("aValue");
// Assign an inner property value
tuple.get("user").get("name").set("bob");
// Assign array elements
tuple.append(1).append(2).append(3);
// Assign Set elements
tuple.add(1).add(2).add(3);
String toJson = TupleFactory.getJsonFromTuple(tuple);
Modifier and Type | Method and Description |
---|---|
static org.apache.avro.generic.GenericRecord |
getAvroRecord(org.apache.avro.Schema schema,
Tuple tuple)
Tuple to avro conversion method
|
static String |
getJsonFromTuple(Tuple tuple)
Get a valid Json representation from a tuple.
|
static String |
getJsonFromTuple(Tuple tuple,
Object mapper)
Get a valid Json representation from a tuple.
|
static Object |
getJsonObjectMapper()
Get a json object mapper.
|
static Object |
getJsonParser()
Get a json parser.
|
static Tuple |
getLeafTuple(Object o)
Return a new leaf tuple.
|
static Tuple |
getNewTuple()
Use this method to create a new empty tuples.
|
static Tuple |
getNewTuple(Object value)
Use this method to create a non empty root tuple.
|
static Object |
getObjectMapper(boolean legacy)
Deprecated.
use getJsonObjectMapper(). Will be removed later.
Only keep for compatibility with existing parsers
|
static Tuple |
getTupleFromCsv(String content,
String hashKey,
String delimiter,
boolean inferTypes,
boolean hasHeader,
boolean lowerCaseKeys)
Build a punch tuple from a CSV file content.
|
static Tuple |
getTupleFromCsv(String s,
String hashKey,
String delimiter,
List<String> columnNames,
boolean inferTypes,
boolean lowerCaseKeys)
Build a punch tuple from a CSV file content.
|
static Tuple |
getTupleFromJson(Object mapper,
String fromJson)
Deprecated.
use getTupleFromJson(String fromJson, Object mapper). Will be removed later.
Only keep for compatibility with existing parsers
|
static Tuple |
getTupleFromJson(String s)
Retrieve a tuple from a valid Json String.
|
static Tuple |
getTupleFromJson(String s,
Object jsonMapper) |
static Tuple |
getTupleFromList(List<Object> input) |
static Tuple |
getTupleFromMap(Map map)
Get a tuple from a map.
|
static Object |
toPunchLeafValue(Object v)
The punch tuple type can be filled with various values.
|
public static Tuple getLeafTuple(Object o)
o
- public static Tuple getTupleFromJson(String s)
In addition using a mapper you can control ascii escaping.
s
- the input stringPunchRuntimeException
public static Tuple getTupleFromJson(String s, Object jsonMapper)
s
- jsonMapper
- the json mapper you got previously using getJsonMapper();public static Tuple getTupleFromMap(Map map)
map
- the input mappublic static String getJsonFromTuple(Tuple tuple)
tuple
- the target tuplepublic static String getJsonFromTuple(Tuple tuple, Object mapper)
tuple
- the target tuplepublic static Tuple getTupleFromList(List<Object> input)
input
- a list of json compatible objectspublic static Tuple getNewTuple()
Tuple.exists()
and true to Tuple.isEmpty()
.
An empty tuple is also a root tuple. I.e. it returns true to Tuple.isRoot()
.
public static Tuple getNewTuple(Object value)
Tuple.isRoot()
.value
- the value to assignpublic static org.apache.avro.generic.GenericRecord getAvroRecord(org.apache.avro.Schema schema, Tuple tuple)
schema
- the avro schematuple
- the input tuplepublic static Tuple getTupleFromCsv(String s, String hashKey, String delimiter, List<String> columnNames, boolean inferTypes, boolean lowerCaseKeys)
s
- the input CSV string. This is supposed to be one or several lines separated
by the carriage return character.hashKey
- the name of the item to be used as hash key. Use null if you simply need an array
with all elements.delimiter
- the delimiter to be usedcolumnNames
- the column namesinferTypes
- true to infer the types.lowerCaseKeys
- true to make all keys lower cases.public static Tuple getTupleFromCsv(String content, String hashKey, String delimiter, boolean inferTypes, boolean hasHeader, boolean lowerCaseKeys)
content
- the input CSV string. This is supposed to be one or several lines separated by the
carriage return character.hashKey
- the name of the item to be used as hash key. Use null if you simply need an array
with all elements.delimiter
- the delimiter to be usedinferTypes
- true to infer the inner types, this takes twice as CPUhasHeader
- true if the content includes a first header line with column nameslowerCaseKeys
- true to make all keys lower cases.public static Object toPunchLeafValue(Object v)
Typically ints or shorts become long. etc..
If you pass in a composit type, it will be returned as is.
v
- an objectpublic static Object getJsonParser()
This is only required if you extensively use the getTupleFromJson(java.lang.String) method.
public static Object getJsonObjectMapper()
This is only required if you extensively use the getJsonFromTuple(Tuple, Object) method.
public static Object getObjectMapper(boolean legacy)
legacy
- no used. only for compatibilitypublic static Tuple getTupleFromJson(Object mapper, String fromJson)
mapper
- fromJson
- Copyright © 2023. All rights reserved.