无法编译简单的hbase java程序

时间:2015-01-21 21:03:59

标签: java api classpath hbase

使用此命令编译以下程序: -

javac MyLittleHBaseClient.java -cp /usr/lib/hbase/hbase-client.jar:/usr/lib/hbase/hbase-common.jar:/etc/hbase/conf:/usr/lib/hadoop/hadoop-common.jar:/etc/hadoop/conf:/usr/lib/zookeeper/zookeeper.jar:/usr/lib/hadoop/lib/*.jar:/usr/lib/hadoop/lib/commons-configuration-1.6.jar:/usr/lib/hbase/hbase-server.jar:/usr/lib/hbase/hbase-hadoop-compat-0.98.6-cdh5.3.0.jar:/usr/lib/hbase/hbase-client-0.98.6-cdh5.3.0.jar     

代码:

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class MyLittleHBaseClient {
  public static void main(String[] args) throws IOException {
    Configuration config = HBaseConfiguration.create();
    config.addResource("/etc/hbase/conf/hbase-site.xml");
    Connection connection = ConnectionFactory.createConnection(config);
    try {
      Table table = connection.getTable(TableName.valueOf("myTable"));
      try {
        Put p = new Put(Bytes.toBytes("myLittleRow"));
        p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
        Bytes.toBytes("Some Value"));
        table.put(p);

        Get g = new Get(Bytes.toBytes("myLittleRow"));
        Result r = table.get(g);
        byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
          Bytes.toBytes("someQualifier"));

        String valueStr = Bytes.toString(value);
        System.out.println("GET: " + valueStr);

        Scan s = new Scan();
        s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
        ResultScanner scanner = table.getScanner(s);
        try {
           for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
             System.out.println("Found row: " + rr);
           }

           // The other approach is to use a foreach loop. Scanners are iterable!
           // for (Result rr : scanner) {
           //   System.out.println("Found row: " + rr);
           // }
         } finally {
           scanner.close();
         }
       } finally {
         if (table != null) table.close();
       }
     } finally {
       connection.close();
     }
  }
}

导致以下错误。我不明白为什么我因为hbase-client jar被添加到类路径而得到以下错误。

 MyLittleHBaseClient.java:6: error: cannot find symbol
  import org.apache.hadoop.hbase.client.Connection;
                                       ^
    symbol:   class Connection
    location: package org.apache.hadoop.hbase.client
  MyLittleHBaseClient.java:7: error: cannot find symbol
  import org.apache.hadoop.hbase.client.ConnectionFactory;
                                       ^
    symbol:   class ConnectionFactory
    location: package org.apache.hadoop.hbase.client
  MyLittleHBaseClient.java:9: error: cannot find symbol
  import org.apache.hadoop.hbase.client.Table;
                                       ^
    symbol:   class Table
    location: package org.apache.hadoop.hbase.client
  MyLittleHBaseClient.java:25: error: cannot find symbol
      Configuration config = HBaseConfiguration.create();
      ^
    symbol:   class Configuration
    location: class MyLittleHBaseClient
  MyLittleHBaseClient.java:38: error: cannot find symbol
      Connection connection = ConnectionFactory.createConnection(config);
      ^
    symbol:   class Connection
    location: class MyLittleHBaseClient
  MyLittleHBaseClient.java:38: error: cannot find symbol
      Connection connection = ConnectionFactory.createConnection(config);
                              ^
    symbol:   variable ConnectionFactory
    location: class MyLittleHBaseClient
  MyLittleHBaseClient.java:46: error: cannot find symbol
        Table table = connection.getTable(TableName.valueOf("myTable"));
        ^
    symbol:   class Table
    location: class MyLittleHBaseClient
  7 errors

1 个答案:

答案 0 :(得分:1)

尝试以下方法。这应该涵盖hbase和hadoop下的所有罐子

javac -cp $(hbase classpath):$(hadoop classpath) MyLittleHBaseClient.java