0
点赞
收藏
分享

微信扫一扫

Hbase创建表。查询表。Java API 实现


需求:

Hbase创建表。查询表。Java API 实现_hadoop

 

直接上代码:

package hbase_test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
* Created by hadoopuser on 8/14/19.
*/
public class hbase_Test1 {


private static final String TABLE_NAME="employee";

public static final String FAMILY_NAME_1 = "profile";
public static final String FAMILY_NAME_2 = "department";
public static final String FAMILY_NAME_3 = "income";

//conf
private static Configuration getHBaseConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum",
"localhost");
conf.set("zookeeper.znode.parent", "/hbase");
return conf;
}

//createTable
private static void createTable(Configuration conf) throws IOException {
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();

if (!admin.tableExists(TableName.valueOf(TABLE_NAME))) {
//create table ,create family
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
HColumnDescriptor columnDescriptor_1 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_1));
HColumnDescriptor columnDescriptor_2 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_2));
HColumnDescriptor columnDescriptor_3 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_3));
tableDescriptor.addFamily(columnDescriptor_1);
tableDescriptor.addFamily(columnDescriptor_2);
tableDescriptor.addFamily(columnDescriptor_3);
admin.createTable(tableDescriptor);


} else {
System.err.println("table is exists!!!!!");
}

//put data
table = connection.getTable(TableName.valueOf(TABLE_NAME));

Put put=new Put(Bytes.toBytes("e001")); //rowkey 1
put.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("31"));

put.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("finance"));

put.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("7000"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("125"));



Put put2=new Put(Bytes.toBytes("e002")); //rowkey 1
put2.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Tome"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("34"));

put2.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("legal"));

put2.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("6000"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("100"));


Put put3=new Put(Bytes.toBytes("e003")); //rowkey 1
put3.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Lily"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("58"));

put3.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("market"));

put3.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("12000"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("300"));

table.put(put);
table.put(put2);
table.put(put3);

Get getE001=new Get(Bytes.toBytes("e001"));
//String result=getE001.getFamilyMap().get("salary").toString();
//System.out.println(result);

byte [] ss=table.get(getE001).getValue(Bytes.toBytes(FAMILY_NAME_3),Bytes.toBytes("salary"));
System.out.println("读出rowkey为“e001”的income:salary: "+new String(ss));


Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes("e001"));
scan.setStopRow(Bytes.toBytes("e004")); //到 004 要不然003 的值不会输出
scan.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"));
scan.setCaching(100);

ResultScanner results=table.getScanner(scan);

for (Result result : results) {
while (result.advance()) {
System.out.println("name :"+new String(result.current().getValue()));

}
}

}catch (IOException e){
e.printStackTrace();
}finally {
//close
if (table != null) table.close();
if (connection != null) connection.close();
}
}


public static void main(String [] args){
Configuration conf=getHBaseConfiguration();
try {
createTable(conf);
} catch (IOException e) {
e.printStackTrace();
}finally {

}
}


}

结果先查看存入的表:

Hbase创建表。查询表。Java API 实现_apache_02

运行结果:

 

Hbase创建表。查询表。Java API 实现_Hbase_03

举报

相关推荐

0 条评论