このような行を含むデータセットがあり199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET /history/apollo/ HTTP/1.0" 200 6245
、hadoop で map reduce ジョブを実行している場合、各行の最後の要素を取得するにはどうすればよいですか?
私はすべての明白な答えを試しましString lastWord = test.substring(test.lastIndexOf(" ")+1);
たが、これは私に-
性格を与えます. スペースに基づいて分割し、最後の要素を取得しようとしましたが、最後の文字はまだ-
.
データが 1 行ずつ配信されるとは期待できませんか。つまり、フォーム内のファイルが 1 a b c d \n e f g h\n
行ずつ配信されることを期待できないのでしょうか?
そして、この行の最後の単語を取得する方法に関するヒントはありますか?
これは、データを取得しようとするマップ関数のスニペットです。
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String test = value.toString();
StringTokenizer tokenizer = new StringTokenizer(test);
//String lastWord = test.substring(test.lastIndexOf(" ")+1); <--first try
//String [] array = test.split(" ");//<--second try
//one.set(Integer.valueOf(array[8]));
int i = 0;
String candidate = null;
while (tokenizer.hasMoreTokens()) {
candidate = tokenizer.nextToken();
if (i == 3) {
//this works to get the date field
String wholeDate = candidate;
String[] dateArray = wholeDate.split(":");
String date = dateArray[0].substring(1); // get rid of '['
String hour = dateArray[1];
word.set(date + " " + hour);
} else if (i == 7) {
// <-- third try
String replySizeString = candidate;
one.set(Integer.valueOf(replySizeString)); }
}
i++;