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.util.Iterator;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.TreeMap;
26  import org.apache.hadoop.record.Buffer;
27  
28  public class ChukwaRecord extends ChukwaRecordJT implements Record {
29    public ChukwaRecord() {
30    }
31  
32    public void add(String key, String value) {
33      synchronized (this) {
34        if (this.mapFields == null) {
35          this.mapFields = new TreeMap<String, org.apache.hadoop.record.Buffer>();
36        }
37      }
38      this.mapFields.put(key, new Buffer(value.getBytes()));
39    }
40  
41    public String[] getFields() {
42      return this.mapFields.keySet().toArray(new String[0]);
43    }
44  
45    public String getValue(String field) {
46      if (this.mapFields.containsKey(field)) {
47        return new String(this.mapFields.get(field).get());
48      } else {
49        return null;
50      }
51    }
52  
53    public boolean containsField(String field) {
54      return this.mapFields.containsKey(field);
55    }
56  
57    public void removeValue(String field) {
58      if (this.mapFields.containsKey(field)) {
59        this.mapFields.remove(field);
60      }
61    }
62  
63    @Override
64    public String toString() {
65      Set<Map.Entry<String, Buffer>> f = this.mapFields.entrySet();
66      Iterator<Map.Entry<String, Buffer>> it = f.iterator();
67  
68      Map.Entry<String, Buffer> entry = null;
69      StringBuilder sb = new StringBuilder();
70      sb.append("<event  ");
71      StringBuilder body = new StringBuilder();
72  
73      String key = null;
74      String val = null;
75      boolean hasBody = false;
76      String bodyVal = null;
77      while (it.hasNext()) {
78        entry = it.next();
79        key = entry.getKey().intern();
80        val = new String(entry.getValue().get());
81        if (key.intern() == Record.bodyField.intern()) {
82          hasBody = true;
83          bodyVal = val;
84        } else {
85          sb.append(key).append("=\"").append(val).append("\" ");
86          body.append(key).append(" = ").append(val).append("<br>");
87        }
88  
89      }
90      if (hasBody) {
91        sb.append(">").append(bodyVal);
92      } else {
93        sb.append(">").append(body);
94      }
95      sb.append("</event>");
96  
97      return sb.toString();
98    }
99  
100 }