hive split 如何取最后一个分隔符
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive split 如何取最后一个分隔符相关的知识,希望对你有一定的参考价值。
参考技术A PIG中输入输出分隔符默认是制表符\t,而到了hive中,默认变成了八进制的\001, 也就是ASCII: ctrl - A Oct Dec Hex ASCII_Char 001 1 01 SOH (start of heading) 官方的解释说是尽量不和文中的字符重复,因此选用了 crtrl - A,单个的字符可以...本回答被提问者采纳我该如何使用“。”作为java中String.split()的分隔符[重复]
【中文标题】我该如何使用“。”作为java中String.split()的分隔符[重复]【英文标题】:How can I use "." as the delimiter with String.split() in java [duplicate] 【发布时间】:2011-02-14 21:42:07 【问题描述】:我要做的是读取一个 .java 文件,然后挑选出所有标识符并将它们存储在一个列表中。我的问题是 .split() 方法。如果您按原样运行此代码,您将获得 ArrayOutOfBounds,但如果您将分隔符从“.”更改为对于其他任何事情,代码都有效。但我需要用“。”解析的行。那么我还有其他方法可以做到这一点吗?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class MyHash
private static String[] reserved = new String[100];
private static List list = new LinkedList();
private static List list2 = new LinkedList();
public static void main (String args[])
Hashtable hashtable = new Hashtable(997);
makeReserved();
readFile();
String line;
ListIterator itr = list.listIterator();
int listIndex = 0;
while (listIndex < list.size())
if (itr.hasNext())
line = itr.next().toString();
//PROBLEM IS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
String[] words = line.split("."); //CHANGE THIS AND IT WILL WORK
System.out.println(words[0]); //TESTING TO SEE IF IT WORKED
listIndex++;
public static void readFile()
String text;
String[] words;
BufferedReader in = null;
try
in = new BufferedReader(new FileReader("MyHash.java")); //NAME OF INPUT FILE
catch (FileNotFoundException ex)
Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
try
while ((text = in.readLine()) != null)
text = text.trim();
words = text.split("\\s+");
for (int i = 0; i < words.length; i++)
list.add(words[i]);
for (int j = 0; j < reserved.length; j++)
if (list.contains(reserved[j]))
list.remove(reserved[j]);
catch (IOException ex)
Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
try
in.close();
catch (IOException ex)
Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
public static int keyIt (int x)
int key = x % 997;
return key;
public static int horner (String word)
int length = word.length();
char[] letters = new char[length];
for (int i = 0; i < length; i++)
letters[i]=word.charAt(i);
char[] alphabet = new char[26];
String abc = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 26; i++)
alphabet[i]=abc.charAt(i);
int[] numbers = new int[length];
int place = 0;
for (int i = 0; i < length; i++)
for (int j = 0; j < 26; j++)
if (alphabet[j]==letters[i])
numbers[place]=j+1;
place++;
int hornered = numbers[0] * 32;
for (int i = 1; i < numbers.length; i++)
hornered += numbers[i];
if (i == numbers.length -1)
return hornered;
hornered = hornered % 997;
hornered *= 32;
return hornered;
public static String[] makeReserved ()
reserved[0] = "abstract";
reserved[1] = "assert";
reserved[2] = "boolean";
reserved[3] = "break";
reserved[4] = "byte";
reserved[5] = "case";
reserved[6] = "catch";
reserved[7] = "char";
reserved[8] = "class";
reserved[9] = "const";
reserved[10] = "continue";
reserved[11] = "default";
reserved[12] = "do";
reserved[13] = "double";
reserved[14] = "else";
reserved[15] = "enum";
reserved[16] = "extends";
reserved[17] = "false";
reserved[18] = "final";
reserved[19] = "finally";
reserved[20] = "float";
reserved[21] = "for";
reserved[22] = "goto";
reserved[23] = "if";
reserved[24] = "implements";
reserved[25] = "import";
reserved[26] = "instanceof";
reserved[27] = "int";
reserved[28] = "interface";
reserved[29] = "long";
reserved[30] = "native";
reserved[31] = "new";
reserved[32] = "null";
reserved[33] = "package";
reserved[34] = "private";
reserved[35] = "protected";
reserved[36] = "public";
reserved[37] = "return";
reserved[38] = "short";
reserved[39] = "static";
reserved[40] = "strictfp";
reserved[41] = "super";
reserved[42] = "switch";
reserved[43] = "synchronize";
reserved[44] = "this";
reserved[45] = "throw";
reserved[46] = "throws";
reserved[47] = "trasient";
reserved[48] = "true";
reserved[49] = "try";
reserved[50] = "void";
reserved[51] = "volatile";
reserved[52] = "while";
reserved[53] = "=";
reserved[54] = "==";
reserved[55] = "!=";
reserved[56] = "+";
reserved[57] = "-";
reserved[58] = "*";
reserved[59] = "/";
reserved[60] = "";
reserved[61] = "";
return reserved;
【问题讨论】:
【参考方案1】:String.split
采用正则表达式和 '.'对正则表达式有特殊意义。
你(可能)想要这样的东西:
String[] words = line.split("\\.");
有些人似乎无法让它工作,所以这里有一些可运行的代码,您可以使用它来验证正确的行为。
import java.util.Arrays;
public class TestSplit
public static void main(String[] args)
String line = "aa.bb.cc.dd";
String[] words = line.split("\\.");
System.out.println(Arrays.toString(words));
// Output is "[aa, bb, cc, dd]"
【讨论】:
这对我不起作用...line.split(Pattern.quote("."));做了 @AutoMEta 你一定是打错了什么,或者误解了你得到的结果。正则表达式"\\."
对于匹配句号是正确的。 @prunge 对此问题的Pattern.quote(".")
回答对于可能包含不需要的正则表达式元字符的任何拆分也是一个很好的通用解决方案。
@AutoMeta 你是对的。此方法不再有效,并且总是给出空数组。你的方法对我有用。【参考方案2】:
split 的参数是一个正则表达式。 “。”匹配任何内容,因此您要拆分的分隔符是任何内容。
【讨论】:
【参考方案3】:您是否尝试过转义点?像这样:
String[] words = line.split("\\.");
【讨论】:
【参考方案4】:split 的参数是一个正则表达式。句点是匹配任何内容的正则表达式元字符,因此line
中的每个字符都被视为拆分字符,并被丢弃,并且它们之间的所有空字符串都被丢弃(因为它们是空字符串) .结果是你一无所有。
如果您转义句点(通过在其前添加转义的反斜杠),则可以匹配文字句点。 (line.split("\\.")
)
【讨论】:
【参考方案5】:您可能对StringTokenizer 课程感兴趣。但是,java 文档建议您使用 .split 方法,因为 StringTokenizer 是一个遗留类。
【讨论】:
【参考方案6】:如果性能是一个问题,您应该考虑使用StringTokenizer
而不是split
。 StringTokenizer
比 split
快得多,尽管它是“遗留”类(但未弃用)。
【讨论】:
【参考方案7】:用字符串文字分隔符分割时,最安全的方法是使用Pattern.quote()方法:
String[] words = line.split(Pattern.quote("."));
正如其他答案所述,与 "\\."
拆分是正确的,但 quote()
会为您转义。
【讨论】:
用“\\.”分割不再起作用。你和@AutoMeta 的答案是正确的。这应该是首选方式。 与"\\."
拆分仍然适用于当前版本。【参考方案8】:
这绝对不是最好的方法,但是,我通过以下方式完成了它。
String imageName = "my_image.png";
String replace = imageName.replace('.','~');
String[] split = replace.split("~");
System.out.println("Image name : " + split[0]);
System.out.println("Image extension : " + split[1]);
输出,
Image name : my_image
Image extension : png
【讨论】:
你知道这不是一个好方法,这个问题已经有一些正确的方法,那么分享这个“不干净”的有什么意义呢? (只是问) 我从来不知道为什么 split 不适用于 dot。所以,我尝试了不同的方法。看到这个答案后,我认为我所做的很酷。因为我在不知道问题的情况下完成了它。我添加这个答案只是为了表明我们可以用我们拥有的东西做一些事情,即使没有对整个概念的理解。就是这样。以上是关于hive split 如何取最后一个分隔符的主要内容,如果未能解决你的问题,请参考以下文章