JAVA 访问HDFS

2 篇文章 0 订阅
2 篇文章 0 订阅
package org.apache.spark.examples;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * realize the wordcount by multi thread
 * 
 * @author sunlggggg
 *
 */
public class testNoSpark implements Runnable {

    private static HashMap<String, Integer> hashMap;
    BufferedReader bis;
    private static  BufferedWriter bw;

    public void init() throws IOException {
        hashMap = new HashMap<>();
        String path = "hdfs://master:8888/input/TEST2";
        String confFile = "/root/hadoop/etc/hadoop/core-site.xml";
        Configuration conf = new Configuration();
        FileInputStream fis = new FileInputStream(confFile);
        conf.addResource(fis);
        FileSystem fileSystem = FileSystem.get(conf);
        FSDataInputStream fs = fileSystem.open(new Path(path));
        bis = new BufferedReader(new InputStreamReader(fs, "GBK"));
        FileWriter fos = new FileWriter("/root/out");
        bw = new BufferedWriter(fos);
    }

    public void close() {
    }

    @Override
    public void run() {
        String line = null;
        String[] words = null;
        try {
            while ((line = bis.readLine()) != null) {
                words = line.split(" ");
                for (int i = 0; i < words.length; i++) {
                    Integer count = hashMap.get(words[i]);
                    hashMap.put(words[i], count == null ? 1 : count + 1);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void print() throws IOException {
        System.out.println("size" + hashMap.size());
        bw.write("size" + hashMap.size() +"\n");
        Iterator<Entry<String, Integer>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, Integer> en = iterator.next();
            System.out.println(en.getKey() + "------>" + en.getValue());
            bw.write(en.getKey() + "------>" + en.getValue() +"\n");
        }
    }

    public static void main(String[] args) throws IOException {
        long time1 = new Date().getTime();
        testNoSpark testNoSpark = new testNoSpark();
        testNoSpark.init();
        Thread t1 = new Thread(testNoSpark);
        Thread t2 = new Thread(testNoSpark);
        Thread t3 = new Thread(testNoSpark);
        t1.start();
        t2.start();
        t3.start();
        try {
            t1.join();
            t2.join();
            t3.join();
            long time2 = new Date().getTime();
            testNoSpark.bw.write(time2+"\n");
            System.out.println((time2 - time1)/1000.0);
            testNoSpark.print();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值