背景
在
成本管家中,SLS提供了雲産品賬單的資料,通過SLS強大的分析和可視化能力,幫助客戶分析雲産品賬單,優化成本。在釋出後在客戶中間引起強烈的回報,不少客戶要求在賬單中增加執行個體名,Tag,資源用量等資訊,通過更多元度資料做分賬。客戶對分賬有比較強烈的需求,通過把賬單配置設定到不同的部門,讓不同部門各自完成自己的成本優化工作。為了滿足客戶的分賬需求,我們在成本管家二期中引入了更加豐富的賬單資料,本篇文檔,講介紹如何通過代碼一步建立賬單資料。
授權SLS成本管家擷取賬單資料
點選
授權連結,同意SLS成本管家擷取賬單資料。
通過Java SDK建立賬單資料
1. 引入Pom依賴
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-log</artifactId>
<version>0.6.45</version>
</dependency>
2. 複制程式
建立類檔案
bill_ingestion_creator
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.AliyunBSSSource;
import com.aliyun.openservices.log.common.Dashboard;
import com.aliyun.openservices.log.common.Ingestion;
import com.aliyun.openservices.log.common.IngestionConfiguration;
import com.aliyun.openservices.log.common.JobSchedule;
import com.aliyun.openservices.log.common.JobScheduleType;
import com.aliyun.openservices.log.common.JobState;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateDashboardRequest;
import com.aliyun.openservices.log.request.CreateIngestionRequest;
import com.aliyun.openservices.log.request.ListIngestionRequest;
import com.aliyun.openservices.log.response.ListIngestionResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class bill_ingestion_creator {
static String endpoint = "cn-chengdu.log.aliyuncs.com";
static String accessId = ""; // 修改成你的AccessId
static String accessKey = ""; // 修改成你的accessKey
static String aliuid = ""; // 修改成你的aliuid
static String getIngestionName() {
return "ingestion-" + (int) (new Date().getTime() / 1000);
}
private static Ingestion createIngestion() {
Ingestion ingestion = new Ingestion();
String jobName = getIngestionName();
ingestion.setName(jobName);
ingestion.setState(JobState.ENABLED);
ingestion.setDisplayName("BSS-test");
IngestionConfiguration configuration = new IngestionConfiguration();
configuration.setLogstore("aliyun_bill");
AliyunBSSSource source = new AliyunBSSSource();
source.setHistoryMonth(7);
source.setRoleARN("acs:ram::" + aliuid + ":role/AliyunLogAccessingBSSRole");
configuration.setSource(source);
ingestion.setConfiguration(configuration);
JobSchedule schedule = new JobSchedule();
schedule.setInterval("10m");
schedule.setType(JobScheduleType.FIXED_RATE);
schedule.setRunImmediately(true);
ingestion.setSchedule(schedule);
return ingestion;
}
public static void main(String args[]) throws LogException {
Ingestion ingestion = createIngestion();
String project = "bill-sls-" + aliuid;
String logstore = "aliyun_bill";
Client client = new Client(endpoint, accessId, accessKey);
String index = downFile("index");
try {
client.CreateProject(project, "bill analysis");
} catch (LogException e) {
e.printStackTrace();
}
try {
client.CreateLogStore(project, new LogStore("aliyun_bill", 3000, 5));
} catch (LogException e) {
e.printStackTrace();
}
try {
client.CreateIndex(project, logstore, index);
} catch (LogException e) {
e.printStackTrace();
}
String dashboards[] = {"dashboard_sls", "dashboard_oss", "dashboard_ecs", "dashboard_all", "bill_summary_cn", "bill_detail_cn", "bill_optimize_cn"};
for (String dashboardName : dashboards) {
Dashboard dashboard = new Dashboard();
String config = downFile(dashboardName);
dashboard.FromJsonString(config);
try {
client.createDashboard(new CreateDashboardRequest(project, dashboard));
}
catch (Exception e) {
e.printStackTrace();
}
}
ListIngestionResponse response1 = client.listIngestion(new ListIngestionRequest(project));
if(response1.getResults().size()> 0) {
System.out.println("ingestion is already created,don't recreate");
}
else {
client.createIngestion(new CreateIngestionRequest(project, ingestion));
}
response1 = client.listIngestion(new ListIngestionRequest(project));
for (Ingestion ingestion1 : response1.getResults()) {
System.out.println("bill ingestion:" + ingestion1.getName() + ", project:" + project + ", logstore :" + ingestion1.getConfiguration().getLogstore());
} }
public static String downFile(String fileName) {
try {
URL url = new URL("http://logdemo.oss-cn-beijing.aliyuncs.com/bill_logstore/" + fileName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000);
InputStream inputStream = conn.getInputStream();
byte[] buf = readInputStream(inputStream);
return new String(buf);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
3. 修改賬号參數
在上述程式中,修改以下三個參數,分别是accessId, accessKey, aliuid。
static String accessId = ""; // 修改成你的AccessId
static String accessKey = ""; // 修改成你的accessKey
static String aliuid = ""; // 修改成你的aliuid
檢視賬單資料
以上操作完成後,等待一分鐘,即可進入
sls控制台,選擇bill-sls-開頭的賬單即可。

- 在logstore清單中選擇aliyun_bill,即可檢視賬單原始資料。
資料中台賦能雲成本管理(1):建立賬單項目 - 在dashboard中選擇對應的報表,即可檢視系統SLS提供的内置報表。
賬單原始資料:
賬單日期 BillingDate: 2020-02-05
計費項 BillingItem: 流出流量
計費類型 BillingType: 其它
消費機關 CostUnit: 未配置設定
貨币 Currency: CNY
現金券抵扣 DeductedByCashCoupons: 0.0
代金券抵扣 DeductedByCoupons: 0.0
預存卡抵扣 DeductedByPrepaidCard: 0.0
資源包抵扣 DeductedByResourcePackage: 0
執行個體配置 InstanceConfig: iz:華東 1 可用區 F;執行個體規格名稱:ecs.xn4.small;CPU:1;記憶體:1024;磁盤:1;帶寬:512000;OS:UBUNTU;是否io優化:io_optimized_only
執行個體ID InstanceID: i-bp14putxkqvmal310ian
執行個體描述 InstanceSpec: ecs.xn4.small
公網IP InternetIP: 47.96.36.117
内網IP IntranetIP: 10.80.65.37
發票抵扣 InvoiceDiscount: 0.0
付費類型 Item: PayAsYouGoBill
單價 ListPrice: 0.800000
單價機關 ListPriceUnit: 元/Mbps
昵稱 NickName: iZbp14putxkqvmal310ianZ
OutstandingAmount: 0.0
OwnerID: 1340796328858956
付費金額 PaymentAmount: 0.0
稅前金額 PretaxAmount: 0.0
稅前原始金額 PretaxGrossAmount: 0.009
産品ProductCode: ecs
産品詳情 ProductDetail: 雲伺服器ECS-按量付費
産品名稱 ProductName: 雲伺服器 ECS
産品類型 ProductType:
地域 Region: 華東1(杭州)
資源組 ResourceGroup: 預設資源組
服務周期 ServicePeriod: 86400
訂閱類型 SubscriptionType: PayAsYouGo
标簽 Tag: key:department value:mayunlei
使用量 Usage: 0.011000
使用量機關 UsageUnit: Mbps
地域 Zone: cn-hangzhou-f
__source__: instance_bill
進行自定義分析
在執行個體賬單分析中,各個企業的分賬模式各不相同,有的采用TAG方式,有的在執行個體名稱中定義責任人,有的在昵稱中定義責任人,不同的方式如何進行分賬管理,請參考本系列接下來的文章。