欢迎投稿

今日深度:

HBase的JAVA_API,

HBase的JAVA_API,


HBase的JAVA_API

1、先加载包

<?xml version="1.0"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>

<groupId>hbase_projecct</groupId>

<artifactId>hbase_projecct</artifactId>

<version>0.0.1-SNAPSHOT</version>


<dependencies>


<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-client</artifactId>

<version>2.5.0</version>

</dependency>


<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-exec</artifactId>

<version>0.13.1</version>

</dependency>


<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-jdbc</artifactId>

<version>0.13.1</version>

</dependency>


<dependency>

<groupId>org.apache.hbase</groupId>

<artifactId>hbase-server</artifactId>

<version>0.98.6-hadoop2</version>

</dependency>


<dependency>

<groupId>org.apache.hbase</groupId>

<artifactId>hbase-client</artifactId>

<version>0.98.6-hadoop2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->



<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.9</version>

<scope>test</scope>

</dependency>

</dependencies>


<build>

<sourceDirectory>src</sourceDirectory>


<plugins>


<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.7.0</version>


<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

</project>

2、测试是否存在表

package org.hbase.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HBaseDemo {

	public static Configuration conf;
	static {
		
		conf=HBaseConfiguration.create();		
		conf.set("hbase.rootdir", "hdfs://hh:8020/hbase");
		conf.set("hbase.zookeeper.quorum", "hh");
	}
	
	public static boolean isExits(String tableName) throws IOException {
		//老API 兼容性好
		HBaseAdmin admin=new HBaseAdmin(conf);
		
		return admin.tableExists(TableName.valueOf("wmc:wmc_t"));
		
	}
	
	public static void main(String[] args) throws IOException {
		
		System.out.println(isExits("wmc:wmc_t"));
	}
	
}

小技巧

当你把hadoop中hdfs-site.xml、core-site.xml还有hbase中的hbase-site.xml一起导出来加载到你API的这个包下,你直接创建你的configuration不用set设置你的连接,就可以用。

3、创建表

public static void createTable(String tableName,String...columnFamily) throws IOException, IOException, IOException {
		
		HBaseAdmin admin=new HBaseAdmin(conf);
		
		HTableDescriptor htd=new HTableDescriptor(TableName.valueOf(tableName));
		
		for(String cf:columnFamily) {
		htd.addFamily(new HColumnDescriptor(cf));
		
		}
		admin.createTable(htd);
	}
	  public static void main(String[] args) throws IOException {
		
		createTable("wangmengchen", "info1","info2");
	}	

4、删除表

PS: 如果表的状态是不可用的,这样表才会删除,不然会报异常。

//HBase表的删除
	public static void deleteTable(String tableName) throws IOException{
		
		HBaseAdmin admin=new HBaseAdmin(conf);
		if(isExits(tableName)) {
		admin.disableTable(TableName.valueOf(tableName));
		admin.deleteTable(TableName.valueOf(tableName));
		System.out.println("表已删除");
		}else {
			System.out.println("表不存在");
		}
	}
		
	public static void main(String[] args) throws IOException {
		deleteTable("wangmengchen");
	}

5、数据的添加

//添加数据
	public static void addRow(String tableName,String rowKey,String column,String value) throws IOException {
		
		HTable table=new HTable(conf, TableName.valueOf(tableName));
		
		Put put=new Put(Bytes.toBytes(rowKey));
		
		//获取列簇
		HColumnDescriptor[] famlis=table.getTableDescriptor().getColumnFamilies();
		
		for(HColumnDescriptor hd:famlis) {
			
			String cm=hd.getNameAsString();
									
			put.add(Bytes.toBytes(cm), Bytes.toBytes(column), Bytes.toBytes(value));
		
		}
			
		table.put(put);
	}

      public static void main(String[] args) throws IOException {		
      	
		addRow("wangmengchen", "1", "age","22");
	}

6、删除一行数据

//删除一行数据
	public static void deleteRow(String tableName,String rowKey,String cf) throws IOException {
		
		HTable table=new HTable(conf, tableName);
		
		Delete delete=new Delete(Bytes.toBytes(rowKey));
		
		table.delete(delete);
		
	}
	
	
	public static void main(String[] args) throws IOException {		
		deleteRow("wangmengchen", "1", null);
	}

7、删除多行数据

//删除多行数据
	public static void deleteRows(String tableName,String... rowKeys) throws IOException {	
		HTable table=new HTable(conf, tableName);
		
	   List<Delete> list=new ArrayList<>();			   
		for(String row:rowKeys) {
			
			Delete delete=new Delete(Bytes.toBytes(row));
			list.add(delete);	
			
		}
		table.delete(list);
	}

	public static void main(String[] args) throws IOException {		
		deleteRows("wangmengchen", "1","2");
	}
	

8、扫描数据

	//扫描数据
	public static void getAllRows(String tableName) throws IOException {
		
		HTable table=new HTable(conf, tableName);
		
		Scan scan=new Scan(Bytes.toBytes(0));
		
		ResultScanner resultScanner=table.getScanner(scan);
		
		for(Result result:resultScanner) {
			
			Cell[] cells=result.rawCells();
			for(Cell cell:cells) {
				System.out.println("行键"+Bytes.toString(CellUtil.cloneRow(cell)));
				System.out.println("列族"+Bytes.toString(CellUtil.cloneFamily(cell)));
				System.out.println("列"+Bytes.toString(CellUtil.cloneQualifier(cell)));
				System.out.println("值"+Bytes.toString(CellUtil.cloneValue(cell)));
				System.out.println("时间戳"+cell.getTimestamp());
			}
		}
	}
	
	
	public static void main(String[] args) throws IOException {		
		
		
		getAllRows("wangmengchen");
	}

9、获得一个具体的数据

//获得一个具体的数据
	public static void getRow(String tableName,String rowKey) throws IOException {
		HTable table=new HTable(conf, tableName);		
		
		Get get=new Get(Bytes.toBytes(rowKey));
		
		//get.addFamily(Bytes.toBytes("info1"));//获取info1列的值
		
		get.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("sex"));//获取info1下的属性名为sex的值
		Result result=table.get(get);
		Cell[] cells=result.rawCells();
		for(Cell cell:cells) {
			System.out.println("行键"+Bytes.toString(CellUtil.cloneRow(cell)));
			System.out.println("列族"+Bytes.toString(CellUtil.cloneFamily(cell)));
			System.out.println("列"+Bytes.toString(CellUtil.cloneQualifier(cell)));
			System.out.println("值"+Bytes.toString(CellUtil.cloneValue(cell)));
			System.out.println("时间戳"+cell.getTimestamp());			
		}
	}
	
	
	public static void main(String[] args) throws IOException {		
		
		
		getRow("wangmengchen","1");
	}

10、总结


  • HBaseAdmin(Admin):管理表(创建,删除)
  • HTableDescriptor:表描述器,用于构建表。
  • HColunDescriptor:列描述器(构建列表)。

  • Table:用于表中数据的操作
  • Put:用于封装存放的数据。
  • Delete:用于封装删除的数据。
  • Get:用于得到某一个具体的数据。

  • Scan:用于扫描表的配置信息。
  • ResultScanner:用过配置的扫描器,得到一个扫描表的实力扫描器。
  • Result:每一个该类型的实例化对象,都对应了一个rowKey中的若干数据。
  • Cell:用于封装一个rowKey下面所有单元格中的数据(rowKey,cf,cn,value)

www.htsjk.Com true http://www.htsjk.com/hbase/42160.html NewsArticle HBase的JAVA_API, HBase的JAVA_API 1、先加载包 ?xml version="1.0"? project xsi: schemaLocation = " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd " xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instan...
相关文章
    暂无相关文章
评论暂时关闭