重温HBaes部署与java客户端访问(二)

2015-07-24 06:57:36 · 作者: · 浏览: 8
ic void deleteByRow(String tableName,String rowkey[])throws Exception{ HTable h=new HTable(conf, tableName); List list=new ArrayList(); for(String k:rowkey){ Delete d=new Delete(Bytes.toBytes(k)); list.add(d); } h.delete(list);//删除 h.close();//释放资源 } /** * 得到一条数据 * * @param tableName 表名 * @param rowkey 行号 * ***/ public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{ HTable h=new HTable(conf, tableName); Get g=new Get(Bytes.toBytes(rowkey)); Result r=h.get(g); for(KeyValue k:r.raw()){ System.out.println("行号: "+Bytes.toStringBinary(k.getRow())); System.out.println("时间戳: "+k.getTimestamp()); System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily())); System.out.println("列: "+Bytes.toStringBinary(k.getQualifier())); //if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){ // System.out.println("值: "+Bytes.toInt(k.getValue())); //}else{ String ss= Bytes.toString(k.getValue()); System.out.println("值: "+ss); //} } h.close(); } /** * 扫描所有数据或特定数据 * @param tableName * **/ public void showAll(String tableName)throws Exception{ HTable h=new HTable(conf, tableName); Scan scan=new Scan(); //扫描特定区间 //Scan scan=new Scan(Bytes.toBytes("开始行号"),Bytes.toBytes("结束行号")); ResultScanner scanner=h.getScanner(scan); for(Result r:scanner){ System.out.println("=================================="); for(KeyValue k:r.raw()){ System.out.println("行号: "+Bytes.toStringBinary(k.getRow())); System.out.println("时间戳: "+k.getTimestamp()); System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily())); System.out.println("列: "+Bytes.toStringBinary(k.getQualifier())); //if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){ // System.out.println("值: "+Bytes.toInt(k.getValue())); //}else{ String ss= Bytes.toString(k.getValue()); System.out.println("值: "+ss); //} } } h.close(); } }

工程使用maven,其pom.xml如下:



    4.0.0

    com.codesolid
    HelloJUnit
    1.0

    
        
            junit
            junit
            4.11
            test
        
        
            log4j
            log4j
            1.2.17
            compile
        
        
            org.apache.hadoop
            hadoop-core
            1.0.4
        
        
            org.apache.hbase
            hbase
            0.94.5
        
    



三、遇到的问题

This server is in the failed servers list: localhost/127.0.0.1:60718

出现该问题的原因是没有在服务端配置主机名和真实ip的映射,因此需要再/etc/hosts中加上10.1.102.104 ccf04(ccf04为服务器的主机名)

同时,需要在你跑程序的机器上添加ccf04的映射,我的是在windows上跑的java程序,需要修改windows的host文件,最后一行加入10.1.102.104 ccf04即可