java调用hbase接口方法

使用maven+idea进行管理

maven

<dependency>
  <groupId>log4jgroupId>
  <artifactId>log4jartifactId>
  <version>1.2.17version>
dependency>

<dependency>
  <groupId>org.apache.hbasegroupId>
  <artifactId>hbase-shaded-clientartifactId>
  <version>1.2.3version>
dependency>

代码如下:
package hbase;
import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;


/**
 * Created by Administrator on 2016/9/19.
 */
public class HbaseDemo {
        static   Connection connection =null;
        static{
                Configuration hbaseConfiguration = HBaseConfiguration.create();
                hbaseConfiguration.set("hbase.zookeeper.property.clientPort", "2181");
                hbaseConfiguration.set("hbase.zookeeper.quorum", "192.168.199.6,192.168.199.7,192.168.199.8");
                try {
                        connection = ConnectionFactory.createConnection(hbaseConfiguration);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        /**
         * 创建表
         * @param tablename 表名
         * @param columnFamily 列族
         * @throws IOException
         * @throws ZooKeeperConnectionException
         * @throws MasterNotRunningException
         */
        public static void CreateTable(String tablename,String columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
                Admin admin = connection.getAdmin();
                TableName tableName =TableName.valueOf(tablename);
                HTableDescriptor tableDesc = new HTableDescriptor(tableName);
                tableDesc.addFamily(new HColumnDescriptor(columnFamily));
                admin.createTable(tableDesc);
                System.out.println(tablename+"表已经成功创建!");
                
        }

        /**
         * 向表中插入一条新数据
         * @param tableName 表名
         * @param row 行键key
         * @param columnFamily 列族
         * @param column 列名
         * @param data 要插入的数据
         * @throws IOException
         */
        public static void PutData(String tableName,String row,String columnFamily,String column,String data) throws IOException{
                
                TableName tableName1 = TableName.valueOf(tableName);
                Table table = connection.getTable(tableName1);
                Put put = new Put(Bytes.toBytes(row));
                put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
                table.put(put);
                System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");
        }

        /**
         * 获取指定行的所有数据
         * @param tableName 表名
         * @param row 行键key
         * @param columnFamily 列族
         * @param column 列名
         * @throws IOException
         */
        public static void GetData(String tableName,String row,String columnFamily,String column) throws IOException{
                TableName tableName1 = TableName.valueOf(tableName);
                Table table = connection.getTable(tableName1);
                Get get = new Get(Bytes.toBytes(row));
                Result result = table.get(get);
                byte[] rb = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
                String value = new String(rb,"UTF-8");
                System.out.println(value);
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                try {
                     // HbaseDemo.CreateTable("user3", "info1");
                        //HbaseDemo.PutData("userinfo", "row2", "baseinfo", "vio2", "驾驶车辆违法信息2:");
                       HbaseDemo.GetData("user", "row2","info1","name");
                } catch (Exception e) {
                        e.printStackTrace();
                }

        }
}

注意:
1.版本一定对应
2.查询方法,需要先创建表



你可能感兴趣的:(hadoop)