Browse Source

添加hadoop基础操作:
CommonOperation.java

liuyuqi-dellpc 6 years ago
parent
commit
1164bd2fb2

+ 2 - 0
.gitignore

@@ -12,3 +12,5 @@
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
 
+/build/
+/target/

+ 0 - 0
auto_install/copy-conf.sh


+ 0 - 0
auto_install/install.sh


+ 0 - 0
auto_install/start-all.sh


+ 20 - 0
pom.xml

@@ -0,0 +1,20 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>hadoop-tools</groupId>
+  <artifactId>hadoop-tools</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <description>my hadoop-tools</description>
+  <build>
+    <sourceDirectory>src</sourceDirectory>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.5.1</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

+ 88 - 0
src/me/yoqi/hadoop/test/CommonOperation.java

@@ -0,0 +1,88 @@
+package me.yoqi.hadoop.test;
+
+import java.io.IOException;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataInputStream;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+/**hadoop通用操作:新建文件/文件夹/读写文件等等
+ * 
+ * @author liuyuqi
+ *
+ */
+public class CommonOperation {
+
+	public static void main(String[] args) throws IOException {
+		Path path = new Path("/user/root/test1");
+		makeDir(path);
+	}
+
+	/**
+	 * @param path
+	 * @return
+	 * @throws IOException
+	 */
+	public static boolean makeDir(Path path) throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);// 有异常抛出
+		boolean flag = fs.mkdirs(path);
+		// fs.create(path);
+		fs.close();
+		return flag;
+	}
+
+	public boolean delDir(Path path) throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);// 有异常抛出
+		boolean flag = fs.delete(path);
+		fs.close();
+		return flag;
+	}
+
+	public void putFile() throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);
+		Path src = new Path("/home/hadoop/word.txt");
+		Path dst = new Path("/user/hadoop/data/");
+		fs.copyFromLocalFile(src, dst);
+		fs.close();
+	}
+
+	public void delFile() throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);
+
+		Path path = new Path("/user/hadoop/data/word.txt");
+		fs.delete(path);
+		fs.close();
+	}
+
+	public void readFile() throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);
+		Path path = new Path("/user/hadoop/data/write.txt");
+		if (fs.exists(path)) {
+			FSDataInputStream is = fs.open(path);
+			FileStatus status = fs.getFileStatus(path);
+			byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];
+			is.readFully(0, buffer);
+			is.close();
+			fs.close();
+			System.out.println(buffer.toString());
+		}
+	}
+
+	public static void writeFile() throws IOException {
+		Configuration conf = new Configuration();
+		FileSystem fs = FileSystem.get(conf);
+		Path path = new Path("/user/hadoop/data/write.txt");
+		FSDataOutputStream out = fs.create(path);
+		out.writeUTF("da jia hao,cai shi zhen de hao!");
+		fs.close();
+	}
+
+}

+ 62 - 0
src/me/yoqi/hadoop/test/WordCount.java

@@ -0,0 +1,62 @@
+package me.yoqi.hadoop.test;
+import java.io.IOException;
+import java.util.StringTokenizer;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapreduce.Job;
+import org.apache.hadoop.mapreduce.Mapper;
+import org.apache.hadoop.mapreduce.Reducer;
+import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
+import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
+
+public class WordCount {
+
+  public static class TokenizerMapper
+       extends Mapper<Object, Text, Text, IntWritable>{
+
+    private final static IntWritable one = new IntWritable(1);
+    private Text word = new Text();
+
+    public void map(Object key, Text value, Context context
+                    ) throws IOException, InterruptedException {
+      StringTokenizer itr = new StringTokenizer(value.toString());
+      while (itr.hasMoreTokens()) {
+        word.set(itr.nextToken());
+        context.write(word, one);
+      }
+    }
+  }
+
+  public static class IntSumReducer
+       extends Reducer<Text,IntWritable,Text,IntWritable> {
+    private IntWritable result = new IntWritable();
+
+    public void reduce(Text key, Iterable<IntWritable> values,
+                       Context context
+                       ) throws IOException, InterruptedException {
+      int sum = 0;
+      for (IntWritable val : values) {
+        sum += val.get();
+      }
+      result.set(sum);
+      context.write(key, result);
+    }
+  }
+
+  public static void main(String[] args) throws Exception {
+    Configuration conf = new Configuration();
+    Job job = Job.getInstance(conf, "word count");
+    job.setJarByClass(WordCount.class);
+    job.setMapperClass(TokenizerMapper.class);
+    job.setCombinerClass(IntSumReducer.class);
+    job.setReducerClass(IntSumReducer.class);
+    job.setOutputKeyClass(Text.class);
+    job.setOutputValueClass(IntWritable.class);
+    FileInputFormat.addInputPath(job, new Path(args[0]));
+    FileOutputFormat.setOutputPath(job, new Path(args[1]));
+    System.exit(job.waitForCompletion(true) ? 0 : 1);
+  }
+}