public interface Tuple extends Comparable<Tuple>
Tuples can hold primitive values, referred to as a leaf, of type double, long, String or boolean. Tuples can also contains maps, array and set, of primitive values or sub-tuples. A tuple can thus be arbitrary nested.
A tuple allows you to work on json document using a compact and intuitive notation. Here is an example:
Tuple tuple; tuple:[name] = "bob"; tuple:[age] = 23;
Converting this tuple to a String would produce the following output
{"age":23,"name":"bob"}
Tuples can be nested, contain arrays, or primitive types, just as you expect. For example:
Tuple tuple; tuple:[user][name] = "bob"; tuple:[user][age] = 23; tuple:[tags].append("aTag").append("anotherTag");
As a string, this tuple is:
{ "tags" : ["aTag", "anotherTag"], "user" : {"age":23 , "name":"bob" } }
Check out these examples:
Modifier and Type | Method and Description |
---|---|
Tuple |
add(Object element)
Add a tuple element into a set.
|
Tuple |
append(Object element)
Add a abject element at the end of an array.
|
List<Tuple> |
asArray()
Returns an ordered list of elements if the tuple is an array or a set.
|
boolean |
asBoolean()
Return the leaf value as a boolean.
|
byte[] |
asBytes()
Return the leaf value as a byte array.
|
Collection<Tuple> |
asCollection()
Returns a collection of elements.
|
double |
asDouble()
Return the leaf value as a double.
|
double[] |
asDoubleArray()
Easily get an array of double.
|
float |
asFloat()
Return the leaf value as a float.
|
GeoFeatureTuple |
asGeoShape()
Convert the Tuple in GeoFeatureTuple if the Tuple have the attributs : type and coordinates in content
|
int |
asInt()
Return the leaf value as an integer.
|
long |
asLong()
Return the leaf value as a long.
|
long[] |
asLongArray()
Easily get an array of long.
|
Set<Tuple> |
asSet()
Returns a set of elements.
|
String |
asString()
Return the leaf value as a String.
|
String |
asString(String fmt,
Object... values)
Return the leaf value as a String.
|
String[] |
asStringArray()
Easily get an array of long.
|
void |
attach(String key,
Object value)
Attach some opaque data to this tuple.
|
String |
concat(String str)
Concatenates the specified string to the end of this string.
|
boolean |
contains(Object element)
This method behaves differently depending on the tuple type.
|
boolean |
containsOneOf(String[] strs)
Check if this tuple contains a sub-string.
|
boolean |
containsOneOf(Tuple resource)
Check if this tuple contains one of several sub-string.
|
Tuple |
cutPrefix(String s)
Remove a matching prefix
|
Tuple |
cutSuffix(String s)
Remove a matching prefix
|
String |
decodeBase64()
Decode a string Tuple already in base64
|
String |
dump()
Return a String representation of this tuple.
|
Tuple |
duplicate()
Clone a tuple.
|
Tuple[] |
elements()
Returns an array of elements.
|
void |
empty()
Make a tuple empty.
|
String |
encodeBase64()
Encode a string Tuple into a base64 String
|
boolean |
endsWith(String s)
Return true if the String value of this tuple ends with a given suffix.
|
boolean |
equalsOneOf(Tuple resource)
Check if this tuple contains a sub-string.
|
boolean |
exists() |
Tuple |
fromEscapedJson()
This method assumes your tuple holds a string leaf value.
|
Tuple |
get(String key)
Return a sub-tuple by key.
|
long |
get(String key,
long defaultValue)
Retrieve a field from a tuple, and if not there return a default.
|
String |
get(String key,
String defaultValue)
Retrieve a field from a tuple, and if not there return a default.
|
Tuple |
get(Tuple key)
Return a sub-tuple by key.
|
Object |
getAttachment(String key)
Retrieve an attachment by key.
|
Tuple |
getByKey(String key)
Return a sub-tuple by key.
|
Tuple |
getDeep(String key)
Return a sub-tuple by key recursively.
|
Tuple |
getDeepEs(String key)
Return a sub-tuple by key recursively.
|
Tuple |
getElement()
This method returns a new empty array element.
|
Tuple |
getElement(int index)
Retrieve the ith item from this tuple.
|
List<Entry> |
getEntries()
Returns the list of inner property key/values.
|
Iterator<Object> |
getIterator()
This method only works on array tuples.
|
List<String> |
getKeys()
Returns the list of inner property keys.
|
Tuple |
getSingleChild()
Return the single child of this tuple.
|
String |
getType()
return a String representation of the type of the value contained in a tuple.
|
Collection<Tuple> |
getValues()
Returns the collection of inner property values.
|
boolean |
hasKey(String key) |
int |
indexOf(char ch)
String operation only.
|
int |
indexOf(char ch,
int fromIndex)
String operation only.
|
int |
indexOf(String str)
String operation only.
|
int |
indexOf(String str,
int fromIndex)
String operation only.
|
boolean |
isArray()
Return true if this tuple contains an array value
|
boolean |
isBooleanLeaf()
Check if a tuple contains a long value.
|
boolean |
isBytesLeaf()
Check if a tuple contains a Byte array value.
|
boolean |
isDoubleLeaf()
Check if a tuple contains a double value.
|
boolean |
isEmpty()
Check if a tuple contains some value.
|
boolean |
isEquals(Object o)
Check if a tuple contains a leaf value and if it is equal to a given value
|
boolean |
isLeaf() |
boolean |
isLongLeaf()
Check if a tuple contains a long value.
|
boolean |
isRoot()
Return true if this tuple is a root one.
|
boolean |
isSet()
Check if this tuple holds a set
|
boolean |
isStringLeaf()
Check if a tuple contains a String value.
|
boolean |
isTuple()
Return true if this tuple contains a tuple value
|
String |
leafAsString()
Return any leaf value as a String.
|
int |
length()
String API, returns the length of a String leaf Tuple.
|
boolean |
matches(String regex)
String API : Tells whether or not this string matches the given regular
expression.
|
Tuple |
merge(Tuple b)
See mergeWith().
|
Tuple |
mergeWith(Tuple b)
Return a merge of two Tuples A and B recursively.
|
Tuple |
putAll(Map<String,Object> map)
Put all the provided tuple key values in this tuple.
|
void |
remove()
Remove this tuple.
|
Tuple |
remove(String key)
Return and removes a sub-tuple by key.
|
boolean |
remove(Tuple element)
Set operation: remove an element from a set.
|
boolean |
removeElement(Object value)
Remove an element from an array or set.
|
String |
replace(char oldChar,
char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this
string with newChar.
|
String |
replaceAll(String regex,
String replacement)
Replaces each substring occurence of this string that matches the given
regular expression
regex with the given replacement . |
Tuple |
replaceAllKeys(String regex,
String replacement)
For each inner property Tuple contained in this tuple, replaces each
substring occurence of its key that matches the given regular expression with
the given replacement.
|
Tuple |
replaceAllKeyValues(String regex,
String replacement)
Combines
replaceAllKeys(java.lang.String, java.lang.String) and replaceAllValues(java.lang.String, java.lang.String) in one call. |
Tuple |
replaceAllValues(String regex,
String replacement)
For each inner String value contained in this tuple, replaces each substring
occurence that matches the given regular expression with the given
replacement.
|
String |
replaceFirst(String regex,
String replacement)
Replaces the first substring of this string that matches the given regular
expression with the given replacement.
|
Tuple |
select(String... keys)
Given a tuple, return a selection of one or several of its child.
|
Tuple |
set(Object v)
Assign this tuple a new leaf value
|
Tuple |
set(String key,
Object v)
Add an object as property to this tuple.
|
int |
size()
Return the number of fields in this tuple.
|
void |
skip()
Skip a tuple value.
|
String[] |
split(String s)
Splits this string around matches of the given regular expression.
|
Tuple[] |
splitAsTupleArray(String s)
Split this string into a Tuple array of the given regular expression.
|
boolean |
startsWith(String s)
Split a string tuple.
|
String |
substring(int beginIndex) |
String |
substring(int beginIndex,
int endIndex) |
String |
toAsciiJson()
Watchout, if you use this variant, any accent will be transformed into an
ascii unicode representation.
|
String |
toEscapedJson() |
String |
toFlat() |
String |
toJson()
This method preserves non ascii characters.
|
Tuple |
toLowerCase() |
Map<String,Object> |
toMap()
Convert a tuple to a Map.
|
String |
toString()
Return a String representation of the content of this tuple.
|
Tuple |
toUpperCase() |
String |
trim()
Methods inherited from plain Strings.
|
Boolean |
tryBoolean()
Try to parse the given value as Boolean.
|
boolean |
tryBoolean(boolean b)
Try to parse as Boolean or return a default value.
|
byte[] |
tryBytes()
Try to parse the given value as Byte array.
|
byte[] |
tryBytes(byte[] b)
Try to parse as Byte array or return a default value.
|
Double |
tryDouble()
Try to parse the given value as Double.
|
double |
tryDouble(double d)
Try to parse as Double or return a default value.
|
String |
tryIp()
Efficiently check for valid ip address.
|
String |
tryIp(String defaultValue)
Efficiently check for a valid ip address.
|
String |
tryIpv4()
Efficiently check for an dot decimal ipv4 ip address.
|
String |
tryIpv4(String defaultValue)
Efficiently check for an dot decimal ipv4 ip address.
|
String |
tryIpv6()
Efficiently check for an dot decimal ipv6 ip address.
|
String |
tryIpv6(String defaultValue)
Efficiently check for an dot decimal ipv6 ip address.
|
Long |
tryLong()
Try to parse the given value as Long.
|
long |
tryLong(long l)
Try to parse as Long or return default.
|
compareTo
void attach(String key, Object value)
getAttachment(String)
. Attachments are transient data, they survive
as long as the holding tuple survives. Besides, they will not appear in the
toString()
String representation of this tuple.key
- the key for retrieving the attachment latervalue
- the attachment valueObject getAttachment(String key)
key
- the attachment keyTuple append(Object element)
element
- the element to appendTuple add(Object element)
As expected if you add several times the same element, it will be inserted only once.
This method erases any previous value stored inside the tuple if it was not a set.
element
- the Tuple you want to addList<Tuple> asArray()
Watch out, if the tuple is a set, the order will be random.
PunchRuntimeException
- if the contained value is not an array or a setbyte[] asBytes()
PunchRuntimeException
- if the contained value is not an array of bytesCollection<Tuple> asCollection()
PunchRuntimeException
- if the contained value is not a collectionSet<Tuple> asSet()
PunchRuntimeException
- if the contained value is not a setboolean asBoolean()
PunchRuntimeException
- if the contained value is not a booleanBoolean tryBoolean()
boolean tryBoolean(boolean b)
b
- the default valuedouble asDouble()
PunchRuntimeException
- if the contained value is not a doubleDouble tryDouble()
double tryDouble(double d)
d
- a default v aluefloat asFloat()
PunchRuntimeException
- if the contained value is not a floatint asInt()
PunchRuntimeException
- if the contained value is not a longlong asLong()
PunchRuntimeException
- if the contained value is not a longLong tryLong()
String tryIpv4()
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ip] = [message][callerIpAddress].tryIpv4();
String tryIpv4(String defaultValue)
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ip] = [message][callerIpAddress].tryIpv4("0.0.0.0");
defaultValue
- the default value to returnString tryIpv6()
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ipv6] = [message][callerIpAddress].tryIpv6();
String tryIp(String defaultValue)
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ip] = [message][callerIpAddress].tryIp("0.0.0.0");
defaultValue
- the default value to returnString tryIp()
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ip] = [message][callerIpAddress].tryIp();
String tryIpv6(String defaultValue)
This method will return a dotted numeric representation of convertible ip addresses.
// this statement has no effect if there is no ipv4 value in
// [message][callerIpAddress]
[logs][log][client][ipv6] = [message][callerIpAddress].tryIpv6("::0");
defaultValue
- the default value to returnlong tryLong(long l)
l
- the default valueString asString()
PunchRuntimeException
- if the contained value is not a StringString asString(String fmt, Object... values)
The additional argument will be used to format an error message in case an exception is raised. This is handy to write log parsers that report clear errors.
fmt
- values
- PunchRuntimeException
- with the provided message if the input is not a stringString leafAsString()
PunchRuntimeException
- if the contained value is not a StringString concat(String str)
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
str
- the string to concatenateboolean contains(Object element)
String.contains(CharSequence)
call.
If your tuple is a set it behaves as a Set.contains(Object)
call. Watchout this method
is inneficient for large sets.
element
- the elementPunchRuntimeException
- if the tuple is not a set or a string.Tuple[] elements()
void empty()
boolean exists()
Tuple get(String key)
Watch out, if you get a null value, it means that the Tuple does not contain what you look for OR that it contains no subtuple, i.e. a leaf value, an array or a set.
key
- the key of the sub tupleTuple getDeep(String key)
key
- the key of the sub-tuple. It can be plain String format "name" or
punch-style format "[user][name]"Tuple getDeepEs(String key)
key
- the key of the sub tuple. Accepted forms are "name", "[user][name]" or "user.name"Tuple remove(String key)
key
- the key of the sub tupleTuple get(Tuple key)
This variant expect the string key to be contained in a tuple. It will convert the leaf value to a string, and fail if it is not a leaf value.
key
- the sub-tuple keyTuple getByKey(String key)
key
- the sub tuple keyTuple getElement(int index)
index
- the index of the element to retrievePunchRuntimeException
- if it is not an array, if the index is wrongList<Entry> getEntries()
List<String> getKeys()
PunchRuntimeException
- if the tuple is not a map.Collection<Tuple> getValues()
PunchRuntimeException
- if the tuple is not a collection.Iterator<Object> getIterator()
boolean hasKey(String key)
key
- the key to look forint indexOf(char ch)
ch
- the characterint indexOf(char ch, int fromIndex)
ch
- the characterfromIndex
- the start indexint indexOf(String str)
str
- the substring to search for.int indexOf(String str, int fromIndex)
str
- the substring to search for, starting the search at the
specified index.fromIndex
- the starting index to start searching fromboolean isArray()
boolean isBooleanLeaf()
boolean isDoubleLeaf()
boolean isEmpty()
boolean isEquals(Object o)
o
- a java object valueboolean isLeaf()
boolean isLongLeaf()
boolean isRoot()
boolean isSet()
boolean isStringLeaf()
boolean isBytesLeaf()
boolean isTuple()
int length()
PunchRuntimeException
- if the tuple contains something else than a
String leaf.boolean matches(String regex)
regex
- the regular expression to which this string is to be matchedPunchRuntimeException
- if not a String leaf tupleTuple mergeWith(Tuple b)
TypesA/B | B Leaf | B Array or Set | B Map |
---|---|---|---|
A Leaf | B | B | B |
A Array or Set | A | Fusion A&B | Exception |
A Map | A | Exception | Recursive Merge on Keys |
b
- The Tuple to be merged.PunchRuntimeException
- if a merge is not possible (Array or Set with
Map).Tuple merge(Tuple b)
b
- The Tuple to be merged.PunchRuntimeException
- if a merge is not possible (Array or Set with
Map).boolean remove(Tuple element)
element
- the contained tuple to removePunchRuntimeException
- if there's something else than a setvoid remove()
String replace(char oldChar, char newChar)
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a String object is returned that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar. Examples:
"mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring with a purple porpoise".replace('p', 't') returns "starring with a turtle tortoise" "JonL".replace('q', 'x') returns "JonL" (no change)
oldChar
- the old charnewChar
- the new charString replaceAll(String regex, String replacement)
regex
with the given replacement
. This
methods only acts on the Tuple String value. It has no effect if the Tuple
contains somethng else than a String.
Check the replaceAllValues(java.lang.String, java.lang.String), replaceAllKeys(java.lang.String, java.lang.String) or replaceAllKeyValues(java.lang.String, java.lang.String) for recursive versions that do the job on all keys and?or values contained in a nested Tuple.
regex
- the regular expressionreplacement
- the replacement StringTuple replaceAllValues(String regex, String replacement)
regex
- the regular expressionreplacement
- the replacement StringTuple replaceAllKeys(String regex, String replacement)
regex
- the regular expressionreplacement
- the replacement StringTuple replaceAllKeyValues(String regex, String replacement)
replaceAllKeys(java.lang.String, java.lang.String)
and replaceAllValues(java.lang.String, java.lang.String)
in one call.regex
- the regular expressionreplacement
- the replacement StringString replaceFirst(String regex, String replacement)
regex
- the regular expressionreplacement
- the replacement StringTuple set(Object v)
v
- the valueTuple set(String key, Object v)
key
- the key of your new fieldv
- the new field valueint size()
String substring(int beginIndex)
beginIndex
- the substring start indexPunchRuntimeException
- if not a stringString substring(int beginIndex, int endIndex)
beginIndex
- the substring start indexendIndex
- the substring end indexPunchRuntimeException
- if not a stringTuple toLowerCase()
PunchRuntimeException
- if not a stringTuple cutPrefix(String s)
s
- the string valueTuple cutSuffix(String s)
s
- the string valueboolean startsWith(String s)
s
- the string valueString[] split(String s)
s
- - the delimiting regular expressionPunchRuntimeException
- if not a stringTuple[] splitAsTupleArray(String s)
s
- - the delimiting regular expressionPunchRuntimeException
- if not a stringboolean endsWith(String s)
s
- the String to match a suffixString toString()
Watchout : this method was originally documented as returning a JSON
representation. If you need that use instead the or
toAsciiJson()
variants. If you are outside a punchlet, prefer the
TupleFactory.getJsonFromTuple(Tuple)
method and the likes.
This method is completely different than asString()
. Use it to
produce a Json.
String toJson()
String toAsciiJson()
String dump()
This method is completely different than toString()
in that it will
not escape inner Strings. Use it to debug the tuple you work with.
Tuple toUpperCase()
PunchRuntimeException
- if not a stringMap<String,Object> toMap()
String trim()
See String.trim()
.
PunchRuntimeException
- if not a stringString get(String key, String defaultValue)
key
- the key to look fordefaultValue
- a default value to return if no value is associated to
the keylong get(String key, long defaultValue)
key
- the key to look fordefaultValue
- a default value to return if no value is associated to
the keyString getType()
boolean containsOneOf(String[] strs)
Here is a typical usage :
if (tuple.containsOneOf("CNN", "FOX", "TF1")) {
...
}
Should you need to ignore the case :
if (tuple.toLowerCase().containsOneOf("cnn", "fox", "tf1")) {
...
}
strs
- the list of String to check forboolean containsOneOf(Tuple resource)
Please note that this tuple must be a Leaf tuple, more precisely of type String for this to work. Otherwise, you will get false.
Use this variant only if your set of substrings does never change. It is the most efficient in particular if you have many sub-strings to check for.
An example will best illustrate how it works. Say you have a domain name in a tuple field, [host][name] that contains for example "www.mydomain-prod.com". Suppose you want to know if that domain name contains one of the following substring
[ "prod", "admin" ]
Put your substring in a resource file (say) my-patterns.json. You can then simply write :
Tuple tuple = [host][name]
...
if (tuple.containsOneOf(getResourceTuple("my-patterns"))) {
...
}
resource
- your resource tuple, containing your string atomsboolean equalsOneOf(Tuple resource)
An example will best illustrate how it works. Say you have a domain name in a tuple field, [host][name] that contains for example "www.mydomain-prod.com". Suppose you want to know if that domain name contains one of the following substring
{ "prod", "admin" }
Put your substring in a resource file (say) my-patterns.json. You can then simply write :
if (tuple.equalsOneOf(getResourceTuple("my-patterns"))) {
...
}
resource
- your resource tuple, containing your string atomsTuple getElement()
boolean removeElement(Object value)
value
- the value to removeTuple getSingleChild()
It is handy to deal with a very common punchplatform use case : receiving logs as single stream/field value. If you know that by configuration you can only receive a root tuple from a Storm PunchBolt as a single valued [streamId][fieldId] tuple, then you can simply write :
tuple = root.getSingleChild().getSingleChild();
PunchRuntimeException
- if the tuple does not contain a single sub
tuple value or one named "default"Tuple duplicate()
String encodeBase64()
Usage examples: String encoded = [a].encodeBase64(); [b] = [a].encodeBase64();
String decodeBase64()
Usage example: String decoded = [a].decodeBase64(); [b] = [a].decodeBase64();
Tuple select(String... keys)
// say input is a metricbeat document with tons of fields including a "metricset" part
// Executing this
Tuple t = input.select("metricset");
// produces a tuple t containing only "metricset", i.e. :
// "metricset" : {
// "module": "system",
// "name": "load",
// "rtt": 29
// }
}
keys
- the key(s) you want to selectString toFlat()
String toEscapedJson()
Tuple fromEscapedJson()
PunchRuntimeException
- if the contained value is not a stringvoid skip()
// root : { "a" : { "b" : { "c" : 3 } } }
root:[a].skip();
// you have root == { "b" : { "c" : 3 } }
double[] asDoubleArray()
Longs are converted to doubles, String also as long as they represent a valid double number.
If your tuple contains a single value an array of size 1 will be returned.
long[] asLongArray()
Longs are converted to doubles, String also as long as they represent a valid double number.
If your tuple contains a single value an array of size 1 will be returned.
String[] asStringArray()
Longs are converted to doubles, String also as long as they represent a valid double number.
If your tuple contains a single value an array of size 1 will be returned.
GeoFeatureTuple asGeoShape()
byte[] tryBytes()
byte[] tryBytes(byte[] b)
b
- the default valueCopyright © 2022. All rights reserved.