How to connect to remote HBase in Java?

前端 未结 5 1310
独厮守ぢ
独厮守ぢ 2020-12-23 09:58

I have a standlone HBase server. This is my hbase-site.xml:


 
    hbase.rootdir
    

        
5条回答
  •  时光说笑
    2020-12-23 10:06

    As far as i know, If you want to connect to an remote hbase server the normal java client doesn't work,in which we just declare the configuration and try to connect to the remote hbase as mentioned in precious answers.

    I have tried this above stuff but never succeeded in it. Instead i used Thrift API for connecting to a remote server,

    This link is the best example of using Thrift API java client.It surely works.I am using the same. But before using it carefully go through the code and emit those items which you don't need. I am also giving the sample code for the same which successfully works.

    public class ThriftClient 
    {
    
        port = 9090;
        //Connection to hbase
        TTransport transport = new TSocket(hostname, port);
        TProtocol protocol = new TBinaryProtocol(transport, true, true);
        Hbase.Client client = new Hbase.Client(protocol);
    
        transport.open();
    
        int z=Link.length();
        byte[] tablename = bytes("YOUR TABLE NAME");
    
        // Create the demo table with two column families, entry: and unused:
        ArrayList columns = new ArrayList();
        ColumnDescriptor col = null;
        col = new ColumnDescriptor();
        col.name = ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME"));
        col.maxVersions = 10;
        columns.add(col);
    
        System.out.println("creating table: " + utf8(tablename));
        try 
        {
            client.createTable(ByteBuffer.wrap(tablename), columns);
        } 
        catch (AlreadyExists ae) 
        {
            System.out.println("WARN: " + ae.message);
        }
    
        Map dummyAttributes = null;
        boolean writeToWal = false;
        // Test UTF-8 handling
        byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
            (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1};
        byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
            (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83,
            (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3,
            (byte) 0x83, (byte) 0xAB};
    
    
        ArrayList mutations;
    
        // Run some operations on a bunch of rows
    
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(10);
        nf.setGroupingUsed(false);
        byte[] row=bytes("YOUR ROW NAME");
    
        mutations = new ArrayList();
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("YOUR_COLUMN_FAMILY_NAME:YOUR_COLUMN_NAME")), ByteBuffer.wrap(bytes("YOUR_ROW_VALUE")), writeToWal));
        client.mutateRow(ByteBuffer.wrap(tablename), ByteBuffer.wrap(row), mutations, dummyAttributes);
    
        transport.close();
    
        // Helper to translate byte[]'s to UTF8 strings
    private static String utf8(byte[] buf) {
        try {
            return decoder.decode(ByteBuffer.wrap(buf)).toString();
        } catch (CharacterCodingException e) {
            return "[INVALID UTF-8]";
        }
    }
    
    // Helper to translate strings to UTF8 bytes
    private static byte[] bytes(String s) {
        try {
            return s.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
    }
    

提交回复
热议问题