The split function in Hive splits a string into an array of substrings based on a regular expression delimiter. Its syntax is split(string str, string regex), and it returns an array<string>. For example, split('a,b,c', ',') returns ['a','b','c'].
How the split Function Works
Hive’s split uses Java’s regular expression engine (java.util.regex) to find delimiters. The function scans the input string from left to right, and each time it finds a match for the regex, it treats that match as a separator. The text between separators becomes an element in the output array. If the regex matches at the beginning of the string, the first element will be an empty string. Similarly, trailing delimiters produce trailing empty strings.
Key behavior details:
- If the input string is
NULL, the result isNULL. - If the regex is empty or matches an empty string, the behavior is similar to Java’s
String.split– it may split into individual characters in some cases, but it is safer to avoid empty regexes. - The function does not trim spaces.
split('a, b', ',')returns['a', ' b']– note the leading space in the second element. - The result is an array, so you can access elements by index (0-based) using
[0],[1], etc., or use array functions likesize()andexplode().
Basic Examples and Usage
Here are common ways to use split in Hive queries:
- Split a comma-separated list:
SELECT split('apple,banana,cherry', ',')returns['apple','banana','cherry']. - Split on a dot (remember to escape it):
SELECT split('192.168.1.1', '\\.')returns['192','168','1','1']. In HiveQL, you need double backslashes because the string literal itself processes escapes. - Split on multiple possible delimiters using regex:
SELECT split('a;b,c', '[;,]')returns['a','b','c']. - Access a specific element:
SELECT split('a-b-c', '-')[1]returns'b'. - Count elements:
SELECT size(split('a,b,c', ','))returns3.
Using split with explode for Row Expansion
A very common pattern is to combine split with explode to turn a delimited string into multiple rows. For example, if you have a table orders with a column items containing ‘item1,item2’, you can do:
SELECT order_id, single_item
FROM orders
LATERAL VIEW explode(split(items, ',')) t AS single_item;
This produces one row per item, which is useful for joins, aggregations, or further processing. You can also use posexplode if you need the position index along with the element.
Common Pitfalls and Misconceptions
Several issues trip up users new to split:
- Escaping special characters: Because the delimiter is a regex, characters like dot (
.), pipe (|), and backslash (\) need escaping. For a literal dot, use\\.in HiveQL. For a pipe, use\\|. - Empty strings: If your data has consecutive delimiters, you get empty strings. For example,
split('a,,b', ',')returns['a','','b']. To filter them out, you can usefilter()(Hive 2.2+) or aWHEREclause after exploding. - Trailing delimiters:
split('a,b,', ',')returns['a','b','']. This is often unexpected. - NULL handling: If the string column is NULL, the result is NULL, not an array with a NULL element. Use
COALESCEif you need a default. - Performance: Splitting large strings in a SELECT on many rows can be CPU-intensive. If you only need a substring, consider
substrorregexp_extractinstead.
Alternatives to split
Depending on your goal, other Hive functions might be more appropriate:
regexp_extract– extracts a specific part of a string using a regex group, useful when you need one piece rather than all pieces.substrandinstr– for fixed-position or simple substring extraction.parse_url– for URL components.str_to_map– splits a string into a map using two delimiters (e.g., ‘k1:v1,k2:v2’).explodewithsplit– as shown above, for row expansion.
Frequently Asked Questions
Does split return an array or a string?
It returns an array of strings (array<string>). You can access elements by index or pass the array to functions like size, explode, or array_contains.
How do I split on a pipe character?
Pipe is a regex alternation operator, so you need to escape it. Use split('a|b', '\\|') in HiveQL. The double backslash is required because the string literal processes one level of escaping, leaving a single backslash for the regex.
Why do I get an empty string as the first element?
If the string starts with the delimiter, the first element is empty. For example, split(',a', ',') returns ['','a']. This is standard behavior in Java’s String.split. You can filter out empty strings after exploding if needed.
Can I split a string into characters?
Yes, you can use an empty regex: split('abc', '') typically returns ['a','b','c'] in Hive, but this behavior is not guaranteed across versions and may produce leading/trailing empty strings. A safer approach is to use regexp_extract_all or a UDF if you need each character.
What is the difference between split and explode?
split returns an array in a single row. explode is a table-generating function that takes an array (or map) and produces one row per element. They are often used together: explode(split(...)) turns a delimited string into multiple rows.
Is split case-sensitive?
Yes, the regex matching is case-sensitive by default. If you need case-insensitive splitting, you can use a regex with inline flags, like (?i) at the start: split('aBc', '(?i)b') returns ['a','c'].
Related
- Filtering for Duplicates in Excel: 4 Fast Ways
- JavaScript Random Number in Range: Simple Guide
- Alt Shortcut Keys: The Complete List & How to Use Them
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Erase Function in String C++: How It WorksThe erase() function in C++ strings removes characters from a std::string, and it has three overloads: erase by…
- Excel Formula for Difference Between Two Numbers ExplainedTo find the difference between two numbers in Excel, subtract one cell from the other: if the first…
- Excel XLOOKUP Formula Examples for Everyday LookupsExcel’s XLOOKUP function finds a value in one range and returns a related value from another. For example,…
- Excel Formula Budget Calculator: How to Build OneTo turn Excel into a budget calculator, use a simple formula like =SUM(B2:B13) to total your income and…
- Calculating r in Excel: Formula and ExamplesTo calculate Pearson’s correlation coefficient, or r, in Excel, use =CORREL(A2:A6,B2:B6) when the paired x values are in…
- TRIM in Excel for Text Cleanup: What It RemovesIn Excel for Microsoft 365 on Windows, use TRIM to remove ordinary spaces at the beginning and end…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.