2016-11-12 175 views
0

的Hadoop 2.7.3在我的Mac安裝在:如何將hadoop jar添加到classpath?

/usr/local/Cellar/hadoop/2.7.3 

我寫了一個演示用java讀取HDFS文件:

import java.io.*; 
import java.net.URI; 
import java.net.URISyntaxException; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FSDataInputStream; 
import org.apache.hadoop.fs.FSDataOutputStream; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IOUtils; 

public class HDFSTest{ 

public static void main(String[] args) throws IOException, URISyntaxException{ 

    String file= "hdfs://localhost:9000/hw1/customer.tbl"; 
    Configuration conf = new Configuration(); 
    FileSystem fs = FileSystem.get(URI.create(file), conf); 
    Path path = new Path(file); 
    FSDataInputStream in_stream = fs.open(path); 
    BufferedReader in = new BufferedReader(new  
    InputStreamReader(in_stream)); 
    String s; 
    while ((s=in.readLine())!=null) { 
     System.out.println(s); 
    } 
    in.close(); 
    fs.close(); 
    } 
} 

當我編譯java文件,錯誤如圖所示打擊:

hero:Documents yaopan$ javac HDFSTest.java 
HDFSTest.java:8: error: package org.apache.hadoop.conf does not exist 
import org.apache.hadoop.conf.Configuration; 
         ^
HDFSTest.java:10: error: package org.apache.hadoop.fs does not exist 
import org.apache.hadoop.fs.FSDataInputStream; 
        ^
HDFSTest.java:12: error: package org.apache.hadoop.fs does not exist 
import org.apache.hadoop.fs.FSDataOutputStream; 
        ^
HDFSTest.java:14: error: package org.apache.hadoop.fs does not exist 
import org.apache.hadoop.fs.FileSystem; 

我知道原因是找不到Hadoop的罐子,如何配置 ^

+0

您可能會在這裏找到答案:HTTP: //stackoverflow.com/questions/24390492/package-org-apache-hadoop-conf-does-not-exist-after-setting-classpath –

回答

0

只需添加hadoop的jar文件到類路徑:

我使用上/usr/local/Cellar/hbase/1.2.2自制安裝HBase的,

/usr/local/Cellar/hbase/1.2.2/libexec/lib下添加所有罐子到類路徑:

1.edit .bash_profile

sudo vim ~/.bash_profile 

2.加classpath中

#set hbase lib path 
export CLASSPATH=$CLASSPATH://usr/local/Cellar/hbase/1.2.2/libexec/lib/* 
  • 保存並退出

    WQ

  • 1

    在您的安裝(例如/usr/local/Cellar/hadoop/2.7.3)下找到名爲「hadoop-common-2.7.3.jar」的jar文件,並將其設置在classpath中或直接在命令行中與javac一起給出。

    javac -cp "/PATH/hadoop-common-2.7.3.jar" HDFSTest.java 
    

    (在適當的路徑替換PATH

    相關問題