Path src = new Path(\
Path dst = new Path(\
fs.copyFromLocalFile(src, dst);
System.out.println(\
FileStatus files[] = fs.listStatus(dst);
for(FileStatus file : files){
System.out.println(file.getPath());
} } }
实验名称:HBase环境搭建、sehll操作及Java API编程
一 目的
1.掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。 2.掌握安装和配置HBase基本方法。 3.掌握HBase shell的常用命令。
4.使用HBase shell命令进行表的创建,增加删除修改操作。 5.使用Java API进行表的创建,增加删除修改操作。 二 内容
1.完成Zookeeper和HBase的搭建。
2.使用HBase shell命令进行表的创建,增加删除修改操作。 3.使用Java API进行表的创建,增加删除修改操作。
三 步骤 步骤:
1.完成Zookeeper和HBase的搭建。
2.使用HBase shell命令进行表的创建,增加删除修改操作。 3.使用Java API进行表的创建,增加删除修改操作。 基本调试:
1.掌握Hbase在Hadoop集群体系结构中发挥的作用和使过程。 2.掌握安装和配置HBase基本方法。
3.掌握HBase shell的常用命令。
4.使用HBase shell命令进行表的创建,增加删除修改操作。 5.使用Java API进行表的创建,增加删除修改操作。
四 结果
结果成功,详见上传文档截图 五 疑难
对于HBase的命令练习还不够,经常有点提笔忘字的感觉。 还有就是java api的使用,感觉很乱
六 算法
package com.wujintao.hbase.test;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTablePool; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes;
public class JinTaoTest {
public static Configuration configuration; static {
configuration = HBaseConfiguration.create();
configuration.set(\ configuration.set(\ configuration.set(\ }
public static void main(String[] args) { // createTable(\ // insertData(\ // QueryAll(\
// QueryByCondition1(\ // QueryByCondition2(\ //QueryByCondition3(\ //deleteRow(\
deleteByCondition(\ }
/**
* 创建表
* @param tableName */
public static void createTable(String tableName) { System.out.println(\ try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(tableName); hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + \ }
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); tableDescriptor.addFamily(new HColumnDescriptor(\ tableDescriptor.addFamily(new HColumnDescriptor(\ tableDescriptor.addFamily(new HColumnDescriptor(\ hBaseAdmin.createTable(tableDescriptor); } catch (MasterNotRunningException e) { e.printStackTrace();
} catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
System.out.println(\ }
/**
* 插入数据
* @param tableName */
public static void insertData(String tableName) { System.out.println(\
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName);
Put put = new Put(\一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add(\本行数据的第一列 put.add(\本行数据的第三列 put.add(\本行数据的第三列 try {
table.put(put); } catch (IOException e) { e.printStackTrace(); }
System.out.println(\ }
/**
* 删除一张表
* @param tableName */
public static void dropTable(String tableName) { try {
HBaseAdmin admin = new HBaseAdmin(configuration); admin.disableTable(tableName); admin.deleteTable(tableName); } catch (MasterNotRunningException e) { e.printStackTrace();
} catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
} /**
* 根据 rowkey删除一条记录 * @param tablename * @param rowkey */
public static void deleteRow(String tablename, String rowkey) { try {
HTable table = new HTable(configuration, tablename); List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes()); list.add(d1);
table.delete(list);
System.out.println(\删除行成功!\
} catch (IOException e) { e.printStackTrace(); }
}
/**
* 组合条件删除 * @param tablename * @param rowkey */
public static void deleteByCondition(String tablename, String rowkey) {
//目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作
}
/**
* 查询所有数据 * @param tableName */
public static void QueryAll(String tableName) {
HTablePool pool = new HTablePool(configuration, 1000); HTable table = (HTable) pool.getTable(tableName); try {
ResultScanner rs = table.getScanner(new Scan());