HBase的java代码开发(通过startRowKey和endRowKey进行扫描)

第一步:创建maven工程,导入jar包


    
        cloudera
        https://repository.cloudera.com/artifactory/cloudera-repos/
    


    
        org.apache.hadoop
        hadoop-client
        2.6.0-mr1-cdh5.14.0
    
    
        org.apache.hbase
        hbase-client
        1.2.0-cdh5.14.0
    
    
        org.apache.hbase
        hbase-server
        1.2.0-cdh5.14.0
    
    
        junit
        junit
        4.12
        test
    
    
        org.testng
        testng
        6.14.3
        test
 
    
    
        org.junit.jupiter
        junit-jupiter-api
        RELEASE
        compile
    


    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.0
            
                1.8
                1.8
                UTF-8
                
            
        
        
            org.apache.maven.plugins
            maven-shade-plugin
            2.2
            
                
                    package
                    
                        shade
                    
                    
                        
                            
                                *:*
                                
                                    META-INF/*.SF
                                    META-INF/*.DSA
                                    META-INF/*/RSA
                                
                            
                        
                    
                
            
        
    

 

第二步 :开发 javaAPI 操作 HBase 表数据  通过startRowKey和endRowKey进行扫描

//导的 jar包
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
 
 
public class HBaseAPI {
 
  //通过startRowKey和endRowKey进行扫描

    public static void scanrowkey() throws IOException {
Configuration conf=new Configuration();
        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);

        Table myuser = connection.getTable(TableName.valueOf("myuser"));

        Scan scan =new Scan();
        scan.setStartRow("0002".getBytes());
        scan.setStopRow("0006".getBytes());

        ResultScanner scanner = myuser.getScanner(scan);
        for (Result result : scanner) {
            System.out.println("rowkey   "+Bytes.toString(result.getRow()));
/*
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"id".getBytes())));
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(),"name".getBytes())));
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"age".getBytes())));
*/

            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id" )|| Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
                    System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
                }else {
                    System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
                }
            }


        }

    }
  public static void main(String[] args) throws  IOException{
        scanrowkey();
    }
}

HBase的java代码开发(通过startRowKey和endRowKey进行扫描)_第1张图片

你可能感兴趣的:(大数据,#,HBase)