Java API 操作HBase Shell

HBase Shell API 操作

创建工程

本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse。

然后创建Java项目名字叫hbase-test

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

配置运行环境

在src下创建HBaseDemo类
在这里插入图片描述
然后编写init方法和close方法,一个创建与HBASE的连接,一个关闭连接。

/**
	 * 创建连接返回admin
	 */
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "file:///usr/local/hbase/hbase-tmp");
		// configuration.set("hbase.zookeeper.quorum", "hadoop02");
		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 连接关闭
	 */
	public static void close() {
		try {
			if (admin != null) {
				admin.close();
			}
			if (null != connection) {
				connection.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

建表操作

	/**
	 * 创建表方法
	 * 
	 * @param myTableName
	 * @param colFamily
	 * @throws IOException
	 */
	public static void createTable(String myTableName, String[] colFamily) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if (admin.tableExists(tableName)) {
			System.out.println("talbe is exists!");
		} else {
			TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
			for (String str : colFamily) {
				ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
				tableDescriptor.setColumnFamily(family);
			}
			admin.createTable(tableDescriptor.build());
		}
	}

测试:创建student表,列族有score

	public static void main(String[] args) throws IOException {
		init();
		System.out.print("==================分割线===================");
		//创建student表
		  createTable("student",new String[]{"score"});
		 
		close();
	}

查看执行结果

在这里插入图片描述

查看现有表的名称

	 /**
     * 查看已有表
     * @throws IOException
     */
    public static void listTables() throws IOException {
        init();
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for(HTableDescriptor hTableDescriptor :hTableDescriptors){
            System.out.println(hTableDescriptor.getNameAsString());
        }
        close();
     }

测试:

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割线===================");
		listTables();
		System.out.println("==================分割线===================");
		close();
	}

删除表操作

代码:

	/**
     * 删除指定表
     * @param tableName 表名
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws IOException {
        init();
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        close();
}

测试:

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割线===================");
		deleteTable("student");
		System.out.println("==================分割线===================");
		close();
	}

结果
在这里插入图片描述

删除指定列操作

代码

    /**
     * 删除指定列数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param colFamily 列族名
     * @param col 列名
     * @throws IOException
     */
    public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        //删除指定列族的所有数据
        //delete.addFamily(colFamily.getBytes());
        //删除指定列的数据
        delete.addColumn(colFamily.getBytes(), col.getBytes());
 
        table.delete(delete);
        table.close();
        close();
}

测试

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割线===================");
		deleteRow("student", "zhangsan", "score", "Math");
		System.out.println("==================分割线===================");
		close();
	}

结果 zhangsan行,score列族中的Math列被删除

添加数据

	/**
	 * 向指定表中插入数据
	 * 
	 * @param tableName
	 * @param rowKey
	 * @param colFamily
	 * @param col
	 * @param val
	 * @throws IOException
	 */
	public static void insertData(String tableName, String rowKey, String colFamily, String col, String val)
			throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(rowKey.getBytes());
		put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
		table.put(put);
		table.close();
	}

向student表中添加三条数据,分别是zhangsan的英语成绩,数学成绩和计算机成绩。

	public static void main(String[] args) throws IOException {
		init();
		System.out.print("==================分割线===================");
		 
		  //插入三条数据
		  insertData("student","zhangsan","score","English","69");
		  insertData("student","zhangsan","score","Math","86");
		  insertData("student","zhangsan","score","Computer","77");
		 
		close();
	}

测试结果
在这里插入图片描述

查看数据

/**
	 * 获取指定表中ceil数据
	 * 
	 * @param tableName
	 * @param rowKey
	 * @param colFamily
	 * @param col
	 * @throws IOException
	 */
	public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(rowKey.getBytes());
		get.addColumn(colFamily.getBytes(), col.getBytes());
		Result result = table.get(get);
		System.out.println(tableName+"表,"+colFamily+"列族,"+col+"的值是:"+new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
		table.close();
	}

查看张三英语的 成绩

	public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割线===================");
		
		  getData("student","zhangsan", "score","English");
		 
		close();
	}

控制台输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43672652/article/details/107029794