This project has retired. For details please refer to its Attic page.
ChukwaRecord xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.extraction.engine;
20  
21  
22  import java.nio.charset.Charset;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeMap;
27  
28  import org.apache.hadoop.record.Buffer;
29  
30  public class ChukwaRecord extends ChukwaRecordJT implements Record {
31    public ChukwaRecord() {
32    }
33  
34    public void add(String key, String value) {
35      synchronized (this) {
36        if (this.mapFields == null) {
37          this.mapFields = new TreeMap<String, Buffer>();
38        }
39      }
40      this.mapFields.put(key, new Buffer(value.getBytes(Charset.forName("UTF-8"))));
41    }
42  
43    public String[] getFields() {
44      return this.mapFields.keySet().toArray(new String[0]);
45    }
46  
47    public String getValue(String field) {
48      if (this.mapFields.containsKey(field)) {
49        return new String(this.mapFields.get(field).get(), Charset.forName("UTF-8"));
50      } else {
51        return null;
52      }
53    }
54  
55    public boolean containsField(String field) {
56      return this.mapFields.containsKey(field);
57    }
58  
59    public void removeValue(String field) {
60      if (this.mapFields.containsKey(field)) {
61        this.mapFields.remove(field);
62      }
63    }
64  
65    @Override
66    public String toString() {
67      Set<Map.Entry<String, Buffer>> f = this.mapFields.entrySet();
68      Iterator<Map.Entry<String, Buffer>> it = f.iterator();
69  
70      Map.Entry<String, Buffer> entry = null;
71      StringBuilder sb = new StringBuilder();
72      sb.append("<event  ");
73      StringBuilder body = new StringBuilder();
74  
75      String key = null;
76      String val = null;
77      boolean hasBody = false;
78      String bodyVal = null;
79      while (it.hasNext()) {
80        entry = it.next();
81        key = entry.getKey().intern();
82        val = new String(entry.getValue().get(), Charset.forName("UTF-8"));
83        if (key.intern() == Record.bodyField.intern()) {
84          hasBody = true;
85          bodyVal = val;
86        } else {
87          sb.append(key).append("=\"").append(val).append("\" ");
88          body.append(key).append(" = ").append(val).append("<br>");
89        }
90  
91      }
92      if (hasBody) {
93        sb.append(">").append(bodyVal);
94      } else {
95        sb.append(">").append(body);
96      }
97      sb.append("</event>");
98  
99      return sb.toString();
100   }
101 
102 }