java语言使用apche hbase保存最近7天数据的代码
时间: 2024-02-10 12:10:48 浏览: 80
以下是 Java 代码示例,使用 Apache HBase 存储最近 7 天数据:
```java
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
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.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseExample {
private static final String TABLE_NAME = "myTable";
private static final String FAMILY_NAME = "myFamily";
private static final String COLUMN_NAME = "myColumn";
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
// 创建命名空间
NamespaceDescriptor namespace = NamespaceDescriptor.create("myNamespace").build();
conn.getAdmin().createNamespace(namespace);
// 创建表
HTableDescriptor table = new HTableDescriptor(TableName.valueOf("myNamespace:" + TABLE_NAME));
HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
table.addFamily(family);
conn.getAdmin().createTable(table);
// 插入数据
Table myTable = conn.getTable(TableName.valueOf("myNamespace:" + TABLE_NAME));
Date now = new Date();
for (int i = 0; i < 10; i++) {
String rowKey = "row" + i;
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(FAMILY_NAME), Bytes.toBytes(COLUMN_NAME), now.getTime(), Bytes.toBytes("data" + i));
myTable.put(put);
}
// 查询数据
Get get = new Get(Bytes.toBytes("row0"));
Result result = myTable.get(get);
byte[] value = result.getValue(Bytes.toBytes(FAMILY_NAME), Bytes.toBytes(COLUMN_NAME));
System.out.println(Bytes.toString(value));
// 扫描数据
Calendar calendar = new GregorianCalendar();
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_MONTH, -7);
long timestamp = calendar.getTimeInMillis();
Filter filter = new PrefixFilter(Bytes.toBytes("row"));
Scan scan = new Scan();
scan.setFilter(filter);
scan.setTimeRange(timestamp, now.getTime());
myTable.getScanner(scan).forEach(r -> {
byte[] row = r.getRow();
byte[] data = r.getValue(Bytes.toBytes(FAMILY_NAME), Bytes.toBytes(COLUMN_NAME));
System.out.println(Bytes.toString(row) + ": " + Bytes.toString(data));
});
// 删除数据
Delete delete = new Delete(Bytes.toBytes("row0"));
myTable.delete(delete);
myTable.close();
conn.close();
}
}
```
该示例代码创建了一个名为“myNamespace:myTable”的表,其中包含一个名为“myFamily:myColumn”的列族,用于存储最近 7 天的数据。数据以行的形式存储,每行具有唯一的行键。在插入数据时,使用当前时间作为时间戳,以便以后可以根据时间范围进行扫描。在扫描数据时,使用前缀过滤器和时间范围过滤器来筛选最近 7 天的数据。在删除数据时,使用行键来标识要删除的数据。
阅读全文