Hbase学习之三Hbase Java API

Posted cac2020

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbase学习之三Hbase Java API相关的知识,希望对你有一定的参考价值。

环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  hadoop-2.6.5
  hbase-0.98.12.1-hadoop2

一、Protocol 

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。

1、安装protobuf

[root@PCS102 src]# tar -zxvf protobuf-2.5.0.tar.gz -C /usr/local
[root@PCS102 src]# ./configure && make && make check && make install
[root@PCS102 protobuf-2.5.0]# whereis protoc
protoc: /usr/local/bin/protoc
[root@PCS102 protobuf-2.5.0]# /usr/local/bin/protoc --help
Usage: /usr/local/bin/protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
-IPATH, --proto_path=PATH Specify the directory in which to search for
imports. May be specified multiple times;
directories will be searched in order. If not
given, the current working directory is used.
--version Show version info and exit.
-h, --help Show this text and exit.
--encode=MESSAGE_TYPE Read a text-format message of the given type
from standard input and write it in binary
to standard output. The message type must
be defined in PROTO_FILES or their imports.
--decode=MESSAGE_TYPE Read a binary message of the given type from
standard input and write it in text format
to standard output. The message type must
be defined in PROTO_FILES or their imports.
--decode_raw Read an arbitrary protocol message from
standard input and write the raw tag/value
pairs in text format to standard output. No
PROTO_FILES should be given when using this
flag.
-oFILE, Writes a FileDescriptorSet (a protocol buffer,
--descriptor_set_out=FILE defined in descriptor.proto) containing all of
the input files to FILE.
--include_imports When using --descriptor_set_out, also include
all dependencies of the input files in the
set, so that the set is self-contained.
--include_source_info When using --descriptor_set_out, do not strip
SourceCodeInfo from the FileDescriptorProto.
This results in vastly larger descriptors that
include information about the original
location of each decl in the source file as
well as surrounding comments.
--error_format=FORMAT Set the format in which to print errors.
FORMAT may be \'gcc\' (the default) or \'msvs\'
(Microsoft Visual Studio format).
--plugin=EXECUTABLE Specifies a plugin executable to use.
Normally, protoc searches the PATH for
plugins, but you may specify additional
executables not in the path using this flag.
Additionally, EXECUTABLE may be of the form
NAME=PATH, in which case the given plugin name
is mapped to the given executable even if
the executable\'s own name differs.
--cpp_out=OUT_DIR Generate C++ header and source.
--java_out=OUT_DIR Generate Java source file.
--python_out=OUT_DIR Generate Python source file.

 

2、使用
(1)、编写 .proto 文件
文件命名规则:小写类名.proto,注意类名不要和内部的消息名相同
举例:phone.proto

package com.wjy.hbase;
message PhoneDetail
{
  required string dnum=1;
  required string length=2;
  required string type=3;
  required string date=4;
}

message dayPhoneDetail
{
  repeated PhoneDetail dayPhoneDetail=1;
}

 

注意:里面等号后面的值不要相同 否则不成功

(2)、编译.proto 文件
命令语法:
protoc -I=源文件目录 --java_out=生成文件目录 .proto文件
示例:

[root@PCS102 ~]# /usr/local/bin/protoc -I=/root --java_out=/root /root/phone.proto
[root@PCS102 hbase]# pwd
/root/com/wjy/hbase
[root@PCS102 hbase]# ls
Phone.java
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: phone.proto

package com.wjy.hbase;

public final class Phone {
  private Phone() {}
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
  }
  public interface PhoneDetailOrBuilder
      extends com.google.protobuf.MessageOrBuilder {

    // required string dnum = 1;
    /**
     * <code>required string dnum = 1;</code>
     */
    boolean hasDnum();
    /**
     * <code>required string dnum = 1;</code>
     */
    java.lang.String getDnum();
    /**
     * <code>required string dnum = 1;</code>
     */
    com.google.protobuf.ByteString
        getDnumBytes();

    // required string length = 2;
    /**
     * <code>required string length = 2;</code>
     */
    boolean hasLength();
    /**
     * <code>required string length = 2;</code>
     */
    java.lang.String getLength();
    /**
     * <code>required string length = 2;</code>
     */
    com.google.protobuf.ByteString
        getLengthBytes();

    // required string type = 3;
    /**
     * <code>required string type = 3;</code>
     */
    boolean hasType();
    /**
     * <code>required string type = 3;</code>
     */
    java.lang.String getType();
    /**
     * <code>required string type = 3;</code>
     */
    com.google.protobuf.ByteString
        getTypeBytes();

    // required string date = 4;
    /**
     * <code>required string date = 4;</code>
     */
    boolean hasDate();
    /**
     * <code>required string date = 4;</code>
     */
    java.lang.String getDate();
    /**
     * <code>required string date = 4;</code>
     */
    com.google.protobuf.ByteString
        getDateBytes();
  }
  /**
   * Protobuf type {@code com.wjy.hbase.PhoneDetail}
   */
  public static final class PhoneDetail extends
      com.google.protobuf.GeneratedMessage
      implements PhoneDetailOrBuilder {
    // Use PhoneDetail.newBuilder() to construct.
    private PhoneDetail(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
      super(builder);
      this.unknownFields = builder.getUnknownFields();
    }
    private PhoneDetail(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }

    private static final PhoneDetail defaultInstance;
    public static PhoneDetail getDefaultInstance() {
      return defaultInstance;
    }

    public PhoneDetail getDefaultInstanceForType() {
      return defaultInstance;
    }

    private final com.google.protobuf.UnknownFieldSet unknownFields;
    @java.lang.Override
    public final com.google.protobuf.UnknownFieldSet
        getUnknownFields() {
      return this.unknownFields;
    }
    private PhoneDetail(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      initFields();
      int mutable_bitField0_ = 0;
      com.google.protobuf.UnknownFieldSet.Builder unknownFields =
          com.google.protobuf.UnknownFieldSet.newBuilder();
      try {
        boolean done = false;
        while (!done) {
          int tag = input.readTag();
          switch (tag) {
            case 0:
              done = true;
              break;
            default: {
              if (!parseUnknownField(input, unknownFields,
                                     extensionRegistry, tag)) {
                done = true;
              }
              break;
            }
            case 10: {
              bitField0_ |= 0x00000001;
              dnum_ = input.readBytes();
              break;
            }
            case 18: {
              bitField0_ |= 0x00000002;
              length_ = input.readBytes();
              break;
            }
            case 26: {
              bitField0_ |= 0x00000004;
              type_ = input.readBytes();
              break;
            }
            case 34: {
              bitField0_ |= 0x00000008;
              date_ = input.readBytes();
              break;
            }
          }
        }
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw e.setUnfinishedMessage(this);
      } catch (java.io.IOException e) {
        throw new com.google.protobuf.InvalidProtocolBufferException(
            e.getMessage()).setUnfinishedMessage(this);
      } finally {
        this.unknownFields = unknownFields.build();
        makeExtensionsImmutable();
      }
    }
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return com.wjy.hbase.Phone.internal_static_com_wjy_hbase_PhoneDetail_descriptor;
    }

    protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return com.wjy.hbase.Phone.internal_static_com_wjy_hbase_PhoneDetail_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              com.wjy.hbase.Phone.PhoneDetail.class, com.wjy.hbase.Phone.PhoneDetail.Builder.class);
    }

    public static com.google.protobuf.Parser<PhoneDetail> PARSER =
        new com.google.protobuf.AbstractParser<PhoneDetail>() {
      public PhoneDetail parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return new PhoneDetail(input, extensionRegistry);
      }
    };

    @java.lang.Override
    public com.google.protobuf.Parser<PhoneDetail> getParserForType() {
      return PARSER;
    }

    private int bitField0_;
    // required string dnum = 1;
    public static final int DNUM_FIELD_NUMBER = 1;
    private java.lang.Object dnum_;
    /**
     * <code>required string dnum = 1;</code>
     */
    public boolean hasDnum() {
      return ((bitField0_ & 0x00000001) == 0x00000001);
    }
    /**
     * <code>required string dnum = 1;</code>
     */
    public java.lang.String getDnum() {
      java.lang.Object ref = dnum_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          dnum_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string dnum = 1;</code>
     */
    public com.google.protobuf.ByteString
        getDnumBytes() {
      java.lang.Object ref = dnum_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        dnum_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    // required string length = 2;
    public static final int LENGTH_FIELD_NUMBER = 2;
    private java.lang.Object length_;
    /**
     * <code>required string length = 2;</code>
     */
    public boolean hasLength() {
      return ((bitField0_ & 0x00000002) == 0x00000002);
    }
    /**
     * <code>required string length = 2;</code>
     */
    public java.lang.String getLength() {
      java.lang.Object ref = length_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          length_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string length = 2;</code>
     */
    public com.google.protobuf.ByteString
        getLengthBytes() {
      java.lang.Object ref = length_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        length_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    // required string type = 3;
    public static final int TYPE_FIELD_NUMBER = 3;
    private java.lang.Object type_;
    /**
     * <code>required string type = 3;</code>
     */
    public boolean hasType() {
      return ((bitField0_ & 0x00000004) == 0x00000004);
    }
    /**
     * <code>required string type = 3;</code>
     */
    public java.lang.String getType() {
      java.lang.Object ref = type_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          type_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string type = 3;</code>
     */
    public com.google.protobuf.ByteString
        getTypeBytes() {
      java.lang.Object ref = type_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        type_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    // required string date = 4;
    public static final int DATE_FIELD_NUMBER = 4;
    private java.lang.Object date_;
    /**
     * <code>required string date = 4;</code>
     */
    public boolean hasDate() {
      return ((bitField0_ & 0x00000008) == 0x00000008);
    }
    /**
     * <code>required string date = 4;</code>
     */
    public java.lang.String getDate() {
      java.lang.Object ref = date_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          date_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string date = 4;</code>
     */
    public com.google.protobuf.ByteString
        getDateBytes() {
      java.lang.Object ref = date_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        date_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    private void initFields() {
      dnum_ = "";
      length_ = "";
      type_ = "";
      date_ = "";
    }
    private byte memoizedIsInitialized = -1;
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized != -1) return isInitialized == 1;

      if (!hasDnum()) {
        memoizedIsInitialized = 0;
        return false;
      }
      if (!hasLength()) {
        memoizedIsInitialized = 0;
        return false;
      }
      if (!hasType()) {
        memoizedIsInitialized = 0;
        return false;
      }
      if (!hasDate()) {
        memoizedIsInitialized = 0;
        return false;
      }
      memoizedIsInitialized = 1;
      return true;
    }

    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      getSerializedSize();
      if (((bitField0_ & 0x00000001) == 0x00000001)) {
        output.writeBytes(1, getDnumBytes());
      }
      if (((bitField0_ & 0x00000002) == 0x00000002)) {
        output.writeBytes(2, getLengthBytes());
      }
      if (((bitField0_ & 0x00000004) == 0x00000004)) {
        output.writeBytes(3, getTypeBytes());
      }
      if (((bitField0_ & 0x00000008) == 0x00000008)) {
        output.writeBytes(4, getDateBytes());
      }
      getUnknownFields().writeTo(output);
    }

    private int memoizedSerializedSize = -1;
    public int getSerializedSize() {
      int size = memoizedSerializedSize;
      if (size != -1) return size;

      size = 0;
      if (((bitField0_ & 0x00000001) == 0x00000001)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(1, getDnumBytes());
      }
      if (((bitField0_ & 0x00000002) == 0x00000002)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(2, getLengthBytes());
      }
      if (((bitField0_ & 0x00000004) == 0x00000004)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(3, getTypeBytes());
      }
      if (((bitField0_ & 0x00000008) == 0x00000008)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(4, getDateBytes());
      }
      size += getUnknownFields().getSerializedSize();
      memoizedSerializedSize = size;
      return size;
    }

    private static final long serialVersionUID = 0L;
    @java.lang.Override
    protected java.lang.Object writeReplace()
        throws java.io.ObjectStreamException {
      return super.writeReplace();
    }

    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input, extensionRegistry);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.wjy.hbase.Phone.PhoneDetail parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }

    public static Builder newBuilder() { return Builder.create(); }
    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder(com.wjy.hbase.Phone.PhoneDetail prototype) {
      return newBuilder().mergeFrom(prototype);
    }
    public Builder toBuilder() { return newBuilder(this); }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessage.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code com.wjy.hbase.PhoneDetail}
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessage.Builder<Builder>
       implements com.wjy.hbase.Phone.PhoneDetailOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return com.wjy.hbase.Phone.internal_static_com_wjy_hbase_PhoneDetail_descriptor;
      }

      protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return com.wjy.hbase.Phone.internal_static_com_wjy_hbase_PhoneDetail_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                com.wjy.hbase.Phone.PhoneDetail.class, com.wjy.hbase.Phone.PhoneDetail.Builder.class);
      }

      // Construct using com.wjy.hbase.Phone.PhoneDetail.newBuilder()
      private Builder() {
        maybeForceBuilderInitialization();
      }

      private Builder(
          com.google.protobuf.GeneratedMessage.BuilderParent parent) {
        super(parent);
        maybeForceBuilderInitialization();
      }
      private void maybeForceBuilderInitialization() {
        if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
        }
      }
      private static Builder create() {
        return new Builder();
      }

      public Builder clear() {
        super.clear();
        dnum_ = "";
        bitField0_ = (bitField0_ & ~0x00000001);
        length_ = "";
        bitField0_ = (bitField0_ & ~0x00000002);
        type_ = "";
        bitField0_ = (bitField0_ & ~0x00000004);
        date_ = "";
        bitField0_ = (bitField0_ & ~0x00000008);
        return this;
      }

      public Builder clone() {
        return create().mergeFrom(buildPartial());
      }

      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return com.wjy.hbase.Phone.internal_static_com_wjy_hbase_PhoneDetail_descriptor;
      }

      public com.wjy.hbase.Phone.PhoneDetail getDefaultInstanceForType() {
        return com.wjy.hbase.Phone.PhoneDetail.getDefaultInstance();
      }

      public com.wjy.hbase.Phone.PhoneDetail build() {
        com.wjy.hbase.Phone.PhoneDetail result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      public com.wjy.hbase.Phone.PhoneDetail buildPartial() {
        com.wjy.hbase.Phone.PhoneDetail result = new com.wjy.hbase.Phone.PhoneDetail(this);
        int from_bitField0_ = bitField0_;
        int to_bitField0_ = 0;
        if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
          to_bitField0_ |= 0x00000001;
        }
        result.dnum_ = dnum_;
        if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
          to_bitField0_ |= 0x00000002;
        }
        result.length_ = length_;
        if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
          to_bitField0_ |= 0x00000004;
        }
        result.type_ = type_;
        if (((from_bitField0_ & 0x00000008) == 0x00000008)) {
          to_bitField0_ |= 0x00000008;
        }
        result.date_ = date_;
        result.bitField0_ = to_bitField0_;
        onBuilt();
        return result;
      }

      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof com.wjy.hbase.Phone.PhoneDetail) {
          return mergeFrom((com.wjy.hbase.Phone.PhoneDetail)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(com.wjy.hbase.Phone.PhoneDetail other) {
        if (other == com.wjy.hbase.Phone.PhoneDetail.getDefaultInstance()) return this;
        if (other.hasDnum()) {
          bitField0_ |= 0x00000001;
       

以上是关于Hbase学习之三Hbase Java API的主要内容,如果未能解决你的问题,请参考以下文章

大数据学习之HBase

HBase学习之结构设计

大数据开发学习之hbase命令的简单操作

HBase Java 操作 HBase 教程

HBASE基础使用Java API实现DDL与DML

如何使用Java API操作Hbase