通过JavaAPI访问HBase

先开始创建表

create 'emp001','member_id','address','info'

放入数据

put 'emp001','Rain','id','31'
put 'emp001', 'Rain', 'info:birthday', '1990-05-01'
put 'emp001', 'Rain', 'info:industry', 'architect'
put 'emp001', 'Rain', 'info:city', 'ShenZhen'
put 'emp001', 'Rain', 'info:country', 'China'
get 'emp001','Rain','info'
scan 'emp001',{COLUMNS=> 'info:birthday'}

导入依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.1</version>
    </dependency>
  </dependencies>
  <build>
  <plugins>
  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
  <execution>
  <phase>package</phase>
  <goals>
  <goal>shade</goal>
  </goals>
  <configuration>
  <transformers>
  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  <mainClass>com.www</mainClass>
  </transformer>
  </transformers>
  </configuration>
  </execution>
  </executions>
  </plugin>
  </plugins>
  </build>

创建表

package com.dd12345.aaa;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class CreateTable {
	public static void main(String []args) throws Exception {
		Configuration conf=new Configuration();
		conf.set("hbase.rootdir", "hdfs://locslhost:8020/hbase");
		HBaseAdmin client=new HBaseAdmin(conf);
		HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("student"));
		HColumnDescriptor h1=new HColumnDescriptor("info");
		HColumnDescriptor h2=new HColumnDescriptor("grade");
		htd.addFamily(h1);
		htd.addFamily(h2);
		client.createTable(htd);
		client.close();
	}
	
}

插入一条数据

package com.dd12345.aaa;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class insertOne {
	
		public static void main(String []args) throws Exception {
			Configuration conf=new Configuration();
			conf.set("hbase.rootdir", "hdfs://locslhost:8020/hbase");
			HTable table=new HTable(conf,"student");
			Put put =new Put(Bytes.toBytes("stu001"));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Tom"));
			table.put(put);
			table.close();
		}
		
	}

查找数据

package com.dd12345.aaa;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

public class findUp {
	public static void main(String []args) throws Exception {
		Configuration conf=new Configuration();
		conf.set("hbase.rootdir", "hdfs://locslhost:8020/hbase");
		HTable table=new HTable(conf,"student");
		Get get =new Get(Bytes.toBytes("stu001"));
		Result record=table.get(get);
		String name=Bytes.toString(record.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
		System.out.println(name);
		table.close();
	}

}

扫描数据

package com.dd12345.aaa;

import org.apache.hadoop.conf.Configuration;

public class Scann {
	public static void main(String []args) throws Exception {
		Configuration conf=new Configuration();
		conf.set("hbase.rootdir", "hdfs://locslhost:8020/hbase");
		HTable table=new HTable(conf,"student");
		Scan scanner=new Scan();
		ResultScanner rs=table.getScanner(scanner);
		for(Result r:rs) {
			String name=Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
		
			String age=Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
			System.out.println(name+"   "+age);
		}
		
		table.close();
	}
	
}

删除表

package com.dd12345.aaa;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class delete {
	public static void main(String []args) throws Exception {
		Configuration conf=new Configuration();
		conf.set("hbase.rootdir", "hdfs://locslhost:8020/hbase");
		HBaseAdmin client=new HBaseAdmin(conf);
		client.disableTable("student");
		client.deleteTable("student");;
		client.close();
	}
	

}

成功截图
在这里插入图片描述
有问题私聊我!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要变胖哇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值