2015-08-21 42 views
0

我想通过API将数据从平面文件加载到Hbase。但我得到以下错误 ======== ================================================ java在有机.lang.NumberFormatException.forInputString在java.lang.Integer.parseInt(Integer.java:492) (NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:527) 。 apache.hadoop.hbase.HServerAddress。(HServerAddress.java:63) at org.apache.hadoop.hbase.MasterAddressTracker.getMasterAddress(MasterAddressTracker.java:63) at org.apache.hadoop.hbase.client.HConnectionManager $ HConnectionImplementati on.getMaster(HConnectionManager.java:354) 在org.apache.hadoop.hbase.client.HBaseAdmin。(HBaseAdmin.java:94) 在Hbase.readFromFile.main(readFromFile.java:16)Hbase:需要合适的jar文件cloudera-quickstart-vm-5.4.2-0

代码:

 package Hbase; 

    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileReader; 
    import java.io.IOException; 
    import java.util.StringTokenizer; 

    import org.apache.hadoop.conf.Configuration; 
    import org.apache.hadoop.hbase.HBaseConfiguration; 
    import org.apache.hadoop.hbase.HColumnDescriptor; 
    import org.apache.hadoop.hbase.HTableDescriptor; 
    import org.apache.hadoop.hbase.client.HBaseAdmin; 
    import org.apache.hadoop.hbase.client.HTable; 
    import org.apache.hadoop.hbase.client.Put; 
    import org.apache.hadoop.hbase.util.Bytes; 


    public class readFromFile { 
    public static void main(String[] args) throws IOException{ 
    if(args.length==1) 
     { 
     Configuration conf = HBaseConfiguration.create(new  Configuration()); 
     HBaseAdmin hba = new HBaseAdmin(conf); 
     if(!hba.tableExists(args[0])){ 
      HTableDescriptor ht = new HTableDescriptor(args[0]); 
      ht.addFamily(new HColumnDescriptor("sample")); 
      ht.addFamily(new HColumnDescriptor("region")); 
      ht.addFamily(new HColumnDescriptor("time")); 
      ht.addFamily(new HColumnDescriptor("product")); 
      ht.addFamily(new HColumnDescriptor("sale")); 
      ht.addFamily(new HColumnDescriptor("profit")); 
      hba.createTable(ht); 
      System.out.println("New Table Created"); 

      HTable table = new HTable(conf,args[0]); 

      File f = new File("/home/training/Desktop/data"); 
      BufferedReader br = new BufferedReader(new FileReader(f)); 
      String line = br.readLine(); 
      int i =1; 
      String rowname="row"; 
      while(line!=null && line.length()!=0){ 
       System.out.println("Ok till here"); 
       StringTokenizer tokens = new StringTokenizer(line,","); 
       rowname = "row"+i; 
       Put p = new Put(Bytes.toBytes(rowname)); 
                         p.add(Bytes.toBytes("sample"),Bytes.toBytes("sampleNo."), 
     Bytes.toBytes(Integer.parseInt(tokens.nextToken()))); 
       p.add(Bytes.toBytes("region"),Bytes.toBytes("country"), 
       Bytes.toBytes(tokens.nextToken())); 
       p.add(Bytes.toBytes("region"),Bytes.toBytes("state"), 
       Bytes.toBytes(tokens.nextToken())); 
       p.add(Bytes.toBytes("region"),Bytes.toBytes("city"), 
       Bytes.toBytes(tokens.nextToken())); 
       p.add(Bytes.toBytes("time"),Bytes.toBytes("year"), 
      Bytes.toBytes(Integer.parseInt(tokens.nextToken()))); 
       p.add(Bytes.toBytes("time"),Bytes.toBytes("month"), 
       Bytes.toBytes(tokens.nextToken())); 
                         p.add(Bytes.toBytes("product"),Bytes.toBytes("productNo."), 
      Bytes.toBytes(tokens.nextToken())); 
       p.add(Bytes.toBytes("sale"),Bytes.toBytes("quantity"), 
      Bytes.toBytes(Integer.parseInt(tokens.nextToken()))); 
       p.add(Bytes.toBytes("profit"),Bytes.toBytes("earnings"), 
       Bytes.toBytes(tokens.nextToken())); 
       i++; 
       table.put(p); 
       line = br.readLine(); 
      } 
       br.close(); 
       table.close(); 
      } 
     else 
      System.out.println("Table Already exists. 
      Please enter another table name"); 
    } 
    else 
     System.out.println("Please Enter the table 
     name through command line");  
    } 
    } 

请让我知道我们是否需要添加..我现在用的Cloudera Cloudera的,快速入门-VM-5.4.2-0

感谢任何合适的罐子, VJ

回答

0

如果你读了错误,它说Integer.parseInt方法提出了NumberFormatException。这意味着您试图将无效格式的字符串转换为整数。在代码中,你调用该方法在这条线:

Bytes.toBytes(Integer.parseInt(tokens.nextToken())));

,你需要通过tokens.nextToken()看你传递到这个方法的标记,并确保每一个可以转换为整数。

0

我认为问题出现在使用的cloudera jar版本中。请检查它,这应该工作。