天天看點

常見資料類型-HadoopDataType(僅僅作為記錄)

1 package mapred;
 2 
 3 import org.apache.hadoop.io.ArrayWritable;
 4 import org.apache.hadoop.io.IntWritable;
 5 import org.apache.hadoop.io.MapWritable;
 6 import org.apache.hadoop.io.NullWritable;
 7 import org.apache.hadoop.io.Text;
 8 
 9 /** hadoop資料類型與java資料類型 */
10 public class HadoopDataType {
11     /** 使用hadoop的Text類型資料 */
12     public static void testText() {
13         System.out.println("testText");
14         Text text = new Text("hello hadoop!");
15         System.out.println(text.getLength());
16         System.out.println(text.find("a"));
17         System.out.println(text.toString());
18     }
19 
20     /** 使用ArrayWritable */
21     public static void testArrayWritable() {
22         System.out.println("testArrayWritable");
23         ArrayWritable arr = new ArrayWritable(IntWritable.class);
24         IntWritable year = new IntWritable(2017);
25         IntWritable month = new IntWritable(07);
26         IntWritable date = new IntWritable(01);
27         arr.set(new IntWritable[] { year, month, date });
28         System.out.println(String.format("year=%d,month=%d,date=%d",
29                 ((IntWritable) arr.get()[0]).get(),
30                 ((IntWritable) arr.get()[1]).get(),
31                 ((IntWritable) arr.get()[2]).get()));
32     }
33 
34     /** 使用MapWritable */
35     public static void testMapWritable() {
36         System.out.println("testMapWritable");
37         MapWritable map = new MapWritable();
38         Text k1 = new Text("name");
39         Text v1 = new Text("tonny");
40         Text k2 = new Text("password");
41         map.put(k1, v1);
42         map.put(k2, NullWritable.get());
43         System.out.println(map.get(k1).toString());
44         System.out.println(map.get(k2).toString());
45     }
46 
47     // 入口 main
48     public static void main(String[] args) {
49         testText();
50         testArrayWritable();
51         testMapWritable();
52     }
53 }      
13
7
hello hadoop!
testArrayWritable
year=2017,month=7,date=1
testMapWritable
tonny
(null)      

個人學習記錄